Home > Article > Backend Development > PHP image processing: Example of image scaling using imagecopyresampled function, _PHP tutorial
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:
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: