mirror of
https://github.com/fzaninotto/Faker.git
synced 2025-03-24 09:19:50 +01:00
Fix coding standards, remove __callStatic()
, add cURL and change defaults
This commit is contained in:
parent
02107bc738
commit
f2d5245b00
14
readme.md
14
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).
|
||||
|
@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user