How to resize an image with PHP

March 27, 2009

In this article you will find code that will allow you to resize an image on the fly. Please note that if you are working on the site with high traffic it is not recommended to use this technique to resize the images, because every time user opens the page, it will create resized image and destroy it after displaying. For high traffic sites, it is recommended to save resized images. We will provide code sample for resizing and saving in our next posts.

Script overview

Script output:

 

Original image: here

How to call script:

<img src="http://www.superiorwebsys.com/dat/news/660.php?image_path=/img/bannerBlog.png&w=300" />


Note: only width is passed to the script, in this case height will be adjusted automaticly

To download this script click here. There 2 files in this rar:
imageResize.php - PHP script
LHANDW.TTF - font for displaying No Image

Code

<?
/*
Superior Web Systems

Parameters:
h - needed height of the image in pixels
w - needed width of the image in pixels
image_path - path to the image on the server
*/
$h=(!empty($_REQUEST["h"]))?$_REQUEST["h"]:80; // 80 - default value if nothing is passed, can be changed
$w=(!empty($_REQUEST["w"]))?$_REQUEST["w"]:150; // 150 - default value if nothing is passed, can be changed
$image_path=(!empty($_REQUEST["image_path"]))?$_REQUEST["image_path"]:"";
$image_path=$_SERVER['DOCUMENT_ROOT'].$image_path; //Create full path to the image, might not work on all servers
//Check if image exist.
if(is_file($image_path))
{
    
$fileExtensiontmp=end(explode(".", $image_path));
    
$arrImgSize=getimagesize($image_path);
    
$widthF = $arrImgSize[0]; // get original source image width
    
$heightF = $arrImgSize[1]; // get original source imageand height
    //Calculate widht and height for proportional resize
    
if($widthF<$w && $heightF<$h)
    {
        
$neededWidth=$widthF;
        
$neededHeight=$heightF;
    }
    else
    {    
        if(
$widthF>=$w){
            
$neededWidth=$w;
            if((
$heightF*$w/$widthF)<=$h)$neededHeight=(int)$heightF*$w/$widthF;
            else{
$neededHeight=$h; $neededWidth=(int)$widthF*$h/$heightF;}
            }
        elseif(
$heightF>=$h){$neededHeight=$h; $neededWidth=(int)$widthF*$h/$heightF;}
        
    }
        
    switch(
$fileExtensiontmp){
        case
"jpeg":
        case
"jpg":
            
$im=imagecreatefromjpeg($image_path);
            break;
        case
"gif":
            
$im=imagecreatefromgif($image_path);
            break;
        case
"png":
            
$im=imagecreatefrompng($image_path);
            break;                        
        }
        
$img1 = imagecreatetruecolor( $neededWidth, $neededHeight );
        
imagecopyresampled($img1, $im, 0, 0, 0, 0, $neededWidth, $neededHeight, $widthF, $heightF);
}
else
{
    
// If image does not exist display image with text  "no image"
    
$x = 1;
    
$y = 1;
    if(
$w!=''){
        
$new_width=$w; $new_height=$new_width*$y/$x;
        if(
$h!='')
            { if (
$new_height>$h)
                {    
$new_height=$h;
                    
$new_width=$new_height*$x/$y; }
            }
    }
    elseif(
$h!='')
    {
        
$new_height=$h;
        
$new_width=$new_height*$x/$y;
    }
            
    
$img1 = imagecreate($new_width, $new_height);        
    
// Create some colors
    
$white = imagecolorallocate($img1, 255, 255, 255);
    
$grey = imagecolorallocate($img1, 128, 128, 128);
    
$grey = imagecolorallocate($img1, 160, 160, 160);
    
$black = imagecolorallocate($img1, 0, 0, 0);        


    
// The text to draw
    
$text = '   no
image'
;
    
// Replace path by your own font path
    
$font = 'LHANDW.TTF';            
    
// Add some shadow to the text
    
imagettftext($img1, 12, 0, 11, 21, $grey, $font, $text);        
    
// Add the text
    
imagettftext($img1, 10, 0, 10, 20, $black, $font, $text);    
        
    
}
imagejpeg($img1, '', 90);
imagedestroy($img1);
imagedestroy($im);
?>