Skip to content

Instantly share code, notes, and snippets.

@mcunha98
Created January 14, 2021 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcunha98/da842ab54d64b45c2adfc896eccac691 to your computer and use it in GitHub Desktop.
Save mcunha98/da842ab54d64b45c2adfc896eccac691 to your computer and use it in GitHub Desktop.
//Examples :
placeholder.php?size=200x100&bg=eee&fg=000&text=Hello
placeholder.php?size=200x100&bg=eee&fg=000
placeholder.php?size=200x100
<?php
$getsize = isset($_GET['size']) ? $_GET['size'] : '100x100';
$dimensions = explode('x', $getsize);
$image = imagecreate($dimensions[0], $dimensions[1]);
$bg = isset($_GET['bg']) ? $_GET['bg'] : 'ccc';
$bg = hex2rgb($bg);
$setbg = imagecolorallocate($image, $bg['r'], $bg['g'], $bg['b']);
$fg = isset($_GET['fg']) ? $_GET['fg'] : '555';
$fg = hex2rgb($fg);
$setfg= imagecolorallocate($image, $fg['r'], $fg['g'], $fg['b']);
$text = isset($_GET['text']) ? strip_tags($_GET['text']) : $getsize;
$text = str_replace('+', ' ', $text);
$fontsize = 4;
$fontwidth = imagefontwidth($fontsize);
$fontheight = imagefontheight($fontsize);
$length = strlen($text);
$textwidth = $length * $fontwidth;
$xpos = (imagesx($image) - $textwidth) / 2;
$ypos = (imagesy($image) - $fontheight) / 2;
imagestring($image, $fontsize, $xpos, $ypos, $text, $setfg);
header ("Content-type: image/png");
imagepng($image);
function hex2rgb($colour)
{
$colour = preg_replace("/[^abcdef0-9]/i", "", $colour);
if (strlen($colour) == 6)
{
list($r, $g, $b) = str_split($colour, 2);
return Array("r" => hexdec($r), "g" => hexdec($g), "b" => hexdec($b));
}
elseif (strlen($colour) == 3)
{
list($r, $g, $b) = array($colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2]);
return Array("r" => hexdec($r), "g" => hexdec($g), "b" => hexdec($b));
}
return false;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment