Example of how to change the size of an image in PHP
小云云
Release: 2023-03-22 21:56:01
Original
4502 people have browsed it
This article mainly shares with you examples of how to change the size of images in PHP. I hope it can help you.
Changing the size of pictures is a very common functional requirement. Let’s start studying how to change the size of pictures in PHP. Let me first introduce a function I wrote myself.
imagecopyresampled -- Resample and resize a portion of the image.
int imagecopyresampled ( resource dst_im, resource src_im, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH)
imagecopyresampled() Copies a square area from one image to another, smoothly interpolating pixel values and thus, inter alia, reducing the size of the image while still maintaining great sharpness. dst_im and src_im are the identifiers of the target image and source image respectively. If the source and destination have different widths and heights, the image shrinks and stretches accordingly. The coordinates refer to the upper left corner. This function can be used to copy regions within the same image (if dst_im and src_im are the same), but the results are unpredictable if the regions overlap.
Note: There is a problem because of the palette image limit (255+1 colors). Resampling or filtering an image often requires more than 255 colors, and an approximation is used in calculating the new resampled pixels and their colors. When trying to assign a new color to a palette image, if that fails we choose the closest (theoretically) calculated color. This is not always the visually closest color. This can produce weird results, such as blank (or visually blank) images. To bypass this problem, use a truecolor image as the target image, such as one created with imagecreatetruecolor().
Note: imagecopyresampled() requires GD 2.0.l or higher.
There are two ways to change the size of the image:
The ImageCopyResized() function is valid in all GD versions, but its algorithm for scaling images is relatively rough.
ImageCopyResamples(), the image edges obtained by its pixel interpolation algorithm are relatively smooth. (But this function is slower than ImageCopyResized()).
The parameters of the two functions are the same, as follows:
The above is the detailed content of Example of how to change the size of an image in PHP. For more information, please follow other related articles on the PHP Chinese 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