Sometimes we need to resize images on our website. For example, the size of the image is more than what we need. If your website is created with PHP, the resize image method will be helpful and can be done in two ways in PHP. One uses the Imagick class and the other with PHP’s own functions.
Many hosts do not have Imagick installed, so we have to use PHP’s own functions. In this article, we will teach you how to resize the image, so stay tuned.
Why do we need Image resizing?
All websites need high SEO in order to be displayed in search engines. One of the things that are important in increasing the SERP of a page is its size. Now suppose that the page has images with high size and volume. what will happen? The page loading speed will slow down and you will lose your users even if you have written the best content on the internet.
Therefore, all pages need to have a suitable size. One of the important things that increase the size of pages is images. In this tutorial, functions will be introduced that you can use to reduce the size of your images. We have also prepared an article for you on this website titled How to Compress Image Without Losing Visible Quality.
How to resize an image in PHP?
It is possible to change the image size in PHP using the following examples.
Resize JPEG format image
Resize JPEG image.
function resize_image_jpeg($source_file,$destination_file, $width, $height, $quality, $crop=FALSE) {
list($current_width, $current_height) = getimagesize($source_file);
$rate = $current_width / $current_height;
if ($crop) {
if ($current_width > $current_height) {
$current_width = ceil($current_width-($current_width*abs($rate-$width/$height)));
} else {
$current_height = ceil($current_height-($current_height*abs($rate-$width/$height)));
}
$newwidth = $width;
$newheight = $height;
} else {
if ($width/$height > $rate) {
$newwidth = $height*$rate;
$newheight = $height;
} else {
$newheight = $width/$rate;
$newwidth = $width;
}
}
$src_file = imagecreatefromjpeg($source_file);
$dst_file = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst_file, $src_file, 0, 0, 0, 0, $newwidth, $newheight, $current_width, $current_height);
imagejpeg($dst_file, $destination_file, $quality);
}
How to use the function
resize_image_jpeg('image.jpg','dst_image.jpg','100','100',75,false);
In this example, we have two ways to change the image size, one with crop and the other without crop. In crop, it doesn’t matter how wide the image is, its width will be the same as entered in the function parameter, which is 100 in this example. If the width of the image after resizing is greater than 100, the remaining width will be cropped.
In the method without a crop, if the height is greater than the width, the height will be considered the basis, and if the width is greater than the height, the width will be considered the basis, and the image will be resized based on the basis.
Resize PNG format image
PNG images are slightly different from JPEG format due to the possibility of being transparent. The way to determine the width and height is the same as the previous function and the only difference is the way to create and save the image.
function resize_image_png($source_file,$destination_file, $width, $height, $quality, $crop=FALSE) {
list($current_width, $current_height) = getimagesize($source_file);
$rate = $current_width / $current_height;
if ($crop) {
if ($current_width > $current_height) {
$current_width = ceil($current_width-($current_width*abs($rate-$width/$height)));
} else {
$current_height = ceil($current_height-($current_height*abs($rate-$width/$height)));
}
$newwidth = $width;
$newheight = $height;
} else {
if ($width/$height > $rate) {
$newwidth = $height*$rate;
$newheight = $height;
} else {
$newheight = $width/$rate;
$newwidth = $width;
}
}
$src_file = imagecreatefrompng($source_file);
imagepalettetotruecolor($src_file);
imagealphablending($src_file, true);
imagesavealpha($src_file, true);
$dst_file = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst_file, $src_file, 0, 0, 0, 0, $newwidth, $newheight, $current_width, $current_height);
imagepng($dst_file, $destination_file, $quality);
}
How to use the function
resize_image_png('image.png','dst_image.png','100','100',7,false);
The quality parameter must be between 0 to 9.
Resize WebP format image
The WebP format is one of the image formats designed by Google and, like SVG, is known as Next-Gen Image. The size of WebP images is lower than JPEG and PNG because this format uses text to store images.
In the following function, you will do how to resize WebP images.
function resize_image_webp($source_file,$destination_file, $width, $height, $quality, $crop=FALSE) {
list($current_width, $current_height) = getimagesize($source_file);
$rate = $current_width / $current_height;
if ($crop) {
if ($current_width > $current_height) {
$current_width = ceil($current_width-($current_width*abs($rate-$width/$height)));
} else {
$current_height = ceil($current_height-($current_height*abs($rate-$width/$height)));
}
$newwidth = $width;
$newheight = $height;
} else {
if ($width/$height > $rate) {
$newwidth = $height*$rate;
$newheight = $height;
} else {
$newheight = $width/$rate;
$newwidth = $width;
}
}
$src_file = imagecreatefromwebp($source_file);
$dst_file = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst_file, $src_file, 0, 0, 0, 0, $newwidth, $newheight, $current_width, $current_height);
imagewebp($dst_file, $destination_file, $quality);
}
How to use the function
resize_image_webp('image.webp','dst_image.webp','100','100',75,false);
Conclusion
In the examples above, we introduced functions that you can use to resize the image. These functions are built-in PHP functions that can be used in most PHP versions today.
You can even develop a plugin for WordPress using these functions and provide it to users or even sell it. On this website, we have provided an article titled How To Create A Custom WordPress Plugin for you.