PHP image processing: Example of image scaling using imagecopyresampled function, _PHP tutorial

WBOY
Release: 2016-07-13 10:13:44
Original
770 people have browsed it

PHP image processing using imagecopyresampled function to achieve image scaling example,

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.

Use 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 looks like this:

Copy code The code 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 extremely 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:

Copy code The code is as follows:

//Used to zoom in and out of images
Function thumb($filename,$width=200,$height=200){
​​​​ //Get the width $width_orig and height $height_orig
of the original image $filename List($width_orig,$height_orig) = getimagesize($filename);
//According to the parameter $width and $height values, convert the proportionally scaled height and width
If ($width && ($width_orig<$height_orig)){
              $width = ($height/$height_orig)*$width_orig;
          }else{
              $height = ($width / $width_orig)*$height_orig;
        }

            // Scale the original image into this newly created image resource
         $image_p = imagecreatetruecolor($width, $height);
​​​​ //Get the image resource of the original image
         $image = imagecreatefromjpeg($filename);

​​​​ //Use the imagecopyresampled() function to set the zoom setting
Imagecopyresampled($image_p,$image,0,0,0,0,$width,$height,$width_orig,$height_orig);

​​​​ //Save the zoomed image $image_p, 100 (best quality, largest file)
Imagejpeg($image_p,$filename);

Imagedestroy($image_p);
Imagedestroy($image);
}

Thumb("brophp.jpg",100,100);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/914051.htmlTechArticlePHP image processing using imagecopyresampled function to achieve image scaling example, website optimization cannot only be determined in the code, the content is also the website One of the objects that needs optimization most, and images are the network...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!