PHP and Other proportional scaling image details and instance code,

Source: Internet
Author: User
Tags imagejpeg

PHP and Other proportional scaling image details and instance code,

Run the Code directly. The imgzip ($ src, $ newwid, $ newhei) function includes the original image, the width required for scaling, and the length of scaling. The Code has been commented out. If you don't understand it, leave a message. Haha

<? Php // compress image thumbnails $ src = "xiezheng.jpg"; $ newwid = 640; $ newhei = 480; function imgzip ($ src, $ newwid, $ newhei) {$ imgInfo = getimagesize ($ src); $ imgType = image_type_to_extension ($ imgInfo [2], false); $ fun = "imagecreatefrom {$ imgType }"; // declare the image to open the image in the memory $ image = $ fun ($ src); // you can easily configure the length, width, and height. The setting box is the variable wid, the height is hei $ wid = $ imgInfo [0]; $ hei = $ imgInfo [1]; // you can determine the length and width to facilitate proportional scaling, 500,320 if ($ wid> $ hei) {$ wid = $ newwid; $ hei = $ newwid/($ wi D/$ hei);} else {$ wid = $ newhei * ($ wid/$ hei); $ hei = $ newhei ;} // create an image in the memory $ images2 = imagecreatetruecolor ($ newwid, $ newhei ); // create a 500*320 image // copy the original image to the new image // imagecopyresampled ($ dst_image, $ src_image, $ dst_x, $ dst_y, $ src_x, $ src_y, $ dst_w, $ dst_h, $ src_w, $ src_h) imagecopyresampled ($ images2, $ image, 0, 0, 0, 0, $ wid, $ hei, $ imgInfo [0], $ imgInfo [0]); // destroy the original image imagedestroy ($ image); // directly output the image File header ("Content-type :". $ imgInfo ['mime ']); imagejpeg ($ images2); // Save the image to the new file imagejpeg ($ images2, 'new.jpg', 100 ); // 10 code output image quality 0-100 100 highest quality // destroy imagedestroy ($ images2);} imgzip ($ src, $ newwid, $ newhei);?>

Effect

Before Compression

After compression

The above is the sorting of PHP and Other proportional scaling images. We will continue to add relevant materials in the future. Thank you for your support for this site!

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.