Quick Contact
Name: Comments:
Email:
Phone: What does 4 + 3 equal ?:

Support : Code Archive : PHP Image Resize Function

PHP Image Resize Script

When developing dynamic web applications with PHP it is commonly necessary to allow users to add images directly to a web application. With varied skill level it is inevitable that users will submit images ranging greatly in size and shape.  To minimize the download time as well as the storage space required on the web server it becomes necessary to physically resize these images large digital images prior to storing them on the server.

About GD & PHP Image Resizing

PHP has a fantastic library of functions that can be used for this very type of activity.  This library is known as the GD library (http://www.boutell.com/gd/).  A list of GD Library functions available in PHP is available at the following URL: http://www.php.net/gd.  (Note: GD must be enabled in your PHP install to utilize the any of the GD functions of the PHP function we have included below)

About the Function

Below we have included a free easy to use PHP function designed to easily resize and store images and store resampled images. It can be included in your PHP based website and called by your script to resize JPG, GIF, and PNG images.  The script is designed to maintain the aspect ratio of the image that is being resized.  It calculates the longest side of a given image then resizes that side to the input width while maintaining the aspect ratio of the image.  The resulting image will have a maximum width or height of the input value.

The code for the function is available below:

<?php
// Image Resize Script by Zipline Interactive
// http://www.gozipline.com
// http://www.gozipline.com/42,phpimageresizefunction
// info@gozipline.com
// 02/02/2008

// This will resize an image keeping its height/width aspect ratio if you do not specify a height value.

// USAGE:

    // MAINTAIN ASPECT AND SAVE FILE.
    // This will create a new files called 'resizedImage.jpg' with a longest side resized to 600px.
//    resizeImage('../uploads/uploadedImage.jpg','resizedImage.jpg','600'); 
    
    // MATIAN ASPECT AND SHOW THE IMAGE IN THE BROWSER.
    // No other output can be shown before this image.
//    resizeImage('../images/uploadedImage.jpg','','450');
    
    // RESHAPE IMAGE WITH SET SIZES
    // This will reshape the image to the set dimensions and save as a jpg file.
//    resizeImage('uploadedImage.jpg','../images/resized/resizedImage.jpg','150','150');
    
    
function resizeImage($source$destination NULL,$wdt$height NULL){
    if(empty(
$height)){
        
// Height is nit set so we are keeping the same aspect ratio.
        
list($width$height) = getimagesize($source);
        if(
$width $height){
            
$w $wdt;
            
$h = ($height $width) * $w;
            
$w $w;
        }else{
            
$w $wdt;
            
$h $w;
            
$w = ($width $height) * $w;
        }
    }else{
        
// Both width and Height are set.
        // this will reshape to the new sizes.
        
$w $wdt;
        
$h $height;
    }
    
$source_image = @file_get_contents($source) or die('Could not open'.$source);
    
$source_image = @imagecreatefromstring($source_image) or die($source.' is not a valid image');
    
$sw imagesx($source_image);
    
$sh imagesy($source_image);
    
$ar $sw/$sh;
    
$tar $w/$h;
    if(
$ar >= $tar){
        
$x1 round(($sw - ($sw * ($tar/$ar)))/2);
        
$x2 round($sw * ($tar/$ar));
        
$y1 0;
        
$y2 $sh;
    }else{
        
$x1 0;
        
$y1 0;
        
$x2 $sw;
        
$y2 round($sw/$tar);
    }
    
$slate = @imagecreatetruecolor($w$h) or die('Invalid thumbnail dimmensions');
    
imagecopyresampled($slate$source_image00$x1$y1$w$h$x2$y2);
    
// If $destination is not set this will output the raw image to the browser and not save the file
    
if(!$destinationheader('Content-type: image/jpeg');
    @
imagejpeg($slate$destination75) or die('Directory permission problem');
    
ImageDestroy($slate);
    
ImageDestroy($source_image);
    if(!
$destination) exit;
    return 
true;
}
?>

Comments:


Ryan posted:
If you supply a destination value it will save a seperate image.

Bobby posted:
Does this script destroy the original image? If so, how can this be prevented?

© 2005-2010 Zipline Interactive Inc. Spokane Washington (WA)

154 S. Madison Suite 201 Spokane WA 99201 | Phone: 509-321-2849 | Zipline Web Design Blog

Toll Free