PHP practical image compression method

Source: Internet
Author: User
: This article mainly introduces the PHP practical image compression method. if you are interested in the PHP Tutorial, please refer to it.
 1 & $ ratio_h> 1) {if ($ ratio_w <$ ratio_h) {$ ratio = $ ratio_h;} else {$ ratio = $ ratio_w ;} // define an intermediate temporary Image. the image's aspect ratio exactly meets the target requirement $ inter_w = (int) ($ new_width/$ ratio); $ inter_h = (int) ($ new_height/$ ratio); $ inter_img = imagecreatetruecolor ($ inter_w, $ inter_h); imagecopy ($ inter_img, $ src_img, 0, 0, 0, 0, $ inter_w, $ inter_h); // generate a temporary image with the maximum edge length as the size of the target image $ ratio // define a new image $ new_img = imagecreatet Ruecolor ($ new_width, $ new_height); imagecopyresampled ($ new_img, $ inter_img, 0, 0, 0, $ new_width, $ new_height, $ inter_w, $ inter_h ); switch ($ type) {case IMAGETYPE_JPEG: imagejpeg ($ new_img, $ dst_file, 100); // store image break; case IMAGETYPE_PNG: imagepng ($ new_img, $ dst_file, 100 ); break; case IMAGETYPE_GIF: imagegif ($ new_img, $ dst_file, 100); break; default: break ;}} else {$ ratio = $ ratio_h> $ ra Tio_w? $ Ratio_h: $ ratio_w; // take the value with a large proportion. // define a large middle image with the same height or width as the target image, then enlarge the source image $ inter_w = (int) ($ w * $ ratio); $ inter_h = (int) ($ h * $ ratio); $ inter_img = imagecreatetruecolor ($ inter_w, $ inter_h); // crop imagecopyresampled ($ inter_img, $ src_img, 0, 0, 0, 0, $ inter_w, $ inter_h, $ w, $ h); // define a new image $ new_img = imagecreatetruecolor ($ new_width, $ new_height); imagecopy ($ new_img, $ inter_img, 0, 0, 0, 0, 0, $ new_width, $ new_height); switch ($ type) {case IMAGETYPE_JPEG: imagejpeg ($ new_img, $ dst_file, 100); // store the image break; case IMAGETYPE_PNG: imagepng ($ new_img, $ dst_file, 100); break; case IMAGETYPE_GIF: imagegif ($ new_img, $ dst_file, 100); break; default: break;} comment ', 200,200 );

The above introduces the PHP practical image compression method, including the content, hope to be helpful to friends interested in PHP tutorials.

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.