Obtener CódigoDemo

Básicamente, hoy aprenderemos a crear un script para generar un placeholder de imágenes con PHP y su librería GD. De éste modo, obtendremos un clon del script placehold.it.

Para qué nos puede servir ésta script de placeholder de imágenes?

Para muchas cosas, en especial, para cuando estemos desarrollando un sitio web y necesitemos imágenes para ir dándonos una guía de cómo es que quedará la maqueta final.

Haremos uso de 3 archivos, uno que se debe llamar index.php, otro que será un htacces (se debe llamar .htaccess) y por último la fuente que se usará.

index.php:

if(isset($_GET)){
$imagedata = explode('-',$_GET['id']); 
if (!is_array($imagedata) || count($imagedata) != 4) //If something goes wrong
        {
            die("Something wrong there!! It should be like - placeholder/350-150-CCCCCC-969696");
        }
create_image($imagedata[0], $imagedata[1], $imagedata[2], $imagedata[3]);
exit;

}

//Function that has all the magic
function create_image($width, $height, $bg_color, $txt_color )
{
    //Define the text to show
    $text = "$width X $height";

    //Create the image resource 
    $image = ImageCreate($width, $height);  

    //We are making two colors one for BackGround and one for ForGround
$bg_color = ImageColorAllocate($image, base_convert(substr($bg_color, 0, 2), 16, 10), 
   base_convert(substr($bg_color, 2, 2), 16, 10), 
   base_convert(substr($bg_color, 4, 2), 16, 10));

$txt_color = ImageColorAllocate($image,base_convert(substr($txt_color, 0, 2), 16, 10), 
   base_convert(substr($txt_color, 2, 2), 16, 10), 
   base_convert(substr($txt_color, 4, 2), 16, 10));

    //Fill the background color 
    ImageFill($image, 0, 0, $bg_color); 
    
//Calculating (Actually astimationg :) ) font size
$fontsize = ($width>$height)? ($height / 10) : ($width / 10) ;
    
//Write the text .. with some alignment astimations
imagettftext($image,$fontsize, 0, ($width/2) - ($fontsize * 2.75), ($height/2) + ($fontsize* 0.2), $txt_color, 'Crysta.ttf', $text);
 
    //Tell the browser what kind of file is come in 
    header("Content-Type: image/png"); 

    //Output the newly created image in png format 
    imagepng($image);
   
    //Free up resources
    ImageDestroy($image);
}

 .htaccess:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?data=$1

El modo de uso, es el siguiente: http://web.com/350-150-CCCCCC-969696 en dónde:

350 es el ancho.
150 es el alto.
CCCCCC es el color de fondo.
969696 es el color de la fuente.

Vía | Descargar código

Obtener CódigoDemo

Williams Azabache Sarmiento

Que puedo decir...