3800x2500px - 2s
3807x6768 - 4.7s
Tested on a core2duo e4300/1gb ram - only one core used.
It reduces first with nearest-neighbour and then with
bicubic on the smaller image.
Thumb quality is fine
<?php
$cale_in = "test.jpg"; //input-file
$cale_out = "test_thumb.jpg"; //output-file
$make_thumbs = "YES";
$thumbsize[0] = 100;
$thumbsize[1] = 75;
ini_set("memory_limit", "134217728"); //
needed for huge pictures
// Bicubic resampling, not written by me
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));
}
}
}
function makeThumb($cale_in) {
global $thumbsize;
$sursa = imagecreatefromjpeg($cale_in);
$is_jpeg = getimagesize($cale_in);
$ws = $is_jpeg[0];
$hs = $is_jpeg[1];
if($ws > $thumbsize[0] && $hs >
$thumbsize[1])
{
$aspect = $ws/$hs;
if($aspect <= 1.333333) {
$hd = $thumbsize[1];
$wd = floor($hd*$aspect);
}
else {
$wd = $thumbsize[0];
$hd = floor($wd/$aspect);
}
$Z =
ceil(log(($ws*$hs)/(4*$thumbsize[0]*$thumbsize[1])))+1;
if(log(($ws*$hs)/(4*$thumbsize[0]*$thumbsize[1])) < 0)
$Z=1;
$dx = $dy = 0;
if($Z > 1) {
$dest = imagecreatetruecolor(round($ws/$Z),
round($hs/$Z));
for($i=0; $i < $hs; $i+=$Z) {
for($j=0; $j < $ws; $j+=$Z) {
$rgb = imagecolorat($sursa, $j, $i);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$pcol = imagecolorallocate($dest, $r, $g, $b);
imagesetpixel($dest, $dx, $dy, $pcol);
$dx++;
}
$dx=0;
$dy++;
}
}
else
{
$dest = imagecreatetruecolor($ws, $hs);
imagecopy($dest, $sursa, 0, 0, 0, 0, $ws, $hs );
}
imagedestroy($sursa);
$destrs = imagecreatetruecolor($wd, $hd);
ImageCopyResampleBicubic($destrs,$dest,0,0,0,0,
$wd,$hd,round($ws/$Z),round($hs/$Z));
ImageJpeg($destrs, $cale_out, 100);
echo "Z:$Z <b>|</b> ($ws x $hs) ->
($wd x $hd) ".$ws/$hs;
}
}
function mf() {
list($usec, $sec) = explode(" ", microtime());
$x[0] = $sec; $x[1] = $usec;
return $x;
}
$timp1 = mf(); //starting to count
makeThumb($cale_in); //makes the actual thumb
$timp2 = mf(); //timer ends
$dsec = $timp2[0] - $timp1[0];
$dusec = $timp2[1] - $timp1[1];
echo "<b>|</b> Time :
<b>".round($dusec+$dsec,6)."s</b>
<b>|</b><br />";
?>
----
Server IP: 69.147.83.197
Probable Submitter: 89.123.58.127
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresam
pled.php
Edit -- https://master
.php.net/note/edit/78049
Del: integrated -- h
ttps://master.php.net/note/delete/78049/integrated
Del: useless -- http
s://master.php.net/note/delete/78049/useless
Del: bad code -- htt
ps://master.php.net/note/delete/78049/bad+code
Del: spam -- https:/
/master.php.net/note/delete/78049/spam
Del: non-english --
https://master.php.net/note/delete/78049/non-english
Del: in docs -- http
s://master.php.net/note/delete/78049/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/78049
Reject -- https://mast
er.php.net/note/reject/78049
Search -- https://
master.php.net/manage/user-notes.php
--
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php
|