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 resizing through PHP programming.
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 the image type by using the 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 are used to get image properties and to get image file resource data expecting the name or path of the image file. For example,
<?php
$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 values denoting image type. For example, IMAGETYPE_JPEG is defined with value 2 which is used for indicating JPEG images.
After getting the reference resource id from a 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, imagecreatetruecolor() is used for this purpose, by accepting required dimensions, that is, the width and height of the target image. For example,
<?php
$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 a resource data identifier as a reference to the newly created image with specified width and height parameters. This reference will be used in subsequent steps, for mentioning the target, on top of which the resized image will be assembled.
For this step, we should provide a list of details about the source and the target image, that is used in the image resize process. These are,
Using these details provided, the 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,
<?php
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 are passed with a 0 value. These arguments, actually, represent the x, and y coordinates of the 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, and y points, meaning that, the entire image will be cropped to preserve its appearance as it is except for 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.
Finally, it’s time for saving the resized image to the target location. For that, we need to specify the following details.
Now, we can use the code sample shown below to save resized image layer.
<?php
imagejpeg($target_layer, "christmas_thump.jpg");
?>
The code sample shown in each step is applicable only for JPEG images. We can replicate the same for other image types by using appropriate PHP functions.
This example shows how to resize any type of image file uploaded from an HTML form. So, the PHP script 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 the 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>
Excellent tutorial… Nicely explained
Thank you Pavan.
Thanks your are writing very nice
Welcome Devesh.
Transparent PNG Images not resized using this code. Can you help us on this.
Hi Mukesh,
I will check the script and update it.
Excellent script.
Would be nice if it would be possible to maintain aspect ratio while resizing..
Thank you Frin. Sure, I will update the script with the aspect ratio.
thanks
Welcome Sagar.
very nice explained
Thank you Narendra.
Thanks for all scripts including this
Welcome Midhun.
it doesn’t work for me….no image uploaded…why do you think ?
Pamela,
Send me details about the issue you are facing. Do you get any exception?
Simply excellent!!!
Thank you Yomi.
hi;
imagecopyresampled() is used for croping that image? or resize the image without cutting in same ratio?
It copies and resizes with resampling.
thank you for the post! Very helpfull!!
Welcome Renato.
Very helpful and nice tutorial
Thank you Martin.
Thanks.. much better than other code
Welcome Sajeel.