PHP Resize Image (WebP, JPG, PNG)

PHP Resize Image (WebP, JPG, PNG)

Resizing images is a common task in web development, especially when dealing with user-uploaded content or creating responsive designs. Sometimes we need to resize an image 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 (JPEG, PNG, and WebP) 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. In the process of resizing image size in PHP, we can control the quality of the final image to resize without losing quality or decreasing the quality.

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 the 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, PHP functions will be introduced to resize that you can use to reduce the size of your image. 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(resize) the image size in PHP using the following examples. The following examples use the PHP built-in functions to resize an image with WebP, JPG, and PNG format.

Resize JPEG format image

Resize the JPEG image with a user-defined function.

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.

You can set the quality to 100 to resize and crop the image without losing the quality in the PHP imagejpeg function.

Resize PNG format image

PNG images are slightly different from JPEG format due to the possibility of being transparent. To resize the PNG image in PHP, we have to deal with transparency. 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. You can set the quality to 9 to resize and crop the PNG image without losing the quality in the PHP imagepng() function.

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 learn how to resize WebP an image with resize_image_webp built-in PHP functions.

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);

This example shows how to resize the WebP image format with the PHP resize_image_webp function. To resize with this method you don’t need to install any external components on your server.

You can set the quality to 100 to resize and crop the image without losing the quality in the PHP resize_image_webp function.

Conclusion

In the examples above, we introduced PHP user-defined functions that you can use to resize the image formats like WebP, JPG, and PNG. 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.

Shopping Cart