PHP image processing using the imagecopyresampled function to achieve picture scaling example

Source: Internet
Author: User
Site optimization can not only be set in the code, content is also the site of the most need to optimize one of the objects, and the image is the main content of the site. Image optimization Most need to deal with is to upload all the large images uploaded to the site automatically scaled scale (in the Web page is enough) to reduce storage space n times, and improve the speed of download browsing. So the image is scaled to a dynamic site must be processed tasks, often and file upload binding work together, you can upload pictures at the same time adjust its size. Of course, sometimes you need to handle the image scaling separately, for example, when you make a picture list, if you use a large image to zoom to a small image, it will not only slow down the download speed, but also reduce the page response time. Usually encountered such an application is when uploading pictures, and then to the image of a special used to make a list of small icons, when you click on this small icon, will download the large view.

Using the GD library to handle picture scaling, usually using one of the two functions of imagecopyresized () and imagecopyresampled (), and using the imagecopyresampled () function to handle quality is better. Here are just a few ways to use the imagecopyresampled () function. The prototype of the function is as follows:

BOOL Imagecopyresampled (Resource Dst_image,resource src_image,int dst_x,int dst_y,int src_x,int src_y,int dst_w,int DST _h, int src_w,int src_h)

This function copies the square area of an image into another image, inserting the pixel values smoothly, thus reducing the size of the image and still maintaining a high level of clarity. Returns true if successful, and false if it fails. The parameters Dst_image and Src_image are identifiers for the target image and the source image, respectively. If the source and destination are different in width and height, the corresponding image shrinks and stretches, and the coordinates refer to the upper-left corner. This function can be used to copy inside the same image (if Dst_image and Src_image are the same) but if the area overlaps, the result is unpredictable. In the following example, in the case of JPEG picture format, write an image scaling function thumb (), as shown in the code below:

<?php//For zooming the picture function thumb ($filename, $width =200, $height =200) {/        /Get the original image $filename width $width_orig and height $height_orig list ($width _orig, $height _orig) = getimagesize ($filename); Depending on the parameter $width and $height values, the height and width of the equal scale are scaled if ($width && ($width _orig< $height _orig)) {$width = ($he        ight/$height _orig) * $width _orig;        }else{$height = ($width/$width _orig) * $height _orig;        }//The original image is scaled to this newly created picture resource $image _p = Imagecreatetruecolor ($width, $height);         Gets the image resource $image = Imagecreatefromjpeg ($filename) of the original picture; Use the imagecopyresampled () function to scale settings imagecopyresampled ($image _p, $image, 0,0,0,0, $width, $height, $width _orig, $height _         Orig);         Save the scaled picture $image_p, 100 (best quality, File max) imagejpeg ($image _p, $filename);        Imagedestroy ($image _p);    Imagedestroy ($image); } Thumb ("Brophp.jpg", 100,100);? 

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.