note 33613 deleted from function.imagecopyresampled by aidan
| From: | aidan@php.net | Date: | Mon, 04 Oct 2004 09:44:37 +0000 |
| Subject: | note 33613 deleted from function.imagecopyresampled by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-77959@lists.php.net to get a copy of this message | ||
Note Submitter: nanobot at chipx86 dot com
----
imagecopyresampled will shrink an image smoothly, but if you try to use it to enlarge an image, the
result will be blocky, like in imagecopyresized. The following function will enlarge an image more
smoothly using bilinear resampling.
function imagecopyenlarged ( $dst_im , $src_im , $dstX , $dstY , $srcX , $srcY , $dstW , $dstH ,
$srcW , $srcH )
{
for ( $y = $dstY ; $y < $dstH ; $y ++ )
{
$tempy = ( $y - $dstY ) * $srcH / $dstH + $srcY;
$tempyd = $tempy - floor ( $tempy );
$tempyu = 1 - $tempyd;
$tempy = floor ( $tempy );
for ( $x = $dstX ; $x < $dstW ; $x ++ )
{
$tempx = ( $x - $dstX ) * $srcW / $dstW + $srcX;
$tempxr = $tempx - floor ( $tempx );
$tempxl = 1 - $tempxr;
$tempx = floor ( $tempx );
$tempq = imagecolorsforindex ( $src_im , imagecolorat ( $src_im , $tempx , $tempy ) );
$tempw = imagecolorsforindex ( $src_im , imagecolorat ( $src_im , $tempx + 1 , $tempy ) );
$tempa = imagecolorsforindex ( $src_im , imagecolorat ( $src_im , $tempx , $tempy + 1 ) );
$temps = imagecolorsforindex ( $src_im , imagecolorat ( $src_im , $tempx + 1 , $tempy + 1 ) );
$tempq['red'] = $tempq['red'] * $tempxl + $tempw['red'] * $tempxr;
$tempq['green'] = $tempq['green'] * $tempxl + $tempw['green'] *
$tempxr;
$tempq['blue'] = $tempq['blue'] * $tempxl + $tempw['blue'] *
$tempxr;
$tempa['red'] = $tempa['red'] * $tempxl + $temps['red'] * $tempxr;
$tempa['green'] = $tempa['green'] * $tempxl + $temps['green'] *
$tempxr;
$tempa['blue'] = $tempa['blue'] * $tempxl + $temps['blue'] *
$tempxr;
$tempq['red'] = $tempq['red'] * $tempyu + $tempa['red'] * $tempyd;
$tempq['green'] = $tempq['green'] * $tempyu + $tempa['green'] *
$tempyd;
$tempq['blue'] = $tempq['blue'] * $tempyu + $tempa['blue'] *
$tempyd;
imagesetpixel ( $dst_im , $x , $y , imagecolorallocate ( $dst_im , $tempq['red'] ,
$tempq['green'] , $tempq['blue'] ) );
}
}
}
Note that this function is only for enlarging an image. Depending on the application, you may need
to have your script detect whether it will be enlarging or shrinking the image, and use the
respective function.