Related Examples ( Source code ) » imagecolortransparent

Code Examples / Notes » imagecolortransparent

fred

Wow! I am actually surprized there are no functions to retrieve transparency information from the image. I assume I'm not missing anything since there are all kinds of notes about how to get around this limitation. At any rate, here is some code to read the gif file and retrieve the missing information. The code will speak volumes.
<?php
class GifInfo
{
var $m_transparentRed;
var $m_transparentGreen;
var $m_transparentBlue;
var $m_signature;
var $m_version;
var $m_width;
var $m_height;
var $m_width;
var $m_height;
var $m_colorFlag;
var $m_backgroundIndex;

When you use palette images (created with imagecreate()), the first color allocated is the background color. This color cannot be used for transparency. So if you want to make the background transparent, first allocate a dummy background color, then allocate the real background color and declare this is as transparent.

Only one color may be transparent in one image. The last call to imagecolortransparent will be the color that is set to transparent.
I am processing button images that have a slightly different fill color than the background color outside the border of the button. I was hoping that I could just make both of those colors transparent and solve the problem.
Hope this tidbit of info will save you some time.

calar

ok some works...
i had a test-sytem with GDLib, phpinfo shows me this version:
"GD Version 2.0 or higher" so far no porblems with: imageColorTransparent
Then we had to copy ouer codes to another sever on this server phpinfo shots me version:
"GD Version bundled (2.0.12 compatible)"
The jpg was wrong and nothing was transparent.
The point is, you have to use: imagecopymerge
Don't if its true on any system just work on
SUSE 8.2
PHP 4.3.2

shelley_hina

It took me a while to figure this out. I added visual web thumbnail preview graphic from http://www.thumbshots.org and http://www.thumbshots.com to my customer sites to visualize web pages. Then I needed to create a small blank transparent PNG image if the thumbshot is not available. You can do this easily using imagecolortransparent function. It came out really nice and fast. It's very cool stuff and highly recommended library for all your web site needs!
// Use GD library to create transparent color PNG image of the pixel size
$ImgGD = imagecreate(120, 90);
$ImgWhite = imagecolorallocate($ImgGD, 255, 255, 255);
imagefill($ImgGD, 0, 0, $ImgWhite);
imagecolortransparent($ImgGD, $ImgWhite);
// Capture STDOUT to buffer
ob_start();
imagepng($ImgGD);
$Image = ob_get_contents();
ob_end_clean();
// Remember to free your memory
imagedestroy($ImgGD);
// Print image to web browser.
print $Image

In response to the note above...
ImageColorTransparent() does work on TrueColor images. I have it working myself but I do remember having to add an extra step somewhere. Anyways, here is how I overlay text on a jpeg. Don't forget, you need GD 2.x for true color to work. This example requires TTF support also.
//
// get src image and dimensions
$src_img = ImageCreateFromJPEG("foo.jpg");
$src_w = ImageSX($src_img);
$src_h = ImageSY($src_img);
//
// create empty true color canvas, def bg is black
$txt_img = ImageCreateTrueColor($src_w,$src_h);
//
// define colors
$white = ImageColorAllocate ($txt_img, 255, 255, 255);
$black = ImageColorAllocate ($txt_img, 0, 0, 0);
//
// add your white text
ImageTTFText ($txt_img, 80, 25, 65, 350, $white, "impact.ttf", "Kung Fu");
//
// make black bg transparent
ImageColorTransparent($txt_img, $black);
//
// merge text image onto src image
ImageCopyMerge($src_img,$txt_img,0,0,0,0,$src_w,$src_h,30); // 30% opacity
'Now' you can do resizing or whatever and output it. You need to create yet another blank image to resample. (Actually you can resize the image before the text overlay step with some small mods, but its REALLY messy if you want the output size to be dynamic)
//
// create empty true color canvas for resizing.
$dst_img = ImageCreateTrueColor(100,100);
//
// resize
ImageCopyResampled($dst_img,$src_img,0,0,0,0,100,100,$src_w,$src_h);
//
// output to browser.
ImageJPEG($dst_img,'',70); // quality 70
If someone could tell me a shorter way to do this, that would be sweet. The way it is now, it just feels like there is one too many steps to get this done. But I am glad it works at all!

glenn

in reference to webmaster at webnetwizard dotco dotuk who had a rather complicated method of determining if a GIF had any transparency set ...
imagecolortransparent will return -1 if no transparency is found.
eg:
$transColorIndexMain = imageColorTransparent($mainImgObj);
if ($transColorIndexMain >= 0 ) {
# GIF has transparency ... ;
}
Current PHP Version: 4.4.4
[GD Version] => bundled (2.0.28 compatible)

php_comment et 5mm de

In fact this function can be used for two purposes:
A.) to work with transparency while dealing with images in php (see watermark example)
B.) to create a partially transparent picture
For A.) one can work with truecolor images with some regulations, but B.) can only be used with palette-based images (= indexed = created with imagecreate not imagecreatetruecolor) and formats that support transparency (png, gif).
For instance if you want to cut out a color out of a give picture that is in truecolor, use the following method to first convert it to palette-based image, assign the transparency and give it to the browser as png:
<?
$img = imagecreatefromjpeg('test.jpg');
imagetruecolortopalette($img, false, 256); // convert
$white = imagecolorresolve($img, 255, 255, 255); // resolve given palette entry
imagecolortransparent($img, $white);
header("Content-type: image/png");
imagepng($img);
?>

cladiniz

If you have a transparent PNG , in PHP 4.3.1 , GD 2.0 , you don't need to set the transparency anymore , it's automatic ...

I wanted to have non-rectangular images being displayed on various website-backgrounds. piece of cake with png-24 and sane browsers, but not with IE. GIF-files are useless since they tend to have ugly edges.
So that's what i came up with, a script to merge a given PNG-24 against a background-image. The script only merges areas of partial transparency (think anti-aliased edges) of the PNG onto the background-image, leaving 100% transparent areas as they are. output is a 256-color gif with transparency.
<?PHP
function alphaishMerge(&$img_unten, &$img_oben, &$transCol) {
$sx = imagesx($img_unten);
$sy = imagesy($img_oben);

Hi!
I'm using GDLib 1.6.3 so far I know.
You can have transparency on any defined color on any image type when using the imagecolorclosest($im, $R, $G, $B) function instead of imagecolorallocate() or imagecolorexact().

After much devious mindbending, I have found a way to test any GIF for presence of background transparency. This ability is essential for my application which uploads any of GIF, JPEG or PNG and simultaneously creates a thumbnail of identical image type and identical filename (full size and thumbnail versions being stored in different folders).
After uploading and moving the image in the usual way, a switch($image_type) statement ensures that the optimum code is used to generate the thumbnail; regardless of image type.
The problem with the GIF type is that those with transparent backgrounds need to be treated differently to those without. When I don't detect GIF transparency, I either end up with all transparent GIF's having black backgrounds, or all GIF's get converted to transparent background - even if they weren't transparent in the original.
But how to detect transparency in the original? It finally occurred to me that I could test for transparency programmatically by overlaying a copy of the original image over an all-black image, record the color value at particular pixel locations and then repeat the process by overlaying a copy of the original image over an all-white image, recording the color values at identical pixel locations and comparing these with the first set of values.
If the two sets of values correlate exactly, and if sensible sampling points are used, the image can be treated as non-transparent. If the two sets of values show differences, the image should be treated as having a transparent background.

joachim kruyswijk

A negative color number will turn antialiasing off, but even a positive number chosen for transparency may affect antialiasing. If the text (created with imagettftext) in your image looks horrible, try using a higher color number.