Talking about how PHP solves the problem of lossless image compression,
This article describes how PHP solves the problem of Lossless Image Compression. The details are as follows:
The Code is as follows:
Header ("Content-type: image/jpeg"); $ file = "111.jpg"; $ percent = 1.5; // list of image compression ratios ($ width, $ height) = getimagesize ($ file); // obtain the source image size // scale the size $ newwidth = $ width * $ percent; $ newheight = $ height * $ percent; $ src_im = imagecreatefromjpeg ($ file); $ dst_im = bytes ($ newwidth, $ newheight); imagecopyresized ($ dst_im, $ src_im, 0, 0, 0, 0, $ newwidth, $ newheight, $ width, $ height); imagejpeg ($ dst_im); // output the compressed image imagedestroy ($ dst_im); imagedestroy ($ src_im );
I found that when imagecopyresized in php is used to scale a large image into a small image, the image will become blurred. At this time, it may be better to improve the clarity than to use imagecopyresampled instead of imagecopyresized.
Note: the loss of compression is inevitable. whether to accept this range is clear or not. for example, if some points on the source image are 2px, But you compress them five times, these points will disappear.
<? Php/*** desription compression image * @ param sting $ imgsrc image path * @ param string $ save path after imgdst compression */function image_png_size_add ($ imgsrc, $ imgdst) {list ($ width, $ height, $ type) = getimagesize ($ imgsrc); $ new_width = ($ width> 600? 600: $ width) * 0.9; $ new_height = ($ height> 600? 600: $ height) * 0.9; switch ($ type) {case 1: $ giftype = check_gifcartoon ($ imgsrc); if ($ giftype) {header ('content-Type: image/gif '); $ image_wp = imagecreatetruecolor ($ new_width, $ new_height); $ image = imagecreatefromgif ($ imgsrc); imagecopyresampled ($ image_wp, $ image, 0, 0, 0, 0, $ new_width, $ new_height, $ width, $ height); imagejpeg ($ image_wp, $ imgdst, 75); imagedestroy ($ image_wp);} break; case 2: header ('cont Ent-Type: image/jpeg '); $ image_wp = imagecreatetruecolor ($ new_width, $ new_height); $ image = imagecreatefromjpeg ($ imgsrc); imagecopyresampled ($ image_wp, $ image, 0, 0, 0, 0, $ new_width, $ new_height, $ width, $ height); imagejpeg ($ image_wp, $ imgdst, 75); imagedestroy ($ image_wp); break; case 3: header ('content-Type: image/png '); $ image_wp = imagecreatetruecolor ($ new_width, $ new_height); $ image = imagecreatefro Mpng ($ imgsrc); imagecopyresampled ($ image_wp, $ image, 0, 0, 0, 0, $ new_width, $ new_height, $ width, $ height); imagejpeg ($ image_wp, $ imgdst, 75); imagedestroy ($ image_wp); break ;}} /*** desription determine whether to perform GIF animation * @ param sting $ image_file image path * @ return boolean t is f No */function check_gifcartoon ($ image_file) {$ fp = fopen ($ image_file, 'rb'); $ image_head = fread ($ fp, 1024); fclose ($ fp); return preg_match ("/ ". Chr (0x21). chr (0xff). chr (0x0b). 'netscape2. 0'."/", $ image_head )? False: true;}?>
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.