PHP can control the thumbnail clarity and the volume generated after the thumbnail. Let's take a look at how to use PHP to optimize our compressed images.
The development of PHP applications often involves generating thumbnails. The process of generating thumbnails using PHP itself is not difficult, but did you know that PHP is adjusted to optimize the quality of the thumbnails it generates? ?
12header('Content-type: image/PNG');
3$image=@imagecreatefrompng('http://www.phpernote.com/images/logo. png');
4imagepng($image,'logo.png',0); //Pay attention to the number 0 at the end, which is the compression level. Parameter range: 0-9*/
5imagedestroy($image) ;
The third parameter exceeds the imagepng function. The meaning of this parameter is the quality level of the generated image. Here, it can be divided into 10 levels (0-9), the uncompressed zero-level image will not be distorted, the clearest image, but the picture is also the largest, with more and more digital compression levels, The picture will become less and less clear, but when the compressed volume of the image can be reduced to 50% of the original, the compression ratio is still getting larger and larger. Let's look at a specific example, now a 125K raw volume, and the following test results through different compression levels:
imagepng($img,null,0); --> Size = 225K
imagepng($img,null,1); --> Size = 85.9K
imagepng($img,null,2); --> Size = 83.7K
imagepng($img,null,3); --> Size = 80.9K
imagepng($img,null,4); --> Size = 74.6K
imagepng($img,null,5); --> Size = 73.8K
imagepng($img,null,6); --> Size = 73K
imagepng($img,null,7); --> Size = 72.4K
imagepng($img,null,8); --> Size = 71K
imagepng($img,null,9); --> Size = 70.6
Copy after login
It should be noted that in the PHP entry, when the compression level is above 0, the volume is larger than the original, because the original image is actually The above is the result of slight compression, and compression level 0 is not a single bit of compression at all, so its volume will be larger than the original image. Based on the above conclusion and actual test results, the author concludes that to optimize images, it is generally appropriate to use level 2. The image is not deformed, but the volume is reduced by 30% for optimization purposes. If you compress at levels 6, 7, 8, and 9, the image has been deformed, but the volume reduction is not obvious. So these levels should not be used to optimize image compression.
The above is the detailed content of How to compress images in PHP?. For more information, please follow other related articles on the PHP Chinese website!