note 33775 added to function.imagecopyresized

From: Date: Mon, 07 Jul 2003 15:07:38 +0000
Subject: note 33775 added to function.imagecopyresized
Groups: php.notes 
Request: Send a blank email to php-notes+get-51636@lists.php.net to get a copy of this message
The trouble with imagecopyresized/resampled is quality. I've taken one of the bicubic resampling functions and altered it a bit. Now, it supports copying to a certain rectangle in the destination image without altering the aspect ratio. Here's the code: function ImageCopyResampleBicubic(&$dst, &$src, $dstx, $dsty, $srcx, $srcy, $w, $h, $zoomX, $zoomY = '') { if (!$zoomY) $zoomY = $zoomX; $palsize = ImageColorsTotal($src); for ($i = 0; $i<$palsize; $i++) { $colors = ImageColorsForIndex($src, $i); ImageColorAllocate($dst, $colors['red'], $colors['green'], $colors['blue']); } $zoomX2 = (int)($zoomX/2); $zoomY2 = (int)($zoomY/2); $dstX = imagesx($dst); $dstY = imagesy($dst); $srcX = imagesx($src); $srcY = imagesy($src); for ($j = 0; $j<($h-$dsty); $j++) { $sY = (int)($j*$zoomY)+$srcy; $y13 = $sY+$zoomY2; $dY = $j+$dsty; if (($sY >= $srcY) or ($dY >= $dstY) or ($y13 >= $srcY)) break 1; for ($i = 0; $i<($w-$dstx); $i++) { $sX = (int)($i*$zoomX)+$srcx; $x34 = $sX+$zoomX2; $dX = $i+$dstx; if (($sX >= $srcX) or ($dX >= $dstX) or ($x34 >= $srcX)) break 1; $c1 = ImageColorsForIndex($src, ImageColorAt($src, $sX, $y13)); $c2 = ImageColorsForIndex($src, ImageColorAt($src, $sX, $sY)); $c3 = ImageColorsForIndex($src, ImageColorAt($src, $x34, $y13)); $c4 = ImageColorsForIndex($src, ImageColorAt($src, $x34, $sY)); $r = ($c1['red']+$c2['red']+$c3['red']+$c4['red'])/4; $g = ($c1['green']+$c2['green']+$c3['green']+$c4['green'])/4; $b = ($c1['blue']+$c2['blue']+$c3['blue']+$c4['blue'])/4; ImageSetPixel($dst, $dX, $dY, ImageColorClosest($dst, $r, $g, $b)); } } } ---- Manual Page -- http://www.php.net/manual/en/function.imagecopyresized.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33775 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33775&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33775&report=yes

« previous php.notes (#51636) next »