Skip to content

Commit

Permalink
⬆️ Require PHP 8.0, use named parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoverbruggen committed Mar 14, 2021
1 parent e231546 commit e9e9e5c
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 111 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

This package is intended to be used for quickly generating placeholder images with a specific size, color and text.

Now that PHP 8.0 supports named arguments, I've updated this package.

## Requirements

* PHP 7.0 or higher
* PHP 8.0 or higher
* GD extension

## Usage
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
},
"require": {
"php": ">=7.0",
"ext-gd" : ">=7.0"
"php": ">=8.0",
"ext-gd" : ">=8.0"
}
}
16 changes: 6 additions & 10 deletions examples/direct.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@

// If you point your browser at this file, it will spew out a PNG. Enjoy.

// Create a new instance of ImageGenerator
$generator = new ImageGenerator([
$generator = new ImageGenerator(
// Decide on a target size for your image
'targetSize' => "200x200",
targetSize: "200x200",
// Fun fact: if you set null for these, you'll get a random color for each generated placeholder!
// You can also specify a specific hex color. ("#EEE" or "#EEEEEE" are both accepted)
'textColorHex' => null,
'backgroundColorHex' => "#AFF",
]);
textColorHex: null,
backgroundColorHex: "#AFF"
);

// We'll do a multiline message here
$generator->makePlaceholderImage(
""
);
$generator->makePlaceholderImage("");
16 changes: 8 additions & 8 deletions examples/saved.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@
*/

// Create a new instance of ImageGenerator
$generator = new ImageGenerator([
$generator = new ImageGenerator(
// Decide on a target size for your image
'targetSize' => "200x200",
targetSize: "200x200",
// Fun fact: if you set null for these, you'll get a random color for each generated placeholder!
// You can also specify a specific hex color. ("#EEE" or "#EEEEEE" are both accepted)
'textColorHex' => null,
'backgroundColorHex' => null,
textColorHex: null,
backgroundColorHex: null,
// Let's point to a font. If it can't be found, it'll use a fallback (built-in to GD)
'pathToFont' => __DIR__ . "/Roboto-Black.ttf",
'fontSize' => 20
]);
fontPath: "/System/Library/Fonts/Supplemental/Arial.ttf",
fontSize: 20
);

// We'll do a multiline message here
$generator->makePlaceholderImage(
Expand All @@ -54,4 +54,4 @@
$generator->makePlaceholderImage(
"NV",
__DIR__ . "/image_example_avatar.png"
);
);
14 changes: 8 additions & 6 deletions src/Converters/HexConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class HexConverter
*
* @param $resource
* @param $hex
* @return int
* @return false|int
*/
public static function allocate($resource, $hex)
public static function allocate($resource, $hex): false|int
{
$rgbArray = self::toRgbArray($hex);
return imagecolorallocate($resource, $rgbArray['r'], $rgbArray['g'], $rgbArray['b']);
Expand All @@ -23,27 +23,29 @@ public static function allocate($resource, $hex)
* Converts a hex color string to an array (r: int, g: int, b: int).
* This array can then be used if you need rgb values.
*
* Returns false if the hex cannot be converted (e.g. invalid length).
*
* @param $hex
* @return array|bool
*/
public static function toRgbArray($hex)
public static function toRgbArray($hex): array|bool
{
$hex = preg_replace("/[^abcdef0-9]/i", "", $hex);
if (strlen($hex) == 6) {
list($r, $g, $b) = str_split($hex, 2);
return [
"r" => hexdec($r),
"g" => hexdec($g),
"b" => hexdec($b)
"b" => hexdec($b),
];
} elseif (strlen($hex) == 3) {
list($r, $g, $b) = array($hex[0] . $hex[0], $hex[1] . $hex[1], $hex[2] . $hex[2]);
return [
"r" => hexdec($r),
"g" => hexdec($g),
"b" => hexdec($b)
"b" => hexdec($b),
];
}
return false;
}
}
}
28 changes: 12 additions & 16 deletions src/Helpers/ColorHelper.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
<?php

/**
* @author Nico Verbruggen <[email protected]>
* @copyright 2017 Nico Verbruggen
* @link https://nicoverbruggen.be
*/

namespace NicoVerbruggen\ImageGenerator\Helpers;

class ColorHelper
Expand All @@ -17,7 +11,7 @@ class ColorHelper
*
* @return string
*/
public static function randomHex()
public static function randomHex(): string
{
return '#' . str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT);
}
Expand All @@ -30,20 +24,23 @@ public static function randomHex()
*
* @return string
*/
public static function contrastColor($hex)
public static function contrastColor($hex): string
{
//////////// hexColor RGB
// Remove the # to avoid errors
$hex = str_replace('#', '', $hex);

// hexColor RGB
$R1 = hexdec(substr($hex, 0, 2));
$G1 = hexdec(substr($hex, 2, 2));
$B1 = hexdec(substr($hex, 4, 2));

//////////// Black RGB
$blackColor = "#000000";
// Black RGB
$blackColor = "000000";
$R2BlackColor = hexdec(substr($blackColor, 0, 2));
$G2BlackColor = hexdec(substr($blackColor, 2, 2));
$B2BlackColor = hexdec(substr($blackColor, 4, 2));

//////////// Calc contrast ratio
// Calc contrast ratio
$L1 = 0.2126 * pow($R1 / 255, 2.2) +
0.7152 * pow($G1 / 255, 2.2) +
0.0722 * pow($B1 / 255, 2.2);
Expand All @@ -52,18 +49,17 @@ public static function contrastColor($hex)
0.7152 * pow($G2BlackColor / 255, 2.2) +
0.0722 * pow($B2BlackColor / 255, 2.2);

$contrastRatio = 0;
if ($L1 > $L2) {
$contrastRatio = (int)(($L1 + 0.05) / ($L2 + 0.05));
} else {
$contrastRatio = (int)(($L2 + 0.05) / ($L1 + 0.05));
}

//////////// If contrast is more than 5, return black color
// If contrast is more than 5, return black color
if ($contrastRatio > 5) {
return '#000';
} else { //////////// if not, return white color.
} else { // if not, return white color.
return '#FFF';
}
}
}
}
90 changes: 22 additions & 68 deletions src/ImageGenerator.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?php

/**
* @author Nico Verbruggen
* @copyright 2017 Nico Verbruggen
* @link https://nicoverbruggen.be
*/

namespace NicoVerbruggen\ImageGenerator;

Expand All @@ -13,65 +8,22 @@

class ImageGenerator
{
public function __construct($config = [])
{
// The following properties can be set when this object is constructed.
$allowed = [
"targetSize",
"textColorHex",
"backgroundColorHex",
"pathToFont",
"fontSize",
"fallbackFontSize"
];
foreach ($allowed as $allowedProperty) {
if (array_key_exists($allowedProperty, $config)) {
$this->{$allowedProperty} = $config[$allowedProperty];
}
}
}

/**
* The default target size for generated images.
* @var string
*/
public $targetSize = "200x200";

/**
* The default text color for generated images.
* If set to null, will result in the best contrast color to the random color.
* @var string
*/
public $textColorHex = "#333";

/**
* The default background color for generated images.
* If set to null, will result in a random color.
* @var string
*/
public $backgroundColorHex = "#EEE";

/**
* Path to the font that needs to be used to render the text on the image.
* Must be a TrueType font (.ttf) for this to work.
*
* @var null|string
*/
public $pathToFont = null;

/**
* The font size to be used when a TrueType font is used.
* Also used to calculate line height in case of multiple lines.
* @var int
* @param string $targetSize: The target size for generated images.
* @param string $textColorHex: The default text color for generated images. If set to null, will result in the best contrast color to the random color.
* @param string $backgroundColorHex: The default background color for generated images. If set to null, will generate a random color.
* @param null $fontPath: Path to the font that needs to be used to render the text on the image. Must be a TrueType font (.ttf) for this to work.
* @param int $fontSize: The font size to be used when a TrueType font is used. Also used to calculate the line height.
* @param int $fallbackFontSize: Can be 1, 2, 3, 4, 5 for built-in fonts in latin2 encoding (where higher numbers corresponding to larger fonts).
*/
public $fontSize = 12;

/**
* Can be 1, 2, 3, 4, 5 for built-in fonts in latin2 encoding (where higher numbers corresponding to larger fonts).
*
* @var int
*/
public $fallbackFontSize = 5;
public function __construct(
public $targetSize = "200x200",
public $textColorHex = "#333",
public $backgroundColorHex = "#EEE",
public $fontPath = null,
public $fontSize = 12,
public $fallbackFontSize = 5
) {}

/**
* Render or save a placeholder image. (Will always be a PNG.)
Expand Down Expand Up @@ -118,21 +70,23 @@ public function makePlaceholderImage($text = "", $path = null, $size = null, $bg
$randomColor = ColorHelper::randomHex();

// Determine which background + foreground (text) color needs to be used
$bgColor = !empty($bgHex) ? $bgHex : $randomColor;
$fgColor = !empty($fgHex) ? $fgHex : ColorHelper::contrastColor($bgHex);
$bgColor = ! empty($bgHex) ? $bgHex : $randomColor;
$fgColor = ! empty($fgHex) ? $fgHex : ColorHelper::contrastColor($bgHex);

if ($text == "") {
$text = $targetSize;
}

// Allocate both the background + foreground (text) color
$allocatedBgColor = HexConverter::allocate($imageResource, $bgColor);
// Merely allocating the color is enough for the background
HexConverter::allocate($imageResource, $bgColor);

// We'll need to use the foreground color later, so assign it to a variable
$allocatedFgColor = HexConverter::allocate($imageResource, $fgColor);

if ($this->pathToFont !== null && file_exists($this->pathToFont)) {
if ($this->fontPath !== null && file_exists($this->fontPath)) {
// Use the TrueType font that was referenced.
// Generate text
$font = $this->pathToFont;
$font = $this->fontPath;
$size = $this->fontSize;

// Get Bounding Box Size
Expand Down

0 comments on commit e9e9e5c

Please sign in to comment.