Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions roave-bc-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ parameters:
- '#\[BC\] REMOVED: Method Faker\\Core\\DateTime\#setTimezone\(\) was removed#'
- '#\[BC\] REMOVED: Method Faker\\Generator\#uuid3\(\) was removed#'
- '#\[BC\] REMOVED: Method Faker\\Generator\#uuid\(\) was removed#'
- '#\[BC\] CHANGED: Value of constant Faker\\Provider\\Image::BASE_URL changed from ''https://via.placeholder.com'' to ''https://placehold.co''#'
19 changes: 19 additions & 0 deletions src/Faker/Provider/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,25 @@ public static function safeHexColor()
return '#' . $color[0] . $color[0] . $color[1] . $color[1] . $color[2] . $color[2];
}

/**
* Returns a suitable text color for the given background color.
* If the background is dark, returns white (`FFFFFF`); if light, returns black (`000000`).
*
* @param string $backgroundHex Background color in hex format (e.g. '#CCCCCC')
*
* @return string Text color hex code ('000000' or 'FFFFFF')
*/
public static function safeTextColorForBackground($backgroundHex)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing tests for this new method

{
$backgroundHex = str_replace('#', '', $backgroundHex);
$r = hexdec(substr($backgroundHex, 0, 2));
$g = hexdec(substr($backgroundHex, 2, 2));
$b = hexdec(substr($backgroundHex, 4, 2));
$luminance = (0.299 * $r + 0.587 * $g + 0.114 * $b);

return $luminance > 186 ? '000000' : 'FFFFFF';
}

/**
* @example 'array(0,255,122)'
*
Expand Down
13 changes: 8 additions & 5 deletions src/Faker/Provider/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Image extends Base
/**
* @var string
*/
public const BASE_URL = 'https://via.placeholder.com';
public const BASE_URL = 'https://placehold.co';

public const FORMAT_JPG = 'jpg';
public const FORMAT_JPEG = 'jpeg';
Expand All @@ -31,7 +31,7 @@ class Image extends Base
*
* 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://placehold.co/640x480/CCCCCC/0000000/png?text=well+hi+there'
*
* @param int $width
* @param int $height
Expand Down Expand Up @@ -70,7 +70,7 @@ public static function imageUrl(
));
}

$size = sprintf('%dx%d.%s', $width, $height, $format);
$size = sprintf('%dx%d', $width, $height);

$imageParts = [];

Expand All @@ -87,12 +87,15 @@ public static function imageUrl(
}

$backgroundColor = $gray === true ? 'CCCCCC' : str_replace('#', '', Color::safeHexColor());
$textColor = $gray === true ? '000000' : Color::safeTextColorForBackground($backgroundColor);

return sprintf(
'%s/%s/%s%s',
'%s/%s/%s/%s/%s%s',
self::BASE_URL,
$size,
$backgroundColor,
$textColor,
$format,
count($imageParts) > 0 ? '?text=' . urlencode(implode(' ', $imageParts)) : '',
);
}
Expand Down Expand Up @@ -142,7 +145,7 @@ public static function image(
// save file
if (function_exists('curl_exec')) {
// use cURL
$fp = fopen($filepath, 'w');
$fp = fopen($filepath, 'wb');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
$success = curl_exec($ch) && curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200;
Expand Down
14 changes: 7 additions & 7 deletions test/Faker/Provider/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,31 @@ final class ImageTest extends TestCase
public function testImageUrlUses640x680AsTheDefaultSize(): void
{
self::assertMatchesRegularExpression(
'#^https://via.placeholder.com/640x480.png/#',
'#^https://placehold.co/640x480#',
Image::imageUrl(),
);
}

public function testImageUrlAcceptsCustomWidthAndHeight(): void
{
self::assertMatchesRegularExpression(
'#^https://via.placeholder.com/800x400.png/#',
'#^https://placehold.co/800x400#',
Image::imageUrl(800, 400),
);
}

public function testImageUrlAcceptsCustomCategory(): void
{
self::assertMatchesRegularExpression(
'#^https://via.placeholder.com/800x400.png/[\w]{6}\?text=nature\+.*#',
'#^https://placehold.co/800x400/[\w]{6}/[\w]{6}/png\?text=nature\+.*#',
Image::imageUrl(800, 400, 'nature'),
);
}

public function testImageUrlAcceptsCustomText(): void
{
self::assertMatchesRegularExpression(
'#^https://via.placeholder.com/800x400.png/[\w]{6}\?text=nature\+Faker#',
'#^https://placehold.co/800x400/[\w]{6}/[\w]{6}/png\?text=nature\+Faker#',
Image::imageUrl(800, 400, 'nature', false, 'Faker'),
);
}
Expand All @@ -54,7 +54,7 @@ public function testImageUrlReturnsLinkToRegularImageWhenGrayIsFalse(): void
);

self::assertMatchesRegularExpression(
'#^https://via.placeholder.com/800x400.png/[\w]{6}\?text=nature\+Faker#',
'#^https://placehold.co/800x400/[\w]{6}/[\w]{6}/png\?text=nature\+Faker#',
$imageUrl,
);
}
Expand All @@ -71,7 +71,7 @@ public function testImageUrlReturnsLinkToRegularImageWhenGrayIsTrue(): void
);

self::assertMatchesRegularExpression(
'#^https://via.placeholder.com/800x400.png/CCCCCC\?text=nature\+Faker#',
'#^https://placehold.co/800x400/CCCCCC/000000/png\?text=nature\+Faker#',
$imageUrl,
);
}
Expand Down Expand Up @@ -113,7 +113,7 @@ public function testImageUrlAcceptsDifferentImageFormats(): void
);

self::assertMatchesRegularExpression(
"#^https://via.placeholder.com/800x400.{$format}/CCCCCC\?text=nature\+Faker#",
"#^https://placehold.co/800x400/CCCCCC/000000/{$format}\?text=nature\+Faker#",
$imageUrl,
);
}
Expand Down