note 40594 deleted from function.imagecopyresized by aidan
| From: | aidan@php.net | Date: | Mon, 04 Oct 2004 09:37:18 +0000 |
| Subject: | note 40594 deleted from function.imagecopyresized by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-77901@lists.php.net to get a copy of this message | ||
Note Submitter:
----
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);
}
}
?>