How to Use php and Other proportional scaling images,

Source: Internet
Author: User
Tags imagejpeg

How to Use php and Other proportional scaling images,

This article describes how to implement image compression in PHP. You can directly upload the code, imgzip ($ src, $ newwid, $ newhei), based on the specific application reference or improvement) this function carries the original image, the width required for scaling, and the scaling length.

<? 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 [1]); // 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);?>

I hope this article will help you with PHP programming.

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.