Home > Backend Development > PHP Tutorial > Example of how to change the size of an image in PHP

Example of how to change the size of an image in PHP

小云云
Release: 2023-03-22 21:56:01
Original
4551 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.

01
#02
03 $imgsrc = "http://www.nowamagic.net/images/3.jpg";
#
04$width = 780;
05$height = 420;
06 
07resizejpg($imgsrc,$imgdst,$width,$height);
08 
09function resizejpg($imgsrc,$imgdst,$imgwidth,$imgheight)
10{
11 //$imgsrc jpg format image path $imgdst jpg format image saving file name $imgwidth The width to be changed $imgheight The height to be changed
12 //Get the width and height values ​​of the image
13    $arr = getimagesize($imgsrc);                    
14    header("Content-type: image/jpg");
15     
16    $imgWidth = $imgwidth;
17    $imgHeight = $imgheight;
18    // Create image and define colors
19    $imgsrc = imagecreatefromjpeg($imgsrc);
20 $image = imagecreatetruecolor($imgWidth, $imgHeight); //Create a color basemap
21    imagecopyresampled($image, $imgsrc, 0, 0, 0, 0,$imgWidth,$imgHeight,$arr[0], $arr[1]);
22    imagepng($image);
23    imagedestroy($image);
24}
25 
26?>

imagecopyresampled

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.

A simple example:

##
01
02// The file
03$filename = 'test.jpg';
04$percent = 0.5;
05 
06// Content type
07header('Content-Type: image/jpeg');
08 
09// Get new dimensions
10list($width, $height) = getimagesize($filename);
11$new_width = $width * $percent;
12$new_height = $height * $percent;
13 
14// Resample
15$image_p = imagecreatetruecolor($new_width, $new_height);
16$image = imagecreatefromjpeg($filename);
17imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
18 
19// Output
20imagejpeg($image_p, null, 100);
21?>

Example 2:

view source

print?

##01
##02
// The file
03$filename = 'test.jpg';
04
05// Set a maximum height and width
06$width = 200;
07$height = 200;
08 
09// Content type
10header('Content-Type: image/jpeg');
11 
12// Get new dimensions
13list($width_orig, $height_orig) = getimagesize($filename);
14 
15$ratio_orig = $width_orig/$height_orig;
16 
17if ($width/$height > $ratio_orig) {
18    $width = $height*$ratio_orig;
19 } else {
20    $height = $width/$ratio_orig;
21 }
22  
23 // Resample
24 $image_p = imagecreatetruecolor($width, $height);
25 $image = imagecreatefromjpeg($filename);
26 imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
27  
28 // Output
29 imagejpeg($image_p, null, 100);
30 ?>

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:

1 imageCopyResampled(dest,src ,dy,dx,sx,sy,dw,dh,sw,sh);
2 imageCopyResized(dest,src,dy,dx,sx,sy,dw,dh,sw,sh);

Example:

##01
02$src = ImageCreateFromJPEG('php.jpg');
03$width = ImageSx($src);
04$height = ImageSy($src);
05$x = $widht/2;
06$y = $height/2;
07$dst = ImageCreateTrueColor($x,$y);
08ImageCopyResampled($dst,$src,0,0,0,0,$x,$y,$widht,$height);
09header('Content-Type : image/png');
#10ImagePNG($det);
#11 ?>
#

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!

Related labels:
source:php.cn
Statement of this 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template