1
0
mirror of https://github.com/fzaninotto/Faker.git synced 2025-03-22 08:19:52 +01:00

Merge pull request #193 from fzaninotto/weotch-image-generation

Image generation powered by LoremPixel (Fixed and rebased)
This commit is contained in:
Francois Zaninotto 2013-10-21 13:36:58 -07:00
commit 5c8ffa5561
3 changed files with 143 additions and 0 deletions

View File

@ -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).

View File

@ -0,0 +1,77 @@
<?php
namespace Faker\Provider;
/**
* Depends on image generation from http://lorempixel.com/
*/
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/640/480/'
*/
public static function imageUrl($width = 640, $height = 480, $category = 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}/";
}
return $url;
}
/**
* 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'
*/
public static function image($dir, $width = 640, $height = 480, $category = null)
{
// Validate directory path
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;
}
}

View File

@ -0,0 +1,48 @@
<?php
namespace Faker\Test\Provider;
use Faker\Provider\Image;
class ImageTest extends \PHPUnit_Framework_TestCase
{
public function testUrlWithDefaults()
{
$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()
{
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);
}
}
}