Website optimization cannot just focus on the code. Content is also one of the most important objects of the website, and images are the most important content of the website. The most important thing to deal with when optimizing images is to automatically scale all large images uploaded to the website into small images (the size is sufficient for the web page), so as to reduce the storage space by N times and increase the speed of downloading and browsing. Therefore, the task of scaling images into a dynamic website must be processed. It is often tied to file upload and can be resized while uploading images. Of course, sometimes it is necessary to handle image scaling separately. For example, when making a picture list, if you directly use a large image and only zoom it into a small image when it is displayed, this will not only slow down the download speed, but also reduce the page response time. Usually when encountering such an application, when uploading a picture, a small icon specially used for making a list is scaled for the picture. When this small icon is clicked, the large picture will be downloaded for browsing.
When using the GD library to process image scaling, you usually use one of the two functions imagecopyresized() and imagecopyresampled(). The quality will be better after using the imagecopyresampled() function. Here we only introduce how to use the imagecopyresampled() function. The prototype of this 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 a square area from one image to another, smoothly interpolating pixel values, thus reducing the size of the image while still maintaining minimal High definition. Returns TRUE if successful, FALSE if failed. The parameters dst_image and src_image are the identifiers of the target image and source image respectively. If the source and target have different widths and heights, the image will be shrunk and stretched accordingly, with coordinates referring to the upper left corner. This function can be used to copy within the same image (if dst_image and src_image are the same) but if the areas overlap, the results are unpredictable. In the following example, taking the JPEG image format as an example, write an image scaling function thumb(). The code is as follows:
<?php //用于对图片进行缩放 function thumb($filename,$width=200,$height=200){ //获取原图像$filename的宽度$width_orig和高度$height_orig list($width_orig,$height_orig) = getimagesize($filename); //根据参数$width和$height值,换算出等比例缩放的高度和宽度 if ($width && ($width_orig<$height_orig)){ $width = ($height/$height_orig)*$width_orig; }else{ $height = ($width / $width_orig)*$height_orig; } //将原图缩放到这个新创建的图片资源中 $image_p = imagecreatetruecolor($width, $height); //获取原图的图像资源 $image = imagecreatefromjpeg($filename); //使用imagecopyresampled()函数进行缩放设置 imagecopyresampled($image_p,$image,0,0,0,0,$width,$height,$width_orig,$height_orig); //将缩放后的图片$image_p保存,100(质量最佳,文件最大) imagejpeg($image_p,$filename); imagedestroy($image_p); imagedestroy($image); } thumb("brophp.jpg",100,100); ?>
More articles related to PHP image processing examples of image scaling using the imagecopyresampled function Please pay attention to PHP Chinese website!