PHP image processing-use the imagecopyresampled function to scale images. example: _ PHP Tutorial

Source: Internet
Author: User
For PHP image processing, use the imagecopyresampled function to implement image scaling ,. For PHP image processing, the imagecopyresampled function is used to implement image scaling. website optimization cannot only be set to code, but the content is also one of the objects to be optimized, the image is an example of image scaling using the imagecopyresampled function for PHP image processing,

Website optimization cannot only be based on code. content is also one of the objects to be optimized on the website, and images are the most important content on the website. To optimize images, you need to automatically scale all the large images uploaded to your website to reduce storage space by N times, it also improves the download and browsing speed. Therefore, to scale an image into a task that must be processed by a dynamic website, it is often associated with file upload and can be resized while uploading the image. Of course, you also need to resize the image separately. for example, you can scale the image into a small image only when you use a large image to display it. this will not only slow down the download speed, the page response time is also reduced. Generally, when uploading an image, you can zoom out a small icon dedicated to the list. when you click this small icon, you can download the large image for browsing.

The GD library is used to process image scaling. generally, one of the imagecopyresized () and imagecopyresampled () functions is used, and the quality will be better after processing using the imagecopyresampled () function. Here we will only introduce how to use the imagecopyresampled () function. The function is prototype as follows:

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 the square area of an image to another image and inserts the pixel value smoothly. Therefore, the image size is reduced and the resolution remains extremely high. If the operation succeeds, TRUE is returned. if the operation fails, FALSE is returned. The dst_image and src_image parameters are the identifier of the target image and the source image respectively. If the width and height of the source and target are different, the corresponding image will be scaled and stretched. the coordinates refer 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 area overlaps, the result is unpredictable. The following example uses the JPEG image format as an example to compile the thumb () function for image scaling. the code is as follows:

The code is as follows:

<? Php
// Used to scale the image
Function thumb ($ filename, $ width = 200, $ height = 200 ){
// Obtain the width of the original image $ filename $ width_orig and height $ height_orig
List ($ width_orig, $ height_orig) = getimagesize ($ filename );
// Calculate the scaled height and width based on the values of $ width and $ height.
If ($ width & ($ width_orig <$ height_orig )){
$ Width = ($ height/$ height_orig) * $ width_orig;
} Else {
$ Height = ($ width/$ width_orig) * $ height_orig;

// Scale the source image to the newly created image resource.
$ Image_p = imagecreatetruecolor ($ width, $ height );
// Obtain the image resource of the source image
$ Image = imagecreatefromjpeg ($ filename );

// Use the imagecopyresampled () function for scaling settings
Imagecopyresampled ($ image_p, $ image, 0, 0, 0, $ width, $ height, $ width_orig, $ height_orig );

// Save the scaled image $ image_p to 100 (optimal quality, maximum file size)
Imagejpeg ($ image_p, $ filename );

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

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

Website, website optimization cannot only be set on the code, content is also one of the objects that the website needs to optimize, and images are the network...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.