diff --git a/readme.md b/readme.md index d94181a6..548b94a7 100644 --- a/readme.md +++ b/readme.md @@ -243,6 +243,24 @@ $faker->optional($weight = 0.9)->randomDigit; // 90% chance to get null // the default $weight value is 0.5 ``` +### `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' + 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/' + ## 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..153e2d92 --- /dev/null +++ b/src/Faker/Provider/Image.php @@ -0,0 +1,77 @@ +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() + { + 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); + } + } +}