note 20905 deleted from function.imagecopyresized by aidan

From: Date: Mon, 04 Oct 2004 09:40:35 +0000
Subject: note 20905 deleted from function.imagecopyresized by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-77927@lists.php.net to get a copy of this message
Note Submitter: rze at counter-strike dot net ---- I have added various optimization updates to my PHP bicubic function, along with a few fixes. Thanks to scott@pawprint.net for pointing out that src_x/y were being factored into the placement on the dst_img. function ImageCopyResampleBicubic (&$dst_img, &$src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) /* port to PHP by John Jensen July 10 2001 (updated 4/21/02) -- original code (in C, for the PHP GD Module) by jernberg@fairytale.se */ { $palsize = ImageColorsTotal ($src_img); for ($i = 0; $i < $palsize; $i++) { // get palette. $colors = ImageColorsForIndex ($src_img, $i); ImageColorAllocate ($dst_img, $colors['red'], $colors['green'], $colors['blue']); } $scaleX = ($src_w - 1) / $dst_w; $scaleY = ($src_h - 1) / $dst_h; $scaleX2 = (int) ($scaleX / 2); $scaleY2 = (int) ($scaleY / 2); for ($j = $src_y; $j < $dst_h; $j++) { $sY = (int) ($j * $scaleY); $y13 = $sY + $scaleY2; for ($i = $src_x; $i < $dst_w; $i++) { $sX = (int) ($i * $scaleX); $x34 = $sX + $scaleX2; $color1 = ImageColorsForIndex ($src_img, ImageColorAt ($src_img, $sX, $y13)); $color2 = ImageColorsForIndex ($src_img, ImageColorAt ($src_img, $sX, $sY)); $color3 = ImageColorsForIndex ($src_img, ImageColorAt ($src_img, $x34, $y13)); $color4 = ImageColorsForIndex ($src_img, ImageColorAt ($src_img, $x34, $sY)); $red = ($color1['red'] + $color2['red'] + $color3['red'] + $color4['red']) / 4; $green = ($color1['green'] + $color2['green'] + $color3['green'] + $color4['green']) / 4; $blue = ($color1['blue'] + $color2['blue'] + $color3['blue'] + $color4['blue']) / 4; ImageSetPixel ($dst_img, $i + $dst_x - $src_x, $j + $dst_y - $src_y, ImageColorClosest ($dst_img, $red, $green, $blue)); } } } It saves a few trivial computations, but in a function like this, it might actually save a few milliseconds off the exec time. Either way, the palette and src_x/y fixes make the update worth using.

« previous php.notes (#77927) next »