The function of changing the size of a picture in a PHP program most people want to use imagecopyresized (), but after testing, it is found that using imagecopyresampled () changes the image quality higher.
Let's take a look at the comparison results.
Original:
Use imagecopyresized () to reduce the picture by half
Code:
<?php
File and new size
$filename = ' test.jpg ';
$percent = 0.5;
Content type
Header (' Content-type:image/jpeg ');
Get New Sizes
List ($width, $height) = getimagesize ($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
Load
$thumb = Imagecreatetruecolor ($newwidth, $newheight);
$source = Imagecreatefromjpeg ($filename);
Resize
Imagecopyresized ($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
Output
Imagejpeg ($THUMB);
?>
Change the picture after:
Use imagecopyresampled () to reduce the picture by half
Code:
<?php
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);
?>
Change the picture after:
It can be seen that imagecopyresampled () changes the image size after the quality is higher than imagecopyresized ().
Imagecopyresampled () The quality is higher than imagecopyresized () after changing the image size.