PHP Image Resize

While displaying images for our websites, it is important to ensure that it could be set within our layout boundary. If not, we need to resize these images, accordingly.  In PHP, resizing image, it can be achieved by using set of PHP functions to create new image from scratch, with the given dimensions, in which the original image is expected to be resized.

So, combination of such functions are used to obtain the following steps, which will result successful image resize through PHP programming.

  • Get image resource id for source image.
  • Get resource id for target image layer.
  • Resizing and reassembling.
  • Save resized image into given target location.

php_image_resize

Get Image Resource Id for Source Image

For working on the given image file to be resized, we need to get the resource identifier for reference, as we have done while working with files after getting file resource, or like, getting directory handle to perform PHP directory functions.

In PHP, there are various functions to get image file resource id. These functions are used appropriately based on the type of image given for resizing. For example, imagecreatefromjpeg(), imagecreatefromgif(), imagecreatefrompng(), used to get the resource identifier for JPEG, GIF and PNG images.

In this step, first we need to get image type by using PHP function getimagesize(), which is used for getting entire list of image properties, including width, height and etc. After that, we can apply suitable function to get the resource id. All these PHP functions used to get image properties and to get image file resource data, expects name or path of the image file. For example,

$file = "christmas.jpg"; 
$source_properties = getimagesize($file);
$image_type = $source_properties[2]; 
if( $image_type == IMAGETYPE_JPEG ) {   
$image_resource_id = imagecreatefromjpeg($file);  
}
elseif( $image_type == IMAGETYPE_GIF )  {  
$image_resource_id = imagecreatefromgif($file);  }
elseif( $image_type == IMAGETYPE_PNG ) {
$image_resource_id = imagecreatefrompng($file); }
 

The constants used in conditional statements are predefined with appropriate integer value denotes image type. For example, IMAGETYPE_JPEG defined with value 2 which is used for indicating JPEG image.

Get Resource Id for Target Image Layer

After getting reference resource id from source image file, we need to create new image as a target layer. This image will be created with the dimensions to what the original image is expected to be resized.

PHP built-in function, named as, imagecreatetruecolor() is used for this purpose, by accepting required dimensions, that is, the width and height of the target image. For example,

$target_width =200;
$target_height =200;
$target_layer=imagecreatetruecolor($target_width,$target_height);

imagecreatetruecolor() function will create empty image. Added to that, it will return resource data identifier as a reference to the newly created image with specified width and height parameter. This reference will be used in subsequent steps, for mentioning target, on top of which the resized image will be assembled.

Resizing and Reassembling

For this step, we should provide list of details about the source and target image, that are used in image resize process.  These are,

  • Source and target layer resource id
  • Dimensions for denoting width and height of original image and target image layer.

Using these details provided, required portion of the original image will be copied and reassembled onto the target layer. For that, PHP function, named as, imagecopyresampled() for such resizing and reassembling process. For example,

imagecopyresampled($target_layer,$image_resource_id,0,0,0,0,$target_width,$target_height, $source_properties[0],$source_properties[1]);

In this code sample, shown above, some of the arguments of this function is passed with 0 value. These arguments, actually, represents the x, y co-ordinates of target and source image, respectively.

These arguments will contain values for cropping some portion of the source image. Otherwise, there is no need to mention x,y points, meaning that, the entire image will be cropped to preserve its appearance as it is except its dimensions.

Note: There is an equivalent PHP function imagecopyresized() as like as imagecopyresampled(), whereas, the imagecopyresampled() function creates resized image with more quality, comparatively.

Save Resized Image into Target Location

Finally, it’s time for saving resized image to the target location. For that, we need to specify the following details.

  • Resource id of the resized image layer.
  • Target image name or location.

Now, we can use the code sample shown below to save resized image layer.

imagejpeg($target_layer,"christmas_thump.jpg");

Code sample shown in each step is applicable only for JPEG image. We can replicate the same for other image types by using appropriate PHP functions.

Example: PHP Image Resize

This example shows how to resize any type of image file uploaded from a HTML form. So, the PHP script is shown below handles the uploaded image file to be resized.

<?php
if(isset($_POST["submit"])) {
if(is_array($_FILES)) {
$file = $_FILES['myImage']['tmp_name']; 
$source_properties = getimagesize($file);
$image_type = $source_properties[2]; 
if( $image_type == IMAGETYPE_JPEG ) {   
$image_resource_id = imagecreatefromjpeg($file);  
$target_layer = fn_resize($image_resource_id,$source_properties[0],$source_properties[1]);
imagejpeg($target_layer,$_FILES['myImage']['name'] . "_thump.jpg");
}
elseif( $image_type == IMAGETYPE_GIF )  {  
$image_resource_id = imagecreatefromgif($file);
$target_layer = fn_resize($image_resource_id,$source_properties[0],$source_properties[1]);
imagegif($target_layer,$_FILES['myImage']['name'] . "_thump.gif");
}
elseif( $image_type == IMAGETYPE_PNG ) {
$image_resource_id = imagecreatefrompng($file); 
$target_layer = fn_resize($image_resource_id,$source_properties[0],$source_properties[1]);
imagepng($target_layer,$_FILES['myImage']['name'] . "_thump.png");
}
}
}
function fn_resize($image_resource_id,$width,$height) {
$target_width =200;
$target_height =200;
$target_layer=imagecreatetruecolor($target_width,$target_height);
imagecopyresampled($target_layer,$image_resource_id,0,0,0,0,$target_width,$target_height, $width,$height);
return $target_layer;
}
?>

And the HTML code which includes the form container to upload image file is,

<form name="frmImageResize" action="" method="post" enctype="multipart/form-data">
<input type="file" name="myImage" /> 
<input type="submit" name="submit" value="Submit" />
</form>

This PHP code tutorial was published on July 19, 2013.

↑ Back to Top