Talking about how PHP solves the problem of lossless image compression,

Source: Internet
Author: User
Tags imagejpeg

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.