Imagecolorallocate() only 256 colors! What if I want more?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gth759k
Forum Commoner
Posts: 76
Joined: Mon Jun 15, 2009 3:04 am

Imagecolorallocate() only 256 colors! What if I want more?

Post by gth759k »

So I've been working with imagecolorallocate() for a while now. I've made a few really helpful scripts that allow me to make just one image in photoshop, for instance an image used for a tab in a website, and then I can specify a hex color and I can make my tab image show up in the browser as any color I want without going back to photoshop. This script checks to see if a color is already allocated and only allocates a new color if it hasn't already been allocated yet like this:

Code: Select all

$color = imagecolorexact($im, $r, $g, $b);
if($color==-1) {
	$color = imagecolorallocate($im, $r, $g, $b);
}
Now I'm working on a much bigger project and I need to allocate 16.7 million colors. Can you create more than one color palette for a single image? I would need like 3600 color palettes for a 720p image. :( Would I have to allocate 256 colors and then save them somehow? Like, maybe I could break the image up into 16x16 px squares and then recombine the pieces. Any help would be appreciated. Thanks.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Imagecolorallocate() only 256 colors! What if I want mor

Post by mikosiko »

Maybe this can help you:

taken from the ImageCreateTrueColor function manual.

"ImageCreateTrueColor is used to create an image resource with an unlimited number of colors, which is useful when manipulating JPEG images, for example.

ImageCreate is used to create an image resource with a 256 color palette, sometimes called indexed color.

In previous versions, ImageCreate worked OK for manipulating JPEG images. But in experimenting with the Win32 version of PHP 4.0.6, which I think relies on the GD 2.0.1 lib beta, you have to use ImageCreateTrueColor to get accurate color results with JPEGs."
gth759k
Forum Commoner
Posts: 76
Joined: Mon Jun 15, 2009 3:04 am

Re: Imagecolorallocate() only 256 colors! What if I want mor

Post by gth759k »

That's what I needed! Thank you!
Post Reply