note 22316 added to function.imagecopyresized
| From: | tim at smoothdeity dot com | Date: | Thu, 13 Jun 2002 16:17:16 +0000 |
| Subject: | note 22316 added to function.imagecopyresized | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-31807@lists.php.net to get a copy of this message | ||
re: ImageCopyResampleBicubic()
here is a modified version of the above code that will properly handle copying only portions of
images (i.e. creating a square thumbnail from a rectangular image by cropping out the square rather
than smashing to fit). Also a few breaks were added to avoid some unnecessary calculations that
took place under some conditions:
function ImageCopyResampleBicubic (&$dst_img, &$src_img, $dst_x,
$dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
/*
port to PHP by John Jensen July 10 2001 (updated June 13, 2002 by tim@smoothdeity.com) --
original code (in C, for the PHP GD Module) by jernberg@fairytale.se
*/
{
$palsize = ImageColorsTotal ($src_img);
for ($i = 0; $i < $palsize; $i++)
{ // get palette.
$colors = ImageColorsForIndex ($src_img, $i);
ImageColorAllocate ($dst_img, $colors['red'], $colors['green'],
$colors['blue']);
}
$scaleX = ($src_w - 1) / $dst_w;
$scaleY = ($src_h - 1) / $dst_h;
$scaleX2 = (int) ($scaleX / 2);
$scaleY2 = (int) ($scaleY / 2);
$dstSizeX = imagesx( $dst_img );
$dstSizeY = imagesy( $dst_img );
$srcSizeX = imagesx( $src_img );
$srcSizeY = imagesy( $src_img );
for ($j = 0; $j < ($dst_h - $dst_y); $j++) {
$sY = (int) ($j * $scaleY) + $src_y;
$y13 = $sY + $scaleY2;
$dY = $j + $dst_y;
if (($sY > $srcSizeY) or ($dY > $dstSizeY))
break 1;
for ($i = 0; $i < ($dst_w - $dst_x); $i++) {
$sX = (int) ($i * $scaleX) + $src_x;
$x34 = $sX + $scaleX2;
$dX = $i + $dst_x;
if (($sX > $srcSizeX) or ($dX > $dstSizeX))
break 1;
$color1 = ImageColorsForIndex ($src_img, ImageColorAt ($src_img, $sX, $y13));
$color2 = ImageColorsForIndex ($src_img, ImageColorAt ($src_img, $sX, $sY));
$color3 = ImageColorsForIndex ($src_img, ImageColorAt ($src_img, $x34, $y13));
$color4 = ImageColorsForIndex ($src_img, ImageColorAt ($src_img, $x34, $sY));
$red = ($color1['red'] + $color2['red'] + $color3['red'] +
$color4['red']) / 4;
$green = ($color1['green'] + $color2['green'] + $color3['green'] +
$color4['green']) / 4;
$blue = ($color1['blue'] + $color2['blue'] + $color3['blue'] +
$color4['blue']) / 4;
ImageSetPixel ($dst_img, $dX, $dY,
ImageColorClosest ($dst_img, $red, $green, $blue));
}
}
}
--
http://www.php.net/manual/en/function.imagecopyresized.php
http://master.php.net/manage/user-notes.php?action=edit+22316
http://master.php.net/manage/user-notes.php?action=delete+22316
http://master.php.net/manage/user-notes.php?action=reject+22316