PHP instance ---- proportional scaling of images and other functions _ PHP Tutorial

Source: Internet
Author: User
PHP instance-proportional scaling of images. PHP instance-proportional scaling of images and other features in the process of website development, some images are often processed, such as scaling, cropping, and watermarking. Of course, these PHP instances-proportional scaling functions such as images

During website development, some images, such as scaling, cropping, and watermarking, are often processed. Of course, these functions can be used not only in the process of developing websites, but also in the function when processing images, just add the required parameters to the function as required. Although it may be difficult for Tom to come up at once, he will be familiar with it once again. Now, go to the topic. Today I will share a function for proportional scaling of images. Students in need can make some modifications and use them by themselves, saving their own writing (please ignore this ).

1. First, understand the functions that are needed in one piece of scaling.

If you do not understand it, you can use the php Development Manual to search for it. the above statements are very clear. it is a very good tool for learning, whether it is a newbie or an old bird.

2. share the code and write comments clearly (I feel good about myself and hope you can correct me)

 ($ Miny/$ maxy) {$ scale = $ miny/$ maxy;} else {$ scale = $ minx/$ maxx ;} // round the value to $ minx = floor ($ maxx * $ scale); $ miny = floor ($ maxy * $ scale ); // add a thumbnail $ minim = imagecreatetruecolor ($ minx, $ miny); // the scaling function imagecopyresampled ($ minim, $ maxim, $ minx, $ miny, $ maxx, $ maxy); // The image output header ("content-type: {$ maxm}"); // you can specify the image type. switch ($ maxt) {case 1: $ imgout = "imagegif"; break; case 2: $ imgout = "imagejpeg"; break; case 3: $ imgout = "imagepng"; break;} // change $Imgout($minim,'copypic.jpg '); // release the resource imagedestroy ($ maxim); imagedestroy ($ minim);} ImageShrink ("picture/apple.jpg", 50,50);?>


3. there are a few points in the entire code segment that are not easy to understand.

(1) The return value of the getimagesize () function is an array that contains the image size, format, and other information. You can obtain the values based on the key-value pairs, assign them to the variables, and then directly input the variables where they are needed.. For more information about functions, see the manual.

(2) for proportional scaling, we need to consider the scaling ratio, compare the length of the small image and the length and width of the big image, and select a smaller proportion as the scaling ratio. This is not very easy to understand. you can try it yourself.

(3) after scaling a large image, the image format does not change. Therefore, when the function is used to output the image, the variable function is used to determine the image format. Assign a function to a variable, which is quite embarrassing when used.

(4) you can set the format of the saved Image. the save path is the same as that of the original image. Other parameters depend on your needs.



In the process of website development, ghost often needs to process some images, such as scaling, cropping, and watermarking. Of course these functions...

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.