Two options and differences for generating thumbnails using GD in php _ PHP Tutorial

Source: Internet
Author: User
Use GD to generate thumbnails in php. The GD extension of PHP provides two functions to scale the image: ImageCopyResized (dest, src, dx, dy, sx, sy, dw, dh, sw, sh); ImageCopyResampled (dest, the GD extension of src, dx, dy, sx, sy, dw PHP provides two functions to scale the image:
ImageCopyResized(dest, src, dx, dy, sx, sy, dw, dh, sw, sh);
ImageCopyResampled(dest, src, dx, dy, sx, sy, dw, dh, sw, sh);

ImageCopyResized ()Function in allGDIt is effective in the version, but its algorithm for scaling the image is rough, which may lead to image edge aliasing.GD 2.xAddedImageCopyResampled ()Function. the edge of the image obtained by the pixel interpolation algorithm is relatively smooth (but the speed ratio of this function isImageCopyResized ()Slow ).

Let's take a look at an example. We will quadruple this image:

$ Src=ImageCreateFromJPEG('Php.jpg');

$ Width=ImageSx($ Src);
$ Height=ImageSy($ Src);
$ X=$ Width/2;$ Y=$ Height/2;
$ Dst=ImageCreateTrueColor($ X,$ Y);
ImageCopyResized($ Dst,$ Src,0,0,0,0,$ X,$ Y,$ Width,$ Height);
// ImageCopyResampled ($ dst, $ src, 0, 0, 0, $ x, $ y, $ width, $ height );

Header('Content-Type: image/jpeg');
ImageJPEG($ Dst,'',100);
?>

SOURCE image:


Use
ImageCopyResized()Function generation result:



Use ImageCopyResampled()Function generation result:




Obviously, the effects of the two functions are different, ImageCopyResampled()The results generated by the function are smoother and better.

By the way, paste an effect and use ASCII to represent the image. ImageColorAt () has an interesting use, which can be checked cyclically.
The color of each pixel in the image, and then operate on the color data.
Source code:
 
           $im = imagecreatefromjpeg('test1.jpg'); 
$dx = imagesx($im);
$dy = imagesy($im);
for($y = 0; $y < $dy; $y++) {
for($x=0; $x < $dx; $x++) {
$col = imagecolorat($im, $x, $y);
$rgb = imagecolorsforindex($im,$col);
printf('*',
$rgb['red'],$rgb['green'],$rgb['blue']);
}
echo "
\n";
}
imagedestroy($im);
?>


Interesting, huh, huh ..

Incluimagecopyresized (dest, src, dx, dy, sx, sy, dw, dh, sw, sh); ImageCopyResampled (dest, src, dx, dy, sx, sy, dw...

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: info-contact@alibabacloud.com 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.