Jump to content

Update Image Colors?


Perad

Recommended Posts

Hi,

 

I wrote the function below. It switches out one color for another. The pictures going in are css sprites with various colors and a white background.

 

So.. sprite 1 might by blue and sprite 2 might be green.

 

The function would be run twice to replace the blue + green with whatever colors were required.

 

/**
 * Changes the color of a graphic.
 * $settings = array (
 *  'icon'
 *  'new_icon'
 *  'old_color' = array
 *  'new_color' = array
 * );
*/
function updateIconColor($settings=array()) {
	// Create Image
	$image = imagecreatefrompng($settings['icon']);

	// Convert True color image to a palatte
	imagetruecolortopalette($image, false, 255);

	// Restore Alpha
	$white = imagecolorclosest($image, 255, 255, 255);
	imagecolortransparent($image, $white);

	// Find + Set color
	$index = imagecolorclosest($image, $settings['old_color'][0],$settings['old_color'][1],$settings['old_color'][2]);
	imagecolorset($image, $index, $settings['new_color'][0], $settings['new_color'][1], $settings['new_color'][2]);

	// Restore Alpha
	imageAlphaBlending($image, true);
	imageSaveAlpha($image, true);

	// Save
	imagepng($image, $settings['new_icon']); // save image as gif
	imagedestroy($image);
}

 

How could this be updated to allow some dithering (is that what it is called?) - the thing that smooths out an image to avoid pixelation around the edges?

 

Minor question.

1) Is there any reason why imagecolorexact doesn't work in this function. When I use imagecolorexact nothing happens and the picture remains the same.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.