I was just reading Logan's Blog about OpenCV and using basic transformation filters on images and I remembered doing a color filter on favicon images for websites.
First, I created a grayscale png image saved in RGB mode and named it favicon.png. This is the base image used to perform transformations on. However, I do not use it as the default, non-colored images, because it has to be much darker than I would like. When I want the favicon to be a certain color, to indicate a certain context on the page, I call favicon.php. (In reality, I use a basic MVC that uses mod-rewrite to hide the .php extension and to call the image as a controller, but here, I am translating that to regular $_GET[] style)
And here is the favicon.php file. It has to output the correct header, so that the browser sees it as an image instead of HTML. The I use the php GD functions to transform the colors. Using the imagealphablending($im, false);
and
imagesavealpha($im, true)
allows the transparency of the original image to be preserved
header('Content-type: image/png');
$im = imagecreatefrompng('images/favico.png');
imagealphablending($im, false);
imagesavealpha($im, true);
$red = hexdec(substr($_GET['color'],0,2));
$green = hexdec(substr($_GET['color'],2,2));
$blue = hexdec(substr($_GET['color'],4,2));
imagefilter($im, IMG_FILTER_COLORIZE, $red, $green, $blue);
imagepng($im);
Here is the favicon.png image before processing![]()
Here is an image after transformation by php using 009900 as the color; called with
<link rel="shortcut icon" href="/favicon.php?color=009900" />
![]()
The results are somewhat grainy due to no anti-aliasing cleaning up the edges, but when it is scaled down, it looks nice


No comments:
Post a Comment