Example of image scaling using imagecopyresampled function in PHP image processing

  • 2021-08-05 09:05:19
  • OfStack

Website optimization can not only be fixed on the code, content is also the site most need to optimize the object of 1, and image is the most important content in the site. The most important thing to deal with in image optimization is to automatically scale all large pictures uploaded to the website into small pictures (as long as the size is enough in the web page), so as to reduce the storage space of N times and improve the download and browsing speed. So picture scaling into a dynamic site must deal with the task, often and file upload bound in a job, can upload pictures at the same time to adjust its size. Of course, sometimes it is necessary to deal with picture scaling separately. For example, when making a picture list, if you use a large picture directly and scale it to a small picture when displaying it, it will not only slow down the download speed, but also reduce the page response time. Usually, when you encounter such an application, when you upload a picture, you will zoom out a small icon specially used for making a list. When you click this small icon, you will download a large picture to browse.

Use the GD library to process image scaling, usually using one of the imagecopyresized () and imagecopyresampled () functions, but use the imagecopyresampled () function to process the image with better quality. Here, we only introduce the use of imagecopyresampled () function under 1. 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, inserting pixel values smoothly, thus reducing the size of the image while still maintaining extremely high clarity. If it succeeds, it returns TRUE, and if it fails, it returns FALSE. The parameters dst_image and src_image are the identifiers of the target image and the source image, respectively. If the width and height of the source and target are different, the image shrinks and stretches accordingly, and the coordinates refer to the upper left corner. This function can be used to copy within the same graph (if dst_image and src_image are the same), but if the regions overlap, the result is unpredictable. In the following example, take the JPEG image format as an example, write a function thumb () for image scaling, and the code is as follows:


<?php
    // Used to zoom pictures
    function thumb($filename,$width=200,$height=200){
        // Get the original image $filename Width of $width_orig And height $height_orig
        list($width_orig,$height_orig) = getimagesize($filename);
        // Depending on the parameters $width And $height Value, convert the height and width of equal scale
        if ($width && ($width_orig<$height_orig)){
            $width = ($height/$height_orig)*$width_orig;
        }else{
            $height = ($width / $width_orig)*$height_orig;
        }
 
        // Zoom the original image to this newly created image resource
        $image_p = imagecreatetruecolor($width, $height);
        // Obtain the image resources of the original image
        $image = imagecreatefromjpeg($filename);
 
        // Use imagecopyresampled() Function for scaling settings
        imagecopyresampled($image_p,$image,0,0,0,0,$width,$height,$width_orig,$height_orig);
 
        // Scaled picture $image_p Save, 100( Best quality, largest file )
        imagejpeg($image_p,$filename);
 
        imagedestroy($image_p);
        imagedestroy($image);
    }
 
    thumb("brophp.jpg",100,100);
?>


Related articles: