note 40594 added to function.imagecopyresized

From: Date: Tue, 09 Mar 2004 07:50:45 +0000
Subject: note 40594 added to function.imagecopyresized
Groups: php.notes 
Request: Send a blank email to php-notes+get-66470@lists.php.net to get a copy of this message
heres what I use. Its very good for dynamic pics since its much faster that bicubic. every pixel is calculated as a avarage of two pixels. only small output will be resampled while large output will be genarated with imagecopyresized(). <? ImageCopyResample(.....); ImageJPEG(......); function ImageCopyResample(&$dst_img, &$src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { global $resample; //submittet by querystring $pxls = intval($src_w / $dst_w)-1; if (($resample!="bicubic" && $pxls>=2) or $resample=="bilinear"){ //slow but better quality ImagePaletteCopy ($dst_img, $src_img); $rX = $src_w / $dst_w; $rY = $src_h / $dst_h; $nY = 0; for ($y=$src_y; $y<$dst_h; $y++) { $oY = $nY; $nY = intval(($y + 1) * $rY+.5); $nX = 0; for ($x=$src_x; $x<$dst_w; $x++) { $r = $g = $b = $a = 0; $oX = $nX; $nX = intval(($x + 1) * $rX+.5); $c = ImageColorsForIndex ($src_img, ImageColorAt ($src_img, $nX, $nY)); $r += $c['red']; $g += $c['green']; $b += $c['blue']; $a++; $c = ImageColorsForIndex ($src_img, ImageColorAt ($src_img, $nX-$pxls, $nY-$pxls)); $r += $c['red']; $g += $c['green']; $b += $c['blue']; $a++; //you can add more pixels here! eg "$nX, $nY-$pxls" or "$nX-$pxls, $nY" ImageSetPixel ($dst_img, ($x+$dst_x-$src_x), ($y+$dst_y-$src_y), ImageColorClosest ($dst_img, $r/$a, $g/$a, $b/$a)); } } return true; } elseif ($resample=="bicubic"){ //very slow but better quality ImagePaletteCopy ($dst_img, $src_img); $rX = $src_w / $dst_w; $rY = $src_h / $dst_h; $nY = 0; for ($y=$src_y; $y<$dst_h; $y++) { $oY = $nY; $nY = intval(($y + 1) * $rY+.5); $nX = 0; for ($x=$src_x; $x<$dst_w; $x++) { $r = $g = $b = $a = 0; $oX = $nX; $nX = intval(($x + 1) * $rX+.5); for ($i=$nY; --$i>=$oY;) { for ($j=$nX; --$j>=$oX;) { $c = ImageColorsForIndex ($src_img, ImageColorAt ($src_img, $j, $i)); $r += $c['red']; $g += $c['green']; $b += $c['blue']; $a++; } } ImageSetPixel ($dst_img, ($x+$dst_x-$src_x), ($y+$dst_y-$src_y), ImageColorClosest ($dst_img, $r/$a, $g/$a, $b/$a)); } } return true; } else { //very fast! $dst_w++; $dst_h++; //->no black border return imagecopyresized($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } } ?> ---- Manual Page -- http://www.php.net/manual/en/function.imagecopyresized.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+40594 Delete -- http://master.php.net/manage/user-notes.php?action=delete+40594&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+40594&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#66470) next »