Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 27 additions & 39 deletions src/Provider/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace Faker\Provider;

/**
* Depends on image generation from http://lorempixel.com/
* Depends on image generation from https://picsum.photos/
*/
class Image extends Base
{
/**
* @var string
*/
public const BASE_URL = 'https://via.placeholder.com';
public const BASE_URL = 'https://picsum.photos';

public const FORMAT_JPG = 'jpg';
public const FORMAT_JPEG = 'jpeg';
Expand All @@ -21,17 +21,14 @@ class Image extends Base
*
* @deprecated Categories are no longer used as a list in the placeholder API but referenced as string instead
*/
protected static $categories = [
'abstract', 'animals', 'business', 'cats', 'city', 'food', 'nightlife',
'fashion', 'people', 'nature', 'sports', 'technics', 'transport',
];
protected static $categories = [];

/**
* Generate the URL that will return a random image
*
* Set randomize to false to remove the random GET parameter at the end of the url.
*
* @example 'http://via.placeholder.com/640x480.png/CCCCCC?text=well+hi+there'
* @example 'https://picsum.photos/640/480'
*
* @param int $width
* @param int $height
Expand All @@ -50,15 +47,14 @@ public static function imageUrl(
$randomize = true,
$word = null,
$gray = false,
$format = 'png'
$format = 'jpg'
) {
trigger_deprecation(
'fakerphp/faker',
'1.20',
'Provider is deprecated and will no longer be available in Faker 2. Please use a custom provider instead',
);

// Validate image format
$imageFormats = static::getFormats();

if (!in_array(strtolower($format), $imageFormats, true)) {
Expand All @@ -69,31 +65,29 @@ public static function imageUrl(
));
}

$size = sprintf('%dx%d.%s', $width, $height, $format);
$url = sprintf('%s/%d/%d', self::BASE_URL, $width, $height);

$imageParts = [];

if ($category !== null) {
$imageParts[] = $category;
if (in_array(strtolower($format), ['jpg', 'jpeg'])) {
$url .= '.jpg';
} elseif (strtolower($format) === 'png') {
$url .= '.png';
}

if ($word !== null) {
$imageParts[] = $word;
$queryParams = [];

if ($gray === true) {
$queryParams['grayscale'] = true;
}

if ($randomize === true) {
$imageParts[] = Lorem::word();
$queryParams['random'] = mt_rand(1, 100000);
}

$backgroundColor = $gray === true ? 'CCCCCC' : str_replace('#', '', Color::safeHexColor());
if (!empty($queryParams)) {
$url .= '?' . http_build_query($queryParams);
}

return sprintf(
'%s/%s/%s%s',
self::BASE_URL,
$size,
$backgroundColor,
count($imageParts) > 0 ? '?text=' . urlencode(implode(' ', $imageParts)) : '',
);
return $url;
}

/**
Expand All @@ -114,51 +108,45 @@ public static function image(
$randomize = true,
$word = null,
$gray = false,
$format = 'png'
$format = 'jpg'
) {
trigger_deprecation(
'fakerphp/faker',
'1.20',
'Provider is deprecated and will no longer be available in Faker 2. Please use a custom provider instead',
);

$dir = null === $dir ? sys_get_temp_dir() : $dir; // GNU/Linux / OS X / Windows compatible
$dir = null === $dir ? sys_get_temp_dir() : $dir;

// 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));
$filename = sprintf('%s.%s', $name, $format);
$filepath = $dir . DIRECTORY_SEPARATOR . $filename;

$url = static::imageUrl($width, $height, $category, $randomize, $word, $gray, $format);

// save file
if (function_exists('curl_exec')) {
// use cURL
$fp = fopen($filepath, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$success = curl_exec($ch) && curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200;
fclose($fp);
curl_close($ch);

if (!$success) {
unlink($filepath);

// could not contact the distant URL or HTTP error - fail silently.
if (file_exists($filepath)) {
unlink($filepath);
}
return false;
}
} elseif (ini_get('allow_url_fopen')) {
// use remote fopen() via copy()
$success = copy($url, $filepath);
$success = @copy($url, $filepath);

if (!$success) {
// could not contact the distant URL or HTTP error - fail silently.
return false;
}
} else {
Expand Down Expand Up @@ -193,4 +181,4 @@ public static function getFormatConstants(): array
static::FORMAT_PNG => constant('IMAGETYPE_PNG'),
];
}
}
}