Scaling features such as ———— Pictures for PHP instances

Source: Internet
Author: User

In the Web site development process, often to some of the pictures to do some processing, such as scaling, cropping, plus watermark and so on. Of course, these features can not only be used in the development of the site process, usually need to do some processing of the picture, the function is encapsulated into the function, to deal with the picture, according to their own requirements in the function to add the required parameters do not get it done. Although this for small white may suddenly come up on the point of difficulty, but a two back to ripe. All right, get to the chase. Share an image, such as scale-scaling functions today. Students in need can make a slight change, and then take the use of their own, the province's own write (the great god Please ignore).

1. First, learn more about the functions needed in a scale

Have not quite understood can use the PHP development manual to search, above said is very clear, is learning a very good tool, whether it is novice or veteran.

2. Share the code, the comments are very clear (self-feeling good, the shortcomings of the hope that everyone correct)

<?phpfunction Imageshrink ($imgfile, $minx, $miny) {//Get large image information $imgarr=getimagesize ($imgfile); $maxx = $imgarr [0];//width $ maxy= $imgarr [1];//long $maxt= $imgarr [2];//format $maxm= $imgarr [' MIME '];//mime type//Large map resource $maxim=imagecreatefromjpeg ($ Imgfile);//Zoom Determine if (($minx/$maxx) > ($miny/$maxy)) {$scale = $miny/$maxy;} else{$scale = $minx/$maxx;} Rounding the $minx=floor ($maxx * $scale), $miny =floor ($maxy * $scale);//Add small figure $minim=imagecreatetruecolor ($minx, $miny); /Zoom function imagecopyresampled ($minim, $maxim, 0,0,0,0, $minx, $miny, $maxx, $maxy);//Small Graph output header ("content-type:{$MAXM}");// Determine the picture type switch ($MAXT) {Case 1: $imgout = "Imagegif"; Break;case 2: $imgout = "Imagejpeg"; Break;case 3: $imgout = "Imagepng"; break;} Variable function $imgout ($minim, ' copypic.jpg ');//Release Resource Imagedestroy ($maxim); Imagedestroy ($minim);} Imageshrink ("Picture/apple.jpg", 50,50);? >


3. There are several areas of the code that are not easy to understand, a little explanation

(1) getimagesize () The return value of this function is an array that contains information such as the image size, format, and so on. The values can be obtained from the key-value pairs, assigned to the variable, and then passed directly to the variable where it is needed later . For functions, please see the manual.

(2) in the positive scaling, we need to consider the scale of scaling, take a small graph of the length and large image of the length of the comparison, select a small scale as the scale. This is not particularly good understanding, you can try it yourself.

(3) Large map after zooming, the format of the small chart has not changed, so in the later need to output the function of the image, the image format, here you need to use the variable function. Assign a function to a variable, and it feels pretty good when used.

(4) Save the picture format can set itself, save the path with the original picture of the same path. Other parameters see personal needs.



Scaling features such as ———— Pictures for PHP instances

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.