From 02107bc738466b32348734318c9c3824b5d4a472 Mon Sep 17 00:00:00 2001 From: Robert Reinhard Date: Fri, 23 Aug 2013 10:44:46 -0700 Subject: [PATCH 1/2] Image generation powered by LoremPixel --- readme.md | 18 +++++++ src/Faker/Provider/Image.php | 79 +++++++++++++++++++++++++++++++ test/Faker/Provider/ImageTest.php | 43 +++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 src/Faker/Provider/Image.php create mode 100644 test/Faker/Provider/ImageTest.php diff --git a/readme.md b/readme.md index d94181a6..9f291118 100644 --- a/readme.md +++ b/readme.md @@ -195,6 +195,7 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle safeColorName // 'fuchsia' colorName // 'Gainsbor' +<<<<<<< HEAD ### `Faker\Provider\Payment` creditCardType // 'MasterCard' @@ -243,6 +244,23 @@ $faker->optional($weight = 0.9)->randomDigit; // 90% chance to get null // the default $weight value is 0.5 ``` +### `Faker\Provider\Image` + + image($dir) // '/path/to/dir/13b73edae8443990be1aa8f1a483bc27.jpg' + image($dir, $width, $height) // '/path/to/dir/13b73edae8443990be1aa8f1a483bc27.jpg' + image($dir, $width, $height, $category) // '/path/to/dir/13b73edae8443990be1aa8f1a483bc27.jpg' + imageUrl // 'http://lorempixel.com/1160/1160/' + imageUrl($width, $height) // 'http://lorempixel.com/800/600/' + imageUrl($width, $height, $category) // 'http://lorempixel.com/800/600/person/' + personImage($dir) // '/path/to/dir/13b73edae8443990be1aa8f1a483bc27.jpg' + personImageUrl // 'http://lorempixel.com/1160/1160/person/' + +Image generation is done by [LoremPixel](http://lorempixel.com/. Common params are: + +* $dir - An absolute path to a local directory +* $width/$height - Integer width and heigh values for the image you want generated +* $category - May be 'abstract', 'animals','business','cats','city','food','nightlife','fashion','people','nature','sports','technics','transport'. Singular forms of all are supported as well. + ## Localization `Faker\Factory` can take a locale as an argument, to return localized data. If no localized provider is found, the factory fallbacks to the default locale (en_EN). diff --git a/src/Faker/Provider/Image.php b/src/Faker/Provider/Image.php new file mode 100644 index 00000000..b0984eb3 --- /dev/null +++ b/src/Faker/Provider/Image.php @@ -0,0 +1,79 @@ +assertEquals(Image::imageUrl(), 'http://lorempixel.com/1160/1160/'); + } + + public function testUrlWithDimensions() + { + $this->assertEquals(Image::imageUrl(800, 400), 'http://lorempixel.com/800/400/'); + } + + public function testUrlWithDimensionsAndCategory() + { + $this->assertEquals(Image::imageUrl(800, 400, 'nature'), 'http://lorempixel.com/800/400/nature/'); + } + + public function testUrlWithDimensionsAndBadCategory() + { + $this->assertEquals(Image::imageUrl(800, 400, 'bullhonky'), 'http://lorempixel.com/800/400/'); + } + + public function testDownloadWithDefaults() { + $file = Image::image('/tmp'); + $this->assertFileExists($file); + if (file_exists($file)) unlink($file); + } + + public function testMagicCategory() { + $this->assertEquals(Image::peopleImageUrl(800, 400, 'nature'), 'http://lorempixel.com/800/400/nature/'); + // $faker = \Faker\Factory::create(); + // $this->assertEquals(Image::peopleImageUrl(800, 400, 'nature'), 'http://lorempixel.com/800/400/nature/'); + + } + +} From f2d5245b002463140afdcda218e64b9865976b8e Mon Sep 17 00:00:00 2001 From: Francois Zaninotto Date: Mon, 21 Oct 2013 21:36:05 +0200 Subject: [PATCH 2/2] Fix coding standards, remove `__callStatic()`, add cURL and change defaults --- readme.md | 14 ++-- src/Faker/Provider/Image.php | 114 +++++++++++++++--------------- test/Faker/Provider/ImageTest.php | 43 ++++++----- 3 files changed, 87 insertions(+), 84 deletions(-) diff --git a/readme.md b/readme.md index 9f291118..548b94a7 100644 --- a/readme.md +++ b/readme.md @@ -195,7 +195,6 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle safeColorName // 'fuchsia' colorName // 'Gainsbor' -<<<<<<< HEAD ### `Faker\Provider\Payment` creditCardType // 'MasterCard' @@ -246,6 +245,13 @@ $faker->optional($weight = 0.9)->randomDigit; // 90% chance to get null ### `Faker\Provider\Image` + /** + * Image generation provided by LoremPixel (http://lorempixel.com/) + * + * @param $dir An absolute path to a local directory + * @param $width/$height Size (in pixel) of the generated image (defaults to 640x480) + * @param $category One of 'abstract','animals','business','cats','city','food','nightlife','fashion','people','nature','sports','technics', and 'transport' + */ image($dir) // '/path/to/dir/13b73edae8443990be1aa8f1a483bc27.jpg' image($dir, $width, $height) // '/path/to/dir/13b73edae8443990be1aa8f1a483bc27.jpg' image($dir, $width, $height, $category) // '/path/to/dir/13b73edae8443990be1aa8f1a483bc27.jpg' @@ -255,12 +261,6 @@ $faker->optional($weight = 0.9)->randomDigit; // 90% chance to get null personImage($dir) // '/path/to/dir/13b73edae8443990be1aa8f1a483bc27.jpg' personImageUrl // 'http://lorempixel.com/1160/1160/person/' -Image generation is done by [LoremPixel](http://lorempixel.com/. Common params are: - -* $dir - An absolute path to a local directory -* $width/$height - Integer width and heigh values for the image you want generated -* $category - May be 'abstract', 'animals','business','cats','city','food','nightlife','fashion','people','nature','sports','technics','transport'. Singular forms of all are supported as well. - ## Localization `Faker\Factory` can take a locale as an argument, to return localized data. If no localized provider is found, the factory fallbacks to the default locale (en_EN). diff --git a/src/Faker/Provider/Image.php b/src/Faker/Provider/Image.php index b0984eb3..153e2d92 100644 --- a/src/Faker/Provider/Image.php +++ b/src/Faker/Provider/Image.php @@ -5,75 +5,73 @@ namespace Faker\Provider; /** * Depends on image generation from http://lorempixel.com/ */ -class Image extends Base { - - static protected $categories = array('abstract','animals','business','cats','city','food','nightlife','fashion','people','nature','sports','technics','transport'); - - /** - * Massage singular and other alternate versions of categories - */ - static private function singular($category) { - switch($category) { - case 'animal': return 'animals'; - case 'cat': return 'cats'; - case 'person': return 'people'; - case 'sport': return 'sports'; - case 'technic': return 'tehnics'; - case 'technical': return 'tehnics'; - default: return $category; - } - } - +class Image extends Base +{ + protected static $categories = array( + 'abstract', 'animals', 'business', 'cats', 'city', 'food', 'nightlife', + 'fashion', 'people', 'nature', 'sports', 'technics', 'transport' + ); + /** * Generate the URL that will return a random image - * @example 'http://lorempixel.com/1160/1160/' + * + * @example 'http://lorempixel.com/640/480/' */ - static public function imageUrl($width = 1160, $height = 1160, $category = null) + public static function imageUrl($width = 640, $height = 480, $category = null) { $url = "http://lorempixel.com/{$width}/{$height}/"; if ($category) { - $category = static::singular($category); - if (in_array($category, static::$categories)) $url .= "{$category}/"; + if (!in_array($category, static::$categories)) { + throw new \InvalidArgumentException(sprintf('Unkown image category "%s"', $category)); + } + $url .= "{$category}/"; } + return $url; } - + /** - * Download a remote URL to disk + * Download a remote random image to disk and return its location + * + * Requires curl, or allow_url_fopen to be on in php.ini. + * * @example '/path/to/dir/13b73edae8443990be1aa8f1a483bc27.jpg' */ - static public function image($dir, $width = 1160, $height = 1160, $category = null) { - + public static function image($dir, $width = 640, $height = 480, $category = null) + { // Validate directory path - if (!is_dir($dir) || !is_writable($dir)) return false; - - // Generate a random filename. Use the server address so that a file generated at the same - // time on a different server won't have a collision. - if (substr($dir, -1, 1) != '/') $dir .= '/'; - $name = md5(uniqid(empty($_SERVER['SERVER_ADDR'])?:$_SERVER['SERVER_ADDR'], true)); - $dst = $dir.$name.'.jpg'; - - // Save file - if (copy(static::imageUrl($width, $height, $category), $dst)) return $dst; - else return false; - + if (!is_dir($dir) || !is_writable($dir)) { + throw new \InvalidArgumentException(sprintf('Cannot write to directory "%s"', $dir)); + } + + // Generate a random filename. Use the server address so that a file + // generated at the same time on a different server won't have a collision. + $name = md5(uniqid(empty($_SERVER['SERVER_ADDR']) ? '' : $_SERVER['SERVER_ADDR'], true)); + $filepath = $dir . DIRECTORY_SEPARATOR . $name .'.jpg'; + + $url = static::imageUrl($width, $height, $category); + + // save file + if (function_exists('curl_exec')) { + // use cURL + $fp = fopen($filepath, 'w'); + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_FILE, $fp); + $success = curl_exec($ch); + curl_close($ch); + fclose($fp); + } elseif (ini_get('allow_url_fopen')) { + // use remote fopen() via copy() + $success = copy($url, $filepath); + } else { + return new \RuntimeException('The image formatter downloads an image from a remote HTTP server. Therefore, it requires that PHP can request remote hosts, either via cURL or fopen()'); + } + + if (!$success) { + // could not contact the distant URL or HTTP error - fail silently. + return false; + } + + return $filepath; } - - /** - * Generate an image using the category name. Like "Image::natureImageUrl()" - * @example 'http://lorempixel.com/1160/1160/' - */ - public static function __callStatic($name, $arguments) { - - // Strip the category from the name - if (!($i = strpos($name, 'Image'))) return false; - $category = substr($name, 0, $i); - $function = substr(str_replace('Image', 'image', $name), $i); - if (!in_array($function, array('image', 'imageUrl'))) return false; - - // Call the real function - return call_user_func_array(__CLASS__.'::'.$function, $arguments); - - } - -} \ No newline at end of file +} diff --git a/test/Faker/Provider/ImageTest.php b/test/Faker/Provider/ImageTest.php index a3643967..8f64a1a5 100644 --- a/test/Faker/Provider/ImageTest.php +++ b/test/Faker/Provider/ImageTest.php @@ -6,38 +6,43 @@ use Faker\Provider\Image; class ImageTest extends \PHPUnit_Framework_TestCase { - public function testUrlWithDefaults() { - $this->assertEquals(Image::imageUrl(), 'http://lorempixel.com/1160/1160/'); + $this->assertEquals(Image::imageUrl(), 'http://lorempixel.com/640/480/'); } - + public function testUrlWithDimensions() { $this->assertEquals(Image::imageUrl(800, 400), 'http://lorempixel.com/800/400/'); } - + public function testUrlWithDimensionsAndCategory() { $this->assertEquals(Image::imageUrl(800, 400, 'nature'), 'http://lorempixel.com/800/400/nature/'); } - + + /** + * @expectedException \InvalidArgumentException + */ public function testUrlWithDimensionsAndBadCategory() { - $this->assertEquals(Image::imageUrl(800, 400, 'bullhonky'), 'http://lorempixel.com/800/400/'); - } - - public function testDownloadWithDefaults() { - $file = Image::image('/tmp'); - $this->assertFileExists($file); - if (file_exists($file)) unlink($file); - } - - public function testMagicCategory() { - $this->assertEquals(Image::peopleImageUrl(800, 400, 'nature'), 'http://lorempixel.com/800/400/nature/'); - // $faker = \Faker\Factory::create(); - // $this->assertEquals(Image::peopleImageUrl(800, 400, 'nature'), 'http://lorempixel.com/800/400/nature/'); - + Image::imageUrl(800, 400, 'bullhonky'); } + public function testDownloadWithDefaults() + { + $file = Image::image(sys_get_temp_dir()); + $this->assertFileExists($file); + if (function_exists('getimagesize')) { + list($width, $height, $type, $attr) = getimagesize($file); + $this->assertEquals(640, $width); + $this->assertEquals(480, $height); + $this->assertEquals(constant('IMAGETYPE_JPEG'), $type); + } else { + $this->assertEquals('jpg', pathinfo($file, PATHINFO_EXTENSION)); + } + if (file_exists($file)) { + unlink($file); + } + } }