Motyar

@motyar

Freelance Web Developer

Static Web Hosting, made easy

Sep 20, 2010

Create Image Placeholder with Php ( Placehold.it Clone Script )

Placehold.it is a quick and simple image placeholder service. Here in this post you will learn how it works Or how to achieve the same functionality with php.

Idea

If you are Designing a website and Need a placeholder graphics, you simply have to put the image size after url like - http://placehold.it/350x150. This simple functionality is very useful to control your photo places.
Placehold.it just create a image according to color and size given in url. We can do this with Php using its GD library.


Code

PHP(index.php)

if(isset($_GET)){
    $imagedata explode('-',$_GET['data']); 
    if (!is_array($imagedata) || count($imagedata) != 4){
      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 create_image($width$height$bg_color$txt_color )

{

    $text "$width X $height";

    //Create the image resource 
    $image ImageCreate($width$height);
    //Making of colors, we are changing HEX to RGB
    $bg_color ImageColorAllocate($image
                base_convert(substr($bg_color02), 1610), 
                base_convert(substr($bg_color22), 1610), 
                base_convert(substr($bg_color42), 1610));


    $txt_color ImageColorAllocate($image, 
                base_convert(substr($txt_color02), 1610), 
                base_convert(substr($txt_color22), 1610), 
                base_convert(substr($txt_color42), 1610));

    //Fill the background color 
    ImageFill($image00$bg_color); 
    //Calculating font size   
    $fontsize = ($width>$height)? ($height 10) : ($width 10) ;
    
    //Inserting Text    
     imagettftext($image,$fontsize0
                    ($width/2) - ($fontsize 2.75), 
                    ($height/2) + ($fontsize0.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


Download

You can download it from here.

Labels: ,




By :