Skip to content

Instantly share code, notes, and snippets.

@uhtred
Created March 20, 2013 21:12
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save uhtred/5208478 to your computer and use it in GitHub Desktop.
Save uhtred/5208478 to your computer and use it in GitHub Desktop.
PHP: Placehold it image
<?php
/*
How to use
/940x278
/940x278/000000
/940x278&text=Texto
/940x278/000000&text=Texto
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [L]
*/
if( empty($_GET) ) {
$_GET['params'] ='100x100/CCCCCC';
}
$fontSize = 5;
$params = array_filter( explode( '/', $_GET['params'] ) );
$dimensions = explode( 'x', $params[0] );
$w = isset($dimensions[0]) ? $dimensions[0] : 100;
$h = isset($dimensions[1]) ? $dimensions[1] : 100;
$bg = isset($params[1]) ? $params[1] : 'CCCCCC';
$text = isset($_GET['text']) ? $_GET['text'] : $w.'x'.$h;
if( $w < 50 ) {
$fontSize = 1;
}
$im = imagecreatetruecolor($w, $h);
imagefilledrectangle($im, 0, 0, $w, $h, '0x'.$bg);
$fontWidth = imagefontwidth($fontSize);
$textWidth = $fontWidth * strlen($text);
$textLeft = ceil( ($w-$textWidth)/2 );
$fontHeight = imagefontheight($fontSize);
$textHeight = $fontHeight;
$textTop = ceil( ($h-$textHeight)/2 );
imagestring($im, $fontSize, $textLeft, $textTop, $text, 0x969696);
header('Content-Type: image/gif');
imagegif($im);
imagedestroy($im);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment