Palette Limitations - Page 19
January 26, 2001
This is far better, but although the horrible white block has
been removed from around the pin you may notice that the head of
the pin is the wrong color. This is due to the relatively small
number of colors available in the color table. As pixels are
copied from one image to another, we can't use the same color
indexes between images. Images may have different color tables
and the color that we see on the screen for the index in the
source image is not always the same color in the corresponding
color index in the destination image.
For this reason when we use the ImageCopyResized()
function a number of things happen in the background. The first
thing that happens is the ImageColorExact() function
is used to try and find an exact RGB color match in the
destination image. If no match is found,
ImageColorAllocate() is used to try and allocate the
color for the destination image. If this also fails the
ImageColorClosest() function is used to find the
closest approximate color in the destination image.
Because our pin head is not appearing in the red that we would
expect, we can assume that the ImageCopyResized()
function has had to use the closest color that it could find. So
why can it not allocate a nice, bright red in the destination
image?
If we turn to the documentation for the gd libraries
themselves, we find that gdImageColorAllocate (as
it's called in the library) will fail if all 256 colors have
already been allocated.
The following piece of code uses the
ImageColorsTotal() function to display the number of
colors in a given image's palette:
<?php
//color_count.php
$image = ImageCreateFromJPEG("island.jpg");
echo ImageColorsTotal($image);
ImageDestroy($image);
?>
This will return a value of 256, explaining why
ImageColorAllocate() fails and resorts to the
closest available color. The JPEG format itself is not restricted
to 256 colors, but the current version of the gd
libraries only supports this many colors in a single image
palette.
We saw earlier that with an indexed color image we could specify
the number of colors in its palette. If we were to save our image
of the island as a PNG file with 128 colors, we would have 128
colors available, and therefore not impact as heavily on the
quality of the image:
<?php
//map2.php
Header("Content-type: image/png");
$image = ImageCreateFromPNG("island.png");
$icon = ImageCreateFromPNG("pin.png");
$trans = ImageColorAt($icon,0,0);
ImageColorTransparent($icon,$trans);
$width = ImageSX($icon);
$height = ImageSY($icon);
ImageCopyResized($image,$icon,174,200,0,
0,$width,$height,$width,$height);
ImagePNG($image);
ImageDestroy($image);
?>
[Lines 11 and 12 above are one line. They have been split for formatting purposes.]
Summary
During the course of this chapter we looked at how to create,
open, manipulate and output images with PHP. This can enable you
to add a new dimension to the scripts and web pages that you are
already creating.
We have seen how these tools can be used in a practical and
meaningful way to add value to the content in a web site. Now,
not only can it have good looking graphics, but those graphics
can engage visitors to your site by making them relate directly
to a topic that interests them.
We have looked at some background information on graphics, and
seen a little of their internal workings. Insight like this is
essential if you want to really understand what's happening when
you use the PHP's image functions.
The ImageColorTransparent() Function - Page 18
Beginning PHP4
|