function imageCopyResampleBicubic()
June 20th, 2007
No comments
<?php
/**
* @author Taken from the comments in the PHP manuals imageCopyResampled, by ron at korving dot demon dot nl
* @version 1.0
* @return void
* @param string $dst_img
* @param string $src_img
* @param integer $dst_x
* @param integer $dst_y
* @param integer $src_x
* @param integer $src_y
* @param integer $dst_w
* @param integer $dst_h
* @param integer $src_w
* @param integer $src_h
* @access public
* @desc Rezised/resamples an image. Faster and higher quality than the regular imageCopyResampled
* @example imageCopyResampleBicubic('new_img.png', 'old_img.png', 0, 0, 0, 0, 100, 100, 1000, 1000);
*/
function imageCopyResampleBicubic($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
{
$scaleX = ($src_w - 1) / $dst_w;
$scaleY = ($src_h - 1) / $dst_h;
$scaleX2 = $scaleX / 2.0;
$scaleY2 = $scaleY / 2.0;
$tc = imageIsTrueColor($src_img);
for ($y = $src_y; $y < $src_y + $dst_h; $y++) {
$sY = $y * $scaleY;
$siY = (int) $sY;
$siY2 = (int) $sY + $scaleY2;
for ($x = $src_x; $x < $src_x + $dst_w; $x++) {
$sX = $x * $scaleX;
$siX = (int) $sX;
$siX2 = (int) $sX + $scaleX2;
if ($tc) {
$c1 = imageColorAt($src_img, $siX, $siY2);
$c2 = imageColorAt($src_img, $siX, $siY);
$c3 = imageColorAt($src_img, $siX2, $siY2);
$c4 = imageColorAt($src_img, $siX2, $siY);
$r = (($c1 + $c2 + $c3 + $c4) >> 2) & 0xFF0000;
$g = ((($c1 & 0xFF00) + ($c2 & 0xFF00) + ($c3 & 0xFF00) + ($c4 & 0xFF00)) >> 2) & 0xFF00;
$b = ((($c1 & 0xFF) + ($c2 & 0xFF) + ($c3 & 0xFF) + ($c4 & 0xFF)) >> 2);
imageSetPixel($dst_img, $dst_x + $x - $src_x, $dst_y + $y - $src_y, $r+$g+$b);
} else {
$c1 = imageColorsForIndex($src_img, imageColorAt($src_img, $siX, $siY2));
$c2 = imageColorsForIndex($src_img, imageColorAt($src_img, $siX, $siY));
$c3 = imageColorsForIndex($src_img, imageColorAt($src_img, $siX2, $siY2));
$c4 = imageColorsForIndex($src_img, imageColorAt($src_img, $siX2, $siY));
$r = ($c1['red'] + $c2['red'] + $c3['red'] + $c4['red'] ) << 14;
$g = ($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) << 6;
$b = ($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue'] ) >> 2;
imageSetPixel($dst_img, $dst_x + $x - $src_x, $dst_y + $y - $src_y, $r+$g+$b);
}
}
}
}
?>
/**
* @author Taken from the comments in the PHP manuals imageCopyResampled, by ron at korving dot demon dot nl
* @version 1.0
* @return void
* @param string $dst_img
* @param string $src_img
* @param integer $dst_x
* @param integer $dst_y
* @param integer $src_x
* @param integer $src_y
* @param integer $dst_w
* @param integer $dst_h
* @param integer $src_w
* @param integer $src_h
* @access public
* @desc Rezised/resamples an image. Faster and higher quality than the regular imageCopyResampled
* @example imageCopyResampleBicubic('new_img.png', 'old_img.png', 0, 0, 0, 0, 100, 100, 1000, 1000);
*/
function imageCopyResampleBicubic($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
{
$scaleX = ($src_w - 1) / $dst_w;
$scaleY = ($src_h - 1) / $dst_h;
$scaleX2 = $scaleX / 2.0;
$scaleY2 = $scaleY / 2.0;
$tc = imageIsTrueColor($src_img);
for ($y = $src_y; $y < $src_y + $dst_h; $y++) {
$sY = $y * $scaleY;
$siY = (int) $sY;
$siY2 = (int) $sY + $scaleY2;
for ($x = $src_x; $x < $src_x + $dst_w; $x++) {
$sX = $x * $scaleX;
$siX = (int) $sX;
$siX2 = (int) $sX + $scaleX2;
if ($tc) {
$c1 = imageColorAt($src_img, $siX, $siY2);
$c2 = imageColorAt($src_img, $siX, $siY);
$c3 = imageColorAt($src_img, $siX2, $siY2);
$c4 = imageColorAt($src_img, $siX2, $siY);
$r = (($c1 + $c2 + $c3 + $c4) >> 2) & 0xFF0000;
$g = ((($c1 & 0xFF00) + ($c2 & 0xFF00) + ($c3 & 0xFF00) + ($c4 & 0xFF00)) >> 2) & 0xFF00;
$b = ((($c1 & 0xFF) + ($c2 & 0xFF) + ($c3 & 0xFF) + ($c4 & 0xFF)) >> 2);
imageSetPixel($dst_img, $dst_x + $x - $src_x, $dst_y + $y - $src_y, $r+$g+$b);
} else {
$c1 = imageColorsForIndex($src_img, imageColorAt($src_img, $siX, $siY2));
$c2 = imageColorsForIndex($src_img, imageColorAt($src_img, $siX, $siY));
$c3 = imageColorsForIndex($src_img, imageColorAt($src_img, $siX2, $siY2));
$c4 = imageColorsForIndex($src_img, imageColorAt($src_img, $siX2, $siY));
$r = ($c1['red'] + $c2['red'] + $c3['red'] + $c4['red'] ) << 14;
$g = ($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) << 6;
$b = ($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue'] ) >> 2;
imageSetPixel($dst_img, $dst_x + $x - $src_x, $dst_y + $y - $src_y, $r+$g+$b);
}
}
}
}
?>