Superior Web Solutions 905 532 9642Free Project Estimator
HOME CLIENTS SERVICES BLOG ABOUT US CONTACT US
Categories:
 Internet Marketing
 iPhone iPad Apps Development
 IT Humour
 IT Industry News
 Search Engines
 User Guides
 Web Development
Browse by tags
Search Blog:
RSS
Subscribe to our newsletter
Name*:
E-Mail*:
Recent Posts
Secure Web Development: SSL Certificate Is Installed, but Browser Displays Warning
PHP, allow visitor to download image that is not on your server
New Home For Web Development
Mobile Websites Development versus Mobile Apps development
Most Common Screen Resolutions in 2011
Make PHP CAPTCHA more secure
March 27, 2009 - How to resize an image with PHP

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);
?>

Associated tags:  PHP, Code
Comments:
Richard Lee wrote on April 2, 2009 at 11:07
Great php script regarding image resizing. It worked out well for my small site. I was wondering if you going to make one for .NET?
Michael Pankratov wrote on April 2, 2009 at 11:49
Thank you! We will post .NET script soon. Once it is up we will send you an email with the link.
Michael Pankratov wrote on April 3, 2009 at 09:44
As promised .NET script is ready: How to resize an image with .NET (C#)
Add Comment:
Name*:
E-Mail: Website:
Your message*:
»
© 2000 - 2012 Superior Web Solutions