Website optimization can not only be set on the code, content is the site most need to optimize one of the objects, and the image is the most important content of the site. The most need to deal with image optimization is to upload all the large images uploaded to the site automatically scaled small figure (in the Web page size enough on the line) to reduce the n times of storage space, and improve the speed of download browsing. So the image scaling into a dynamic site must deal with the task, often and file upload bound together to work, can upload the image at the same time adjust its size. Of course, sometimes also need to deal with the image scaling, such as in the picture list, if the direct use of large graphics and in the display of the zoom into a small picture, do not only download speed will be slow, but also reduce the page response time. Often encountered in such applications are uploaded images, and then for the image to shrink out a special use to make a list of small icons, when clicked on this small icon, will go to download a large image browsing.
Using the GD library to process picture scaling, it is common to use one of the imagecopyresized () and imagecopyresampled () two functions, and the imagecopyresampled () function is better at processing the quality. Here's how to use the imagecopyresampled () function. The prototype of the function looks like this:
Copy Code code 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 to another image, inserts a pixel value smoothly, thereby reducing the size of the image and still maintaining a very high definition. Returns true if successful, and returns False if it fails. The parameters Dst_image and Src_image are the identifiers of the target image and the source image respectively. If the source and destination are different in width and height, the corresponding image is shrunk and stretched, and the coordinates refer to the upper-left corner. This function can be used to replicate inside the same diagram (if Dst_image and Src_image are the same) but if the area overlaps, the result is unpredictable. In the following example, take the JPEG picture format as an example, write an image scaling function thumb (), the code looks like this:
Copy Code code as follows:
<?php
Used to scale a picture
function Thumb ($filename, $width =200, $height =200) {
Gets the width $width_orig and height of the original image $filename $height_orig
List ($width _orig, $height _orig) = getimagesize ($filename);
According to the parameter $width and $height value, the proportional scaling height and width are converted.
if ($width && ($width _orig< $height _orig)) {
$width = ($height/$height _orig) * $width _orig;
}else{
$height = ($width/$width _orig) * $height _orig;
}
Zoom in to this newly created picture resource
$image _p = Imagecreatetruecolor ($width, $height);
Get image resources for original artwork
$image = Imagecreatefromjpeg ($filename);
Use the imagecopyresampled () function to zoom settings
Imagecopyresampled ($image _p, $image, 0,0,0,0, $width, $height, $width _orig, $height _orig);
Will zoom the picture $image_p Save, 100 (best quality, file max)
Imagejpeg ($image _p, $filename);
Imagedestroy ($image _p);
Imagedestroy ($image);
}
Thumb ("brophp.jpg", 100,100);
?>