Image optimization, which results in the smallest possible file size but no loss in visual quality, is a mandatory step before delivery. That way, you save bytes and improve website performance because the smaller the image files, the faster the browser can download and render them on viewers’ screens.
This article shows you how to compress images with PHP, one of the most common server-side languages for building web apps.
Advantages of Compressing and Optimizing Images With PHP
The faster images load on your website, the more likely visitors will return, yielding higher conversion rates. In fact, half of all visitors expect websites to load within two seconds. Surveys from Akamai and Gomez have revealed that—
- Lengthy load time is a major contributing factor of page abandonment, which becomes more likely with each additional second of load time. Nearly 40 percent of visitors abandon a page after only three seconds.
- Seventy-three percent of mobile users have experienced problems with load times on their devices.
- A one-second delay in load time can cut visitor satisfaction by 16 percent, leading to a seven-percent-or-higher drop in conversion rate.
Compressing Images With PHP
You can compress images in PHP in one of the following ways:
- Through PHP’s built-in functions, such as
imagejpeg()
, which takes the path of the output image and quality specification between 1 and 100. As a prerequisite, install the PHP’s GD Graphics Library. - Through the PHP Image Optimizer, a PHP library built by Piotr Sliwa, which leverages several libraries, including OptiPNG,
pngquant
, and[jpegoptim
](http://jpegclub.org/jpegtran/). The Image Optimizer can compress image files by 10-70 percent without sacrificing visual appeal. - With the PHP Image Cache, a simple PHP class that compresses images on the fly. Once PHP Image compression is complete, the Image Cache caches the image in your browser and then returns the image’s new source.
- With Imagick, a native PHP extension for creating and modifying images by means of the ImageMagick API.
An Ideal Alternative for Compressing Images With PHP
With Cloudinary, you can efficiently optimize media assets—regardless of programming language. One reason is that, by default, Cloudinary automatically performs certain optimization steps on all transformed images. Plus, its integrated, fast delivery capability through content delivery networks (CDNs) ensures that images are seamlessly displayed on viewers’ devices.
Cloudinary offers the following image compression capabilities:
Automatic quality adjustment and encoding: Once you have added the
q_auto
parameter to an image URL, Cloudinary chooses the optimal quality-compression level and encoding settings according to the image content, its format, and the viewing browser. The result is a compressed image with superior visual quality. See this example:To fine-tune the visual quality of your images through automation, add the
q_auto:best
.q_auto:low
,q_auto:good
, orq_auto:eco
parameter, as you desire.Automatic formatting: Adding the
f_auto
parameter to image URLs causes Cloudinary to optimally format images. In most cases, Chrome users would receive a WebP (.webp
); Internet Explorer users, a JPEG-XR (.jxr
); and Safari users, a JPEG 2000 (.jp2). If a browser does not support any of those formats, Cloudinary delivers the image in the format depicted by the file extension.In case you add both the
f_auto
andq_auto
parameters to the URL of a JPEG image, Cloudinary’s automated quality algorithm halts chroma subsampling, retaining the JPEG format with no switch to WebP. That’s a quality safeguard because the lossy WebP format always performs chroma subsampling, which might reduce the visual quality of some images.Resizing and cropping: By adding the width (
w
) and height (h
) parameters to their URLs, you can resize images with Cloudinary, as in this example:
Here, Cloudinary maintains the aspect ratio but resizes the image to the height and width you specified.
Cloudinary supports many image-cropping modes: scale, fit, mfit, fill, lfill, limit, pad, lpad, mpad, crop, thumb, imagga_crop, and imagga_scale.
Conclusion
Properly optimizing and caching images is a crucial step for enhancing website performance. The tips this post offers are just a start. See the Cloudinary documentation for more ideas on optimization of JPEG images without compromising quality and on image transformations.
Want to Learn More About Compressing And Optimizing Images?
- Image Optimization for Websites: Beautiful Pages That Load Quickly
- Python Image Optimization and Transformation
- Evolution of : GIF Without the GIF
- Top 10 Mistakes in Handling Website Images and How to Solve Them
- Three Popular and Efficient Ways for Loading Images
- Introducing Cloudinary’s WordPress Plugin for Dynamic Images and Video
- CDN for Images: Optimize and Deliver Images Faster
- Compress an Image Automatically Without Losing Quality
- Automatically Reduce Image Size Without Losing Quality
- How to Automatically Adapt Website Images to Retina and HiDPI Devices