Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

custom text on the picture #501

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,10 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle
// Image generation provided by LoremPixel (http://lorempixel.com/)
imageUrl($width = 640, $height = 480) // 'http://lorempixel.com/640/480/'
imageUrl($width, $height, 'cats') // 'http://lorempixel.com/800/600/cats/'
imageUrl($width, $height, 'cats', false, 'Faker') // 'http://lorempixel.com/800/400/cats/Faker'
image($dir = '/tmp', $width = 640, $height = 480) // '/tmp/13b73edae8443990be1aa8f1a483bc27.jpg'
image($dir, $width, $height, 'cats') // 'tmp/13b73edae8443990be1aa8f1a483bc27.jpg' it's a cat!
image($dir, $width, $height, 'cats', false, 'Faker') // 'tmp/13b73edae8443990be1aa8f1a483bc27.jpg' it's a cat with Faker text

### `Faker\Provider\Uuid`

Expand Down
10 changes: 7 additions & 3 deletions src/Faker/Provider/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ class Image extends Base
*
* @example 'http://lorempixel.com/640/480/?12345'
*/
public static function imageUrl($width = 640, $height = 480, $category = null, $randomize = true)
public static function imageUrl($width = 640, $height = 480, $category = null, $randomize = true, $word = null)
{
$url = "http://lorempixel.com/{$width}/{$height}/";
if ($category) {
if (!in_array($category, static::$categories)) {
throw new \InvalidArgumentException(sprintf('Unkown image category "%s"', $category));
}
$url .= "{$category}/";

if ($word) {
$url .= "{$word}/";
}
}

if ($randomize) {
Expand All @@ -43,7 +47,7 @@ public static function imageUrl($width = 640, $height = 480, $category = null, $
*
* @example '/path/to/dir/13b73edae8443990be1aa8f1a483bc27.jpg'
*/
public static function image($dir = '/tmp', $width = 640, $height = 480, $category = null, $fullPath = true)
public static function image($dir = '/tmp', $width = 640, $height = 480, $category = null, $fullPath = true, $randomize = true, $word = null)
{
// Validate directory path
if (!is_dir($dir) || !is_writable($dir)) {
Expand All @@ -56,7 +60,7 @@ public static function image($dir = '/tmp', $width = 640, $height = 480, $catego
$filename = $name .'.jpg';
$filepath = $dir . DIRECTORY_SEPARATOR . $filename;

$url = static::imageUrl($width, $height, $category);
$url = static::imageUrl($width, $height, $category, $randomize, $word);

// save file
if (function_exists('curl_exec')) {
Expand Down
5 changes: 5 additions & 0 deletions test/Faker/Provider/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public function testImageUrlAcceptsCustomCategory()
$this->assertRegExp('#^http://lorempixel.com/800/400/nature/#', Image::imageUrl(800, 400, 'nature'));
}

public function testImageUrlAcceptsCustomText()
{
$this->assertRegExp('#^http://lorempixel.com/800/400/nature/Faker#', Image::imageUrl(800, 400, 'nature', false, 'Faker'));
}

public function testImageUrlAddsARandomGetParameterByDefault()
{
$url = Image::imageUrl(800, 400);
Expand Down