Changing the size of an image is a common functional requirement. next we will study how to change the size of an image in PHP. First, we will introduce a self-written function.
Copy codeThe code is as follows:
$ Imgsrc = "http://www.nowamagic.net/images/3.jpg ";
$ Width = 780;
$ Height = 420;
Resizejpg ($ imgsrc, $ imgdst, $ width, $ height );
Function resizejpg ($ imgsrc, $ imgdst, $ imgwidth, $ imgheight)
{
// $ Imgsrc jpg format image path $ imgdst jpg format Image save file name $ imgwidth to change the width $ imgheight to change the height
// Obtain the image width and height
$ Arr = getimagesize ($ imgsrc );
Header ("Content-type: image/jpg ");
$ ImgWidth = $ imgwidth;
$ ImgHeight = $ imgheight;
// Create image and define colors
$ Imgsrc = imagecreatefromjpeg ($ imgsrc );
$ Image = imagecreatetruecolor ($ imgWidth, $ imgHeight); // create a colored Basemap
Imagecopyresampled ($ image, $ imgsrc, 0, 0, 0, 0, $ imgWidth, $ imgHeight, $ arr [0], $ arr [1]);
Imagepng ($ image );
Imagedestroy ($ image );
}
?>
Imagecopyresampled
Imagecopyresampled -- re-sample and copy part of the image and resize it.
Int imagecopyresampled (resource dst_im, resource src_im, int dstX, int dstY, int srcX, int srcY, int dstW, int DTH, int srcW, int srcH)
Imagecopyresampled () copies a square area of an image to another image and smoothly inserts the pixel value. Therefore, in particular, the image size is reduced, while the image definition is maintained. Dst_im and src_im are the identifiers of the target image and source Image respectively. If the width and height of the source and target are different, the image will be scaled and stretched accordingly. Coordinates refer to the upper left corner. This function can be used to copy the region within the same image (if dst_im and src_im are the same), but the result is unpredictable if the region overlaps.
Note: There is a problem with the color palette image limit (255 + 1 color. The re-sampling or filtering of images usually requires more than 255 colors. an approximate value is used to calculate the new re-sampling pixels and their colors. When you try to assign a new color to the palette image, if it fails, we select the color with the closest calculation result (theoretically. This is not always the closest visual color. This may produce weird results, such as blank (or visually blank) images. To skip this issue, use a true color image as the target image, for example, created with imagecreatetruecolor.
Note: imagecopyresampled () requires GD 2.0.l or higher.
A simple example:
Copy codeThe code is as follows:
// The file
$ Filename = 'test.jpg ';
$ Percent = 0.5;
// Content type
Header ('content-Type: image/jpeg ');
// Get new dimensions
List ($ width, $ height) = getimagesize ($ filename );
$ New_width = $ width * $ percent;
$ New_height = $ height * $ percent;
// Resample
$ Image_p = imagecreatetruecolor ($ new_width, $ new_height );
$ Image = imagecreatefromjpeg ($ filename );
Imagecopyresampled ($ image_p, $ image, 0, 0, 0, 0, $ new_width, $ new_height, $ width, $ height );
// Output
Imagejpeg ($ image_p, null, 100 );
?>
Example 2:
Copy codeThe code is as follows:
// The file
$ Filename = 'test.jpg ';
// Set a maximum height and width
$ Width = 200;
$ Height = 200;
// Content type
Header ('content-Type: image/jpeg ');
// Get new dimensions
List ($ width_orig, $ height_orig) = getimagesize ($ filename );
$ Ratio_orig = $ width_orig/$ height_orig;
If ($ width/$ height> $ ratio_orig ){
$ Width = $ height * $ ratio_orig;
} Else {
$ Height = $ width/$ ratio_orig;
}
// Resample
$ Image_p = imagecreatetruecolor ($ width, $ height );
$ Image = imagecreatefromjpeg ($ filename );
Imagecopyresampled ($ image_p, $ image, 0, 0, 0, 0, $ width, $ height, $ width_orig, $ height_orig );
// Output
Imagejpeg ($ image_p, null, 100 );
?>
There are two ways to change the image size:
The ImageCopyResized () function is valid in all GD versions, but its algorithm for scaling images is rough.
ImageCopyResamples (), whose edge obtained by the pixel interpolation algorithm is relatively smooth. (But this function is slower than ImageCopyResized ).
The parameters of the two functions are the same, as shown below:
Copy codeThe code is as follows:
ImageCopyResampled (dest, src, dy, dx, sx, sy, dw, dh, sw, sh );
ImageCopyResized (dest, src, dy, dx, sx, sy, dw, dh, sw, sh );
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:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.