Two choices and differences for generating thumbnails with GD

Source: Internet
Author: User
difference | Thumbnail PHP's GD extension provides two functions to scale images:
Imagecopyresized (destsrcdxdysxsyDWDH  SWsh);
Imagecopyresampled (destsrcdxdysxsyDW dhSWSH

The imagecopyresized () function is valid in all versions of GD, but its algorithm for scaling the image is coarse and may cause jagged edges of the image. GD 2.x has a new imagecopyresampled () function, its pixel interpolation algorithm to get the edge of the image is relatively smooth (but the function is faster than imagecopyresized () slow).

To see an example, we'll shrink this figure four times times:
<?php

 $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,0,$x,$y,$width,$height);

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

原图:


使用
ImageCopyResized()函数生成的结果:



使用 ImageCopyResampled()函数生成的结果:




It's obvious to see that two functions produce different image effects, ImageCopyResampled()函数生成的结果比较平滑,效果较好。

By the way, you put an effect in ASCII to represent the image. Imagecolorat () has a very interesting use, it can be cycled check
The color of each pixel in the image, and then manipulate the color data.
Source:
<body bgcolor= "#000000" style= "line-height:6pt" ><?php
$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 (' <font color=#%02x%02x%02x>*</font> ',
$rgb [' Red '], $rgb [' Green '], $rgb [' Blue ']]);
}
echo "<br>\n";
}
Imagedestroy ($im);
?>
</body>


It's funny, huh?


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.