PHP Function ImageJpeg - Image Compression and Image Manipulation in PHP
Image optimization is one of the key steps to improve page loading speed and user experience. It can reduce your website’s server load and bandwidth consumption, as well as boost conversions by reducing page abandonment.
Luckily, there are a number of ways to compress images in PHP without sacrificing quality. Some of these methods are included in the standard GD library, while others are provided by third-party open source libraries that have advanced features for image compression and other image manipulation.
The standard php function imagejpeg() takes an already-uploaded image and creates a new jpeg file. This file can be saved, or the output can be sent directly to a browser. It’s important to note that, in order for the imagejpeg function to work, your PHP interpreter must be compiled with GD and should have GD support enabled by your web host.
This is the same for any other image manipulation function – to use them, your web host should have GD support compiled into it. To check if your PHP interpreter is GD-compatible, you can call the GD_support() function, which will return a list of supported image formats.
When using any of these image functions, you’ll need to allocate the colours (or “colors” if your language uses a different variant) of your image by calling the imagecolorallocate() function. This function will fill the background of the image with a specific colour, and returns an identifier for that particular colour.