PHP Scripts and Resources for Webmasters

PHP Resize Image

This tutorial will demonstrate how to resize images with PHP. The ability to resize images is useful for creating thumbnails for image galleries. Resizing an image involves calculating the target dimensions, loading the existing source image, creating a new image for the output, copying the resized source image into the new image, and saving the new image file.

Determining the Image Size

Before the image can be resized, you need to determine what dimensions the resized image should be. Usually, you'll want to maintain the aspect ratio, or proportion of the height and width, of the original image. The following example demonstrates how to find the dimensions of the original image, and calculate new dimensions based on a specified maximum height and width. It uses the PHP function getimagesize() to get the width and height of the image. Robust error handling has been omitted for clarity.

function get_image_sizes($sourceImageFilePath, 
  $maxResizeWidth, $maxResizeHeight) {
  // Get width and height of original image
  $size = getimagesize($sourceImageFilePath);
  if($size === FALSE) return FALSE; // Error
  $origWidth = $size[0];
  $origHeight = $size[1];
  // Change dimensions to fit maximum width and height
  $resizedWidth = $origWidth;
  $resizedHeight = $origHeight;
  if($resizedWidth > $maxResizeWidth) {
    $aspectRatio = $maxResizeWidth / $resizedWidth;
    $resizedWidth = round($aspectRatio * $resizedWidth);
    $resizedHeight = round($aspectRatio * $resizedHeight);
  }
  if($resizedHeight > $maxResizeHeight) {
    $aspectRatio = $maxResizeHeight / $resizedHeight;
    $resizedWidth = round($aspectRatio * $resizedWidth);
    $resizedHeight = round($aspectRatio * $resizedHeight);
  }
  // Return an array with the original and resized dimensions
  return array($origWidth, $origHeight, $resizedWidth, 
    $resizedHeight);
}

Creating the Resized Image

Once you have the target dimensions, you can create the new image; the example below demonstrates how. The imagecreatetruecolor() function creates a new image and returns a handle to it.

// Get dimensions
$sizes = get_image_sizes($sourceImageFilePath, $maxResizeWidth, 
  $maxResizeHeight);
$origWidth = $sizes[0];
$origHeight = $sizes[1];
$resizedWidth = $sizes[2];
$resizedHeight = $sizes[3];
// Create the resized image 
$imageOutput = imagecreatetruecolor($resizedWidth, 
    $resizedHeight);
if($imageOutput === FALSE) return FALSE; // Error condition

Next, we must load the source image. For the purposes of this example, we will assume that the source image is a JPEG file, however, PHP provides functions to support many other graphic formats such as PNG and GIF.

// Load the source image
$imageSource = imagecreatefromjpeg($sourceImageFilePath);
if($imageSource === FALSE) return FALSE; // Error condition

Now, we can copy the resized contents of the source image into the destination image using the imagecopyresampled() function.

$result = imagecopyresampled($imageOutput, $imageSource, 
    0, 0, 0, 0, $resizedWidth, $resizedHeight, $origWidth, 
    $origHeight);
if($result === FALSE) return false; // Error condition

Lastly, we will write the resized image out to a file. Again, we will assume the JPEG format for the purposes of this example.

// Write out the JPEG file with the highest quality value
$result = imagejpeg($imageOutput, $outputPath, 100);
if($result === FALSE) return false; // Error condition

For more details about image processing in PHP, check out the complete PHP Image Function reference.

Back to Tutorials