note 81252 added to function.imagecolorset

From: Date: Wed, 20 Feb 2008 09:08:33 +0000
Subject: note 81252 added to function.imagecolorset
Groups: php.notes 
Request: Send a blank email to php-notes+get-136299@lists.php.net to get a copy of this message
I've been looking for a simple function which "colorizes" an image, without any success ... it looks like a lot of people mean something different with "colorizing" an image, because actually colorzing means multiplying the old color's grayscale with the new color. So a white pixel would become 100% of the colorize-to-color and a black pixel would stay black (I know I can't explain well ... I hope you understood, otherwise take a look at the example below the functions' code). <?php function image_colorize(&$img,$rgb) { imageTrueColorToPalette($img,true,256); $numColors = imageColorsTotal($img); for ($x = 0; $x < $numColors; $x++) { list($r,$g,$b) = array_values(imageColorsForIndex($img,$x)); // calculate grayscale in percent $grayscale = ($r + $g + $b) / 3 / 0xff; imageColorSet($img,$x, $grayscale * $rgb[0], $grayscale * $rgb[1], $grayscale * $rgb[2] ); } return true; } ?> Example of usage: <?php $color = array(0xff,0xaa,0x2a); // color to convert to $url = 'http://sundog.net/images/uploads/1_google_logo.jpg'; $img = imageCreateFromJpeg($url); image_colorize($img,$color); header('Content-type: image/gif'); imageGif($img); exit; ?> Enjoy ---- Server IP: 194.153.94.10 Probable Submitter: 84.75.181.162 ---- Manual Page -- http://www.php.net/manual/en/function.imagecolorset.php Edit -- https://master.php.net/note/edit/81252 Del: integrated -- https://master.php.net/note/delete/81252/integrated Del: useless -- https://master.php.net/note/delete/81252/useless Del: bad code -- https://master.php.net/note/delete/81252/bad+code Del: spam -- https://master.php.net/note/delete/81252/spam Del: non-english -- https://master.php.net/note/delete/81252/non-english Del: in docs -- https://master.php.net/note/delete/81252/in+docs Del: other reasons-- https://master.php.net/note/delete/81252 Reject -- https://master.php.net/note/reject/81252 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#136299) next »