image resize in php example with code


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 an image, it can be achieved by using a set of PHP functions to create a new image from scratch, with the given dimensions, in which the original image is expected to be resized.

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

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

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 an 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 an entire list of image properties, including width, height and etc. After that, we can apply a 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,

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 a 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,

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 a list of details about the source and the target image, that are used in image resize process.  These are,

  • Source and target layer resource id
  • Dimensions for denoting width and height of the 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,

In this code sample, shown above, some of the arguments of this function are passed with 0 value. These arguments, actually, represents the x, y coordinates 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 the 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.

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 an HTML form. So, the PHP script is shown below handles the uploaded image file to be resized.

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


Leave A Reply

Your email address will not be published.