The Netlobo logo - a Nevada desert landscape

Resizing an Image Using PHP and the GD2 Library

A PHP function that will resize your jpg images to any size you choose.

Published Aug 19, 2005 by lobo235
Last updated on Jul 3, 2007

Many people ask me about resizing images with PHP so I thought I would share a few of my ideas on the subject. As an example, I will present the function that I typically use to resize images that has been tried and tested on many of my web sites. Using PHP to resize images opens up new doors and makes PHP even more useful than it already is. You can create a photo gallery, have users upload a picture for their user profile, and much more.

There are some things you will want to keep in mind when use PHP to resize your images. Resizing images in PHP takes CPU time so it's not something that you want to do frequently. Sometimes people will ask me how to create thumbnail images on-the-fly without having to save them to disk every time. This is a bad idea because you will put unnecessary load on your web server and depending on how many visitors you have, your server could crash. It is better to resize the image to thumbnail size and save the thumbnails for later use even though it might take up a little more disk space on your server.

Here is the function that I use to resize jpg images. It uses the GD2 library, and as you'll see, it preserves the colors in true color images.

<?php
function resampimagejpg( $forcedwidth, $forcedheight, $sourcefile, $destfile )
{

    
$fw = $forcedwidth;
    
$fh = $forcedheight;
    
$is = getimagesize( $sourcefile );
    if(
$is[0] >= $is[1] )
    {
        
$orientation = 0;
    }
    else
    {
        
$orientation = 1;
        
$fw = $forcedheight;
        
$fh = $forcedwidth;
    }
    if (
$is[0] > $fw || $is[1] > $fh )
    {
        if( (
$is[0] - $fw ) >= ( $is[1] - $fh ) )
        {
            
$iw = $fw;
            
$ih = ( $fw / $is[0] ) * $is[1];
        }
        else
        {
            
$ih = $fh;
            
$iw = ( $ih / $is[1] ) * $is[0];
        }
        
$t = 1;
    }
    else
    {
        
$iw = $is[0];
        
$ih = $is[1];
        
$t = 2;
    }
    if (
$t == 1 )
    {
        
$img_src = imagecreatefromjpeg( $sourcefile );
        
$img_dst = imagecreatetruecolor( $iw, $ih );
        
imagecopyresampled( $img_dst, $img_src, 0, 0, 0, 0, $iw, $ih, $is[0], $is[1] );
        if( !
imagejpeg( $img_dst, $destfile, 90 ) )
        {
            exit( );
        }
    }
    else if (
$t == 2 )
    {
        
copy( $sourcefile, $destfile );
    }
}

?>

To use the resize function you must pass in the forced width, forced height, source image, and destination image. The function then uses the GD2 library functions to read the source image's size. It will then calculate the new image's size based off of the forced width and height. This function will maintain the original image's aspect ratio so your image won't look distorted. It won't change the source image at all but when the function is done running you will end up with the destination image which is resized to the dimensions you passed into the function. As far as I know, this function requires the GD2 library although you might be able to get it to work with the GD1 library. The GD library extension must be enabled in PHP before you can use GD2 functions so keep that in mind as well. You can use the PHP function phpinfo() to see if GD2 is enabled or to see what version of the GD library your server is using.

Update - March 22nd, 2007

When I originally wrote this article there were not many digital cameras or other devices capable or creating images larger than about 6 megapixels. Now that 7,8, and 10 megapixel cameras are more commonplace, some people who use this resizing function have run into a problem with PHP running out of memory. The problem is that the user's web host has set the PHP memory limit to a low value so that the server's resources can be shared somewhat equally between all the users on the server. Larger files require more memory to resize so when the resizing script hits the memory limit it chokes and gives an internal server error.

Possible solutions to the problem:

  • Contact your host and ask them to increase the PHP memory limit so your script can resize the larger files.
  • Use a different program to resize the images such as ImageMagick if your host provides it. If your host does not provide it you may be able to install it yourself if you have shell access available on your hosting account.
  • Don't allow users to upload such large files. It consumes a lot of bandwidth on your server and can be very slow for the user depending on the speed of their internet connection. Have them resize the image before uploading it so that it is under 1MB or so.

I hope this PHP function will allow you to resize images easily. It has worked very well for me.

5 comments for this article.

del.icio.us logo add this article to del.icio.us!
Other great Web Development and Programming articles on Netlobo.com:
Javascript Tool-tips
Making AJAX Easier
Preventing MySQL Injection attacks with PHP