1
0
mirror of https://github.com/fzaninotto/Faker.git synced 2025-03-19 23:09:47 +01:00

Image generation powered by LoremPixel

This commit is contained in:
Robert Reinhard 2013-08-23 10:44:46 -07:00 committed by Francois Zaninotto
parent 1c018f1ef4
commit 02107bc738
3 changed files with 140 additions and 0 deletions

View File

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

View File

@ -0,0 +1,79 @@
<?php
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;
}
}
/**
* Generate the URL that will return a random image
* @example 'http://lorempixel.com/1160/1160/'
*/
static public function imageUrl($width = 1160, $height = 1160, $category = null)
{
$url = "http://lorempixel.com/{$width}/{$height}/";
if ($category) {
$category = static::singular($category);
if (in_array($category, static::$categories)) $url .= "{$category}/";
}
return $url;
}
/**
* Download a remote URL to disk
* @example '/path/to/dir/13b73edae8443990be1aa8f1a483bc27.jpg'
*/
static public function image($dir, $width = 1160, $height = 1160, $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;
}
/**
* 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);
}
}

View File

@ -0,0 +1,43 @@
<?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/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/');
}
}