note 101159 added to function.imagefilter
| From: | jonathandotgottiatgmaildotcom at osu1 dot php dot net | Date: | Tue, 30 Nov 2010 14:38:06 +0000 |
| Subject: | note 101159 added to function.imagefilter | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-174475@lists.php.net to get a copy of this message | ||
IMG_FILTER_COLORIZE doesn't seem to work on palette image, here's a way to achieve same
result with palette image:
<?php
//$color is an array containing rvb infos (ie: array(255,80,0))
function paletteColorize($imgResource,array $color){
$nbColors = imagecolorstotal($imgResource);
for($i=0; $i<$nbColors; $i++){
$c = array_values(imagecolorsforindex($imgRes,$i));
for($y=0;$y<3;$y++)
$c[$y] = max(0,min(255,$c[$y]+$color[$y]));
imagecolorset($imgResource,$i,$c[0],$c[1],$c[2]);
}
}
?>
Here's also a function that work on both truecolor and palette images that try to do something
similar to greyscale with a given color
<?php
function colorScale($imgRes,array $color){
imagefilter($imgRes,IMG_FILTER_GRAYSCALE);
$color = self::_read_color($color);
$luminance=($color[0]+$color[1]+$color[2])/3; // average luminance added by the color
$brightnessCorrection = $luminance/3; // quantity of brightness to correct for each channel
if( $luminance < 127 ){
$brightnessCorrection -= 127/3; // color is dark so we have to negate the brightness correction
}
if(! imageistruecolor($imgRes) ){
$nbColors = imagecolorstotal($imgRes);
for($i=0; $i<$nbColors; $i++){
$c = array_values(imgagecolorsforindex($imgRes,$i));
for($y=0;$y<3;$y++){
$c[$y] = max(0, min(255, $c[$y] + ($color[$y]-$luminance) + $brightnessCorrection) ); //
parentheses just for better comprehension
}
imagecolorset($omgRes,$i,$c[0],$c[1],$c[2]);
}
}else{ // much easier with truecolor
imagefilter($imgRes, IMG_FILTER_COLORIZE, $color[0]-$luminance, $color[1]-$luminance,
$color[2]-$luminance);
imagefilter($imgRes, IMG_FILTER_BRIGHTNESS, $brightnessCorrection);
}
}
?>
with hope that someone will find this useful
----
Server IP: 195.221.21.36
Probable Submitter: 217.128.159.36
----
Manual Page -- http://www.php.net/manual/en/function.imagefilter.php
Edit -- https://master.php.net/note/edit/101159
Del: integrated -- https://master.php.net/note/delete/101159/integrated
Del: useless -- https://master.php.net/note/delete/101159/useless
Del: bad code -- https://master.php.net/note/delete/101159/bad+code
Del: spam -- https://master.php.net/note/delete/101159/spam
Del: non-english -- https://master.php.net/note/delete/101159/non-english
Del: in docs -- https://master.php.net/note/delete/101159/in+docs
Del: other reasons-- https://master.php.net/note/delete/101159
Reject -- https://master.php.net/note/reject/101159
Search -- https://master.php.net/manage/user-notes.php