note 91796 added to function.imagefilter

From: Date: Thu, 25 Jun 2009 18:11:21 +0000
Subject: note 91796 added to function.imagefilter
Groups: php.notes 
Request: Send a blank email to php-notes+get-157504@lists.php.net to get a copy of this message
Searching for a way to easily change the color of the image, I tried IMG_FILTER_COLORIZE. I was unable to get the quality results I wanted. It turns out PHP's Colorize is the equivalent of Photoshop's "Linear Dodge" layer filter. Hue adjustments have always worked well for me, so I figured I could try with PHP. This function is kind of slow on larger images, but on small images like what I'm using it for, the difference is trivial. The script calculates the ratio or red, to green, to blue in the color provided, then scales the image appropriately... unfortunately, it does it pixel by pixel. Here's a demo and comparison of this function, to photoshop's hue function, to PHP's colorize. http://img146.imageshack.us/img146/3167/imagefilterhuedemo.png <?php function imagefilterhue($im,$r,$g,$b){ $rgb = $r+$g+$b; $col = array($r/$rgb,$b/$rgb,$g/$rgb); $height = imagesy($im); $width = imagesx($im); for($x=0; $x<$width; $x++){ for($y=0; $y<$height; $y++){ $rgb = ImageColorAt($im, $x, $y); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; $newR = $r*$col[0] + $g*$col[1] + $b*$col[2]; $newG = $r*$col[2] + $g*$col[0] + $b*$col[1]; $newB = $r*$col[1] + $g*$col[2] + $b*$col[0]; imagesetpixel($im, $x, $y,imagecolorallocate($im, $newR, $newG, $newB)); } } } header ("Content-type: image/jpeg"); $im = imagecreatefromjpeg("test.jpg"); // Usage: Just as imagefilter(), except with no filtertype. // imagefilterhue(resource $image, int $red, int $green , int $blue) imagefilterhue($im,2,70,188); // The equivalent with colorize, as tested in demo image: imagefilter($im, IMG_FILTER_COLORIZE, 2, 70, 188); imagejpeg($im); ?> ---- Server IP: 208.69.120.35 Probable Submitter: 24.62.65.187 ---- Manual Page -- http://www.php.net/manual/en/function.imagefilter.php Edit -- https://master.php.net/note/edit/91796 Del: integrated -- https://master.php.net/note/delete/91796/integrated Del: useless -- https://master.php.net/note/delete/91796/useless Del: bad code -- https://master.php.net/note/delete/91796/bad+code Del: spam -- https://master.php.net/note/delete/91796/spam Del: non-english -- https://master.php.net/note/delete/91796/non-english Del: in docs -- https://master.php.net/note/delete/91796/in+docs Del: other reasons-- https://master.php.net/note/delete/91796 Reject -- https://master.php.net/note/reject/91796 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#157504) next »