Optimizing Site Performance with PHP Image for SEO

Teguh Arief

Dec 17, 2020

Share article to

PHP Image

Currently, web developers build in many programming languages, such as PHP. This article shows you how to optimizing site performance by image optimization with PHP.

You can optimize images in PHP’s built-in functions with imagejpeg(), which takes the path of the output image and quality specification between 1 and 100. 

The Importance of Image Optimization

The faster images load on your website, the more likely visitors will return, yielding higher conversion rates.

Lengthy load time is a major contributing factor of page abandonment, which becomes more likely with each additional second of load time. 

So yes, loading speed is important for SEO. As long as your website loads in around 3 seconds, you should be fine with most users.

The last, it doesn't take up much space on server only 1 original image without thumbnail or other smaller size image.

In addition, it also keeps users from being burdened by the image size, and that follows the type of device used. Desktop use an original image, while mobile use an image smaller size.

PHP Image

In this case, the original image only be used on the desktop. As for mobile with PHP Image by smaller image sizes.

To get an original image use the $ _GET function. And PHP code below will work to produce a smaller image.

$image = imagecreatefromjpeg($_GET['file']);
$width = imagesx($image);
$height = imagesy($image);

if( $width < $height ){
$thumb_width = 180;
$thumb_height = 240;
$thumb_width = 320;
$thumb_height = 240;

$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );

imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
header('Content-Type: image/jpeg');
imagejpeg($thumb, NULL, 80);

Related Posts