PHP Snippets
imagecopyresamplebicubic
Information
(PHP Version >= 4.0.1)Functions used in this snippet: imagepalettecopy, round, imagecolorsforindex, imagecolorat, imagesetpixel, imagecolorclosest.
Description
int imagecopyresamplebicubic( int &dst_img, int &src_img, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )
This function will resize images using a Bicubic method. This makes the resized images look fabulous, but as a result is extremely slow. It takes the same arguements as imagecopyresized.
I must note that I did NOT write this function. I found it on php.net at http://www.php.net/manual/en/function.imagecopyresampled.php#44490
Snippet
<?php
function imagecopyresamplebicubic(&$dst_img, &$src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
{
imagepalettecopy($dst_img, $src_img);
$rX = $src_w / $dst_w;
$rY = $src_h / $dst_h;
$w = 0;
for($y = $dst_y; $y < $dst_h; $y++)
{
$ow = $w; $w = round(($y + 1) * $rY);
$t = 0;
for($x = $dst_x; $x < $dst_w; $x++)
{
$r = $g = $b = 0; $a = 0;
$ot = $t; $t = round(($x + 1) * $rX);
for($u = 0; $u < ($w - $ow); $u++)
{
for($p = 0; $p < ($t - $ot); $p++)
{
$c = imagecolorsforindex($src_img, imagecolorat($src_img, $ot + $p, $ow + $u));
$r += $c['red'];
$g += $c['green'];
$b += $c['blue'];
$a++;
}
}
imagesetpixel($dst_img, $x, $y, imagecolorclosest($dst_img, $r / $a, $g / $a, $b / $a));
}
}
}
?>
Example
imagecopyresamplebicubic Can be used in the following way:
<?php
$img = imagecreatefrompng('someimage'); // image is 750x750
$dest = imagecreatetruecolor(100, 100); // create an empty image
imagecopyresamplebicubic($dest, $img, 0, 0, 0, 0, 100, 100, 750, 750);
header('Content-type: image/png');
imagepng($dest);
?>
0 Responses to imagecopyresamplebicubic