Basics of compressing images using PHP

炎欲天舞
Release: 2023-03-14 16:00:02
Original
5112 people have browsed it

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 about PHP adjustments 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.6K

It should be noted that in getting started with PHP , when the compression level is above 0, the volume is larger than the original, because the original image is actually the result of slight compression, and compression level 0 is not a bit compressed 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 Basics of compressing images using PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!