Two selection and difference _php techniques for generating thumbnails using GD in PHP

Source: Internet
Author: User
The GD extension of PHP 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 , "" );
?>

Artwork:


using
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.