note 72606 added to function.imagecopyresampled
| From: | tim at leethost dot com | Date: | Thu, 25 Jan 2007 22:00:35 +0000 |
| Subject: | note 72606 added to function.imagecopyresampled | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-122204@lists.php.net to get a copy of this message | ||
Here's a simple plug-and-play replacement to imagecopyresampled that will deliver results that
are almost identical except MUCH faster (very typically 30 times faster). I've been using this
function for a few years but figured everyone could benefit from it.
For example, a 10 megapixel camera image (3872x2592 resolution) converted to thumbnail size using
the default quality is 60 times faster than using imagecopyresampled (only 0.15 seconds from 8.9
seconds) and the image quality is almost identical. There's also an optional
"quality" parameter that allows you to select the quality you want (1 is lowest quality
but fastest, 5 is highest quality but slowest, 3 is default). All existing imagecopyresampled
parameters fully work so it's truly plug-and-play. It also automatically decides if it would
be better to just use imagecopyresampled depending on source and destination image sizes.
My image analysis shows imagecopyresized renders on average 2.4% of pixels significantly different
than imagecopyresampled. These significantly different pixels give imagecopyresized the jagged,
blocky, aliasing look. Using fastimagecopyresampled with a quality of 2, the significantly
different pixel rate is reduced to 0.8%. With a quality of 3 (the default) it's reduced to
0.5% and at a quality of 4 it's reduced to only 0.03% but is still very typically 10-15 times
faster for thumbnail creation.
Enjoy!
<?
function fastimagecopyresampled (&$dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y,
$dst_w, $dst_h, $src_w, $src_h, $quality = 3) {
// Plug-and-Play fastimagecopyresampled function replaces much slower imagecopyresampled.
// Just include this function and change all "imagecopyresampled" references to
"fastimagecopyresampled".
// Typically from 30 to 60 times faster when reducing high resolution images down to thumbnail
size using the default quality setting.
// Author: Tim Eckel - Date: 12/17/04 - Project: FreeRingers.net - Freely distributable.
//
// Optional "quality" parameter (defaults is 3). Fractional values are allowed, for
example 1.5.
// 1 = Up to 600 times faster. Poor results, just uses imagecopyresized but removes black edges.
// 2 = Up to 95 times faster. Images may appear too sharp, some people may prefer it.
// 3 = Up to 60 times faster. Will give high quality smooth results very close to
imagecopyresampled.
// 4 = Up to 25 times faster. Almost identical to imagecopyresampled for most images.
// 5 = No speedup. Just uses imagecopyresampled, highest quality but no advantage over
imagecopyresampled.
if (empty($src_image) || empty($dst_image)) { return false; }
if ($quality <= 1) {
$temp = imagecreatetruecolor ($dst_w + 1, $dst_h + 1);
imagecopyresized ($temp, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w + 1, $dst_h + 1,
$src_w, $src_h);
imagecopyresized ($dst_image, $temp, 0, 0, 0, 0, $dst_w, $dst_h, $dst_w, $dst_h);
imagedestroy ($temp);
} elseif ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) <
$src_h)) {
$tmp_w = $dst_w * $quality;
$tmp_h = $dst_h * $quality;
$temp = imagecreatetruecolor ($tmp_w + 1, $tmp_h + 1);
imagecopyresized ($temp, $src_image, $dst_x * $quality, $dst_y * $quality, $src_x, $src_y,
$tmp_w + 1, $tmp_h + 1, $src_w, $src_h);
imagecopyresampled ($dst_image, $temp, 0, 0, 0, 0, $dst_w, $dst_h, $tmp_w, $tmp_h);
imagedestroy ($temp);
} else {
imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h,
$src_w, $src_h);
}
return true;
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 72.240.221.30
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php
Edit -- https://master.php.net/note/edit/72606
Del: integrated -- https://master.php.net/note/delete/72606/integrated
Del: useless -- https://master.php.net/note/delete/72606/useless
Del: bad code -- https://master.php.net/note/delete/72606/bad+code
Del: spam -- https://master.php.net/note/delete/72606/spam
Del: non-english -- https://master.php.net/note/delete/72606/non-english
Del: in docs -- https://master.php.net/note/delete/72606/in+docs
Del: other reasons-- https://master.php.net/note/delete/72606
Reject -- https://master.php.net/note/reject/72606
Search -- https://master.php.net/manage/user-notes.php