PHP: imagecolortransparent - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

imageconvolution imagecolorstotal

Last updated: Fri, 26 Jan 2007

view this page in

# imagecolortransparent

(PHP 3, PHP 4, PHP 5)

imagecolortransparent -- Define a color as transparent

## Описание

int imagecolortransparent ( resource image [, int color] )

Sets the transparent color in the given `image`.

## Список параметров

`image`

  An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().

`color`

  A color identifier created with imagecolorallocate()

## Возвращаемые значения

The identifier of the new (or current, if none is specified) transparent color is returned.

## Примечания

> Замечание: Transparency is copied only with imagecopymerge() and true color images, not with imagecopy() or pallete images.

> Замечание: The transparent color is a property of the image, transparency is not a property of the color. Once you have set a color to be the transparent color, any regions of the image in that color that were drawn previously will be transparent.

add a note User Contributed Notes
imagecolortransparent
cinymini
14-Dec-2006 11:17
`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.`
glenn at bengeweb dot co dot nz
17-Nov-2006 08:03
`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)`
Blaine Ehrhart
07-Jul-2006 02:59
`This script creates transparency for a specific RBG color for an already created GIF image.
<?php
$im = imagecreatefromgif('trans.gif');
$img = imagecreatetruecolor(16,16);
$trans = imagecolorallocate($img,255,99,140);
imagecolortransparent($img,$trans);
imagecopy($img,$im,0,0,0,0,16,16);
imagetruecolortopalette($img, true, 256);
imageinterlace($img);
imagegif($img,'trans.gif');
imagedestroy($img);
?>`
Joachim Kruyswijk
19-Oct-2005 04:20
`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.`
contact at wdschools dot com
19-Oct-2005 09:02
`Transparent GIF w/ text from TTF Font
-------------------------------------
$image = imagecreatetruecolor(192, 36);
$trans_color = imagecolorallocate($image, 255, 0, 0);
$color = imagecolorallocate($image, 255, 255, 255);
imagecolortransparent($image, $trans_color);
imagettftext($image, 10, 0, 10, 23, $color, "impact.ttf", " MENU ITEM");
imagegif($image, "output.gif");
imagedestroy($image);`
ingrid
09-Jun-2005 09:37
`This script should take an animated GIF whit a white background transparancy, put some white=transparant text on it, and can be used in any page using the filename and the normal image tags.
Everything works well, exept the animation. The GIF animation stands still. Don't know how to keep the animation animated. Is it possible in PHP?
<?php
$TextFile = "teller.txt";
$Count = trim(file_get_contents($TextFile));
$FP = fopen($TextFile, "r");
$Count=fgets($FP, 4096);
fclose ($FP);
settype($Count, "integer");
$Count++;
if ($FP = fopen ($TextFile, "w")){
fwrite ($FP, $Count);
fclose ($FP);
}
$image = "blad.gif";
$im = imagecreatefromgif($image);
$white = imageColorAllocate ($im, 255, 255, 255);
$trans = imagecolortransparent($im,$white);
$hit = "$Count";
$ip = $_SERVER["REMOTE_ADDR"];
ImageString($im, 2, 30, 60, " je bent bezoeker ", $white);
ImageString($im, 3, 20, 80, " $ip.$hit", $trans);
header("Content-Type: image/gif");
Imagegif($im,'',100);
ImageDestroy ($im);
?>`
thomas at nindel dot net
20-Apr-2005 10:02
`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);
// just be sure
imagealphablending($img_unten, TRUE);
imagealphablending($img_oben, TRUE);
// imagecopy respects alpha channels
imagecopy($img_unten, $img_oben, 0, 0, 0, 0, $sx, $sy);
for ($x = 0; $x < $sx; $x++) {
for ($y = 0; $y < $sy; $y++) {
// get RGBA of upper image
$pxloben = imagecolorat($img_oben, $x, $y);
$pxloben = imagecolorsforindex($img_oben, $pxloben);
if ($pxloben['alpha'] == 127) {
// if this pixel is fully transparent...
// ...set it to the transparent color
imagesetpixel($img_unten,$x,$y,$transCol);
}
}
}
}
// images have to be the same size
$bg_file = "background.jpg";
$png_file ="kauscha.png";
// background-image (jpg)
$img_bg = imagecreatefromjpeg($bg_file);
// image which is supposed to be put on the background (PNG-24)
$img_png = imagecreatefrompng($png_file);
// This color is the one getting transparent in the gif-file
$transCol = imagecolorallocate($img_bg, 0, 0, 0);
// magic
alphaishMerge($img_bg, $img_png, $transCol);
// reduce color depth
imagetruecolortopalette($img_bg, FALSE, 255);
// make it transparent
$transCol = imagecolortransparent($img_bg, $transCol);
header("Content-type: image/gif");
imagegif($img_bg);
?>
i hope thats useful for anyone.`
partyheld at HATESPAMgmail dot com
31-Mar-2005 09:35
`Fred wrote:
<?php
$file['signatur']= substr($result,0,3);
$file['version']= substr($result,3,3);
$file['width']= ord(substr($result,6,1))+ord(substr($result,7,1))*256;
$file['height']= ord(substr($result,8,1))+ord(substr($rsult,9,1))*256;
$file['flag']= ord(substr($result,10,1))>>7;
$file['trans_red']= ord(substr($result,ord(substr($result,11))*3,1));
$file['trans_green']= ord(substr($result,ord(substr($result,11))*3+1,1));
$file['trans_blue']= ord(substr($result,ord(substr($result,11))*3+2,1)) ;
?>
You should write such code like this:
<?php
$file = array(
"signatur" => substr ($result, 0, 3),
"version" => substr ($result, 3, 3),
"width" => ord (substr ($result, 6, 1)) + ord (substr ($result, 7, 1)) * 256,
"height" => ord (substr ($result, 8, 1)) + ord(substr ($result, 9, 1)) * 256,
"flag" => ord (substr ($result, 10, 1)) >> 7,
"trans_red" => ord (substr ($result, ord (substr ($result, 11)) * 3,1)),
"trans_green" => ord (substr ($result, ord (substr($result, 11)) * 3 + 1, 1)),
"trans_blue" => ord (substr ($result, ord (substr($result, 11)) * 3 + 2, 1))
);
?>
And there's a typo in line 4, sayin "rsult" instead of "result". I've got no time for checking your code any further, sorry.`
raiserle
28-Mar-2005 08:52
`it goes also more simply
i rather work with arrays
<?php
if(!function_exists('gif_info'))
{
function gif_info($filename)
{
$fp= fopen($filename,'rb');
$result= fread($fp,13);
$file['signatur']= substr($result,0,3);
$file['version']= substr($result,3,3);
$file['width']= ord(substr($result,6,1))+ord(substr($result,7,1))*256;
$file['height']= ord(substr($result,8,1))+ord(substr($rsult,9,1))*256;
$file['flag']= ord(substr($result,10,1))>>7;
$file['trans_red']= ord(substr($result,ord(substr($result,11))*3,1));
$file['trans_green']= ord(substr($result,ord(substr($result,11))*3+1,1));
$file['trans_blue']= ord(substr($result,ord(substr($result,11))*3+2,1)) ;
fclose($fp);
return $file;
}
}
print_r(gif_info($_FILES['files']['temp_name']));
?>`
talkback at codesponge dot com
16-Mar-2005 03:41
`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.`
fred at webblake dot net
14-Jan-2005 08:17
`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;
function GifInfo($filename)
{
$fp = fopen($filename,"rb");
$result = fread($fp,13);
$this->m_signature = substr($result,0,3);
$this->m_version = substr($result,3,3);
$this->m_width = ord(substr($result,6,1)) + ord(substr($result,7,1)) * 256;
$this->m_height = ord(substr($result,8,1)) + ord(substr($result,9,1)) * 256;
$this->m_colorFlag = ord(substr($result,10,1)) >> 7;
$this->m_background = ord(substr($result,11));
if($this->m_colorFlag)
{
$tableSizeNeeded = ($this->m_background + 1) * 3;
$result = fread($fp,$tableSizeNeeded);
$this->m_transparentRed = ord(substr($result,$this->m_background * 3,1));
$this->m_transparentGreen = ord(substr($result,$this->m_background * 3 + 1,1));
$this->m_transparentBlue = ord(substr($result,$this->m_background * 3 + 2,1));
}
fclose($fp);
}
}
?>
And to use this class....
<?php
$size = getimagesize($userfile);
$img2 = ImageCreate($w,$h);
$gi = new GifInfo($userfile);
if($gi->m_version == '89a' && $gi->m_colorFlag == 1)
{
$transparent = imagecolorallocate($img2,
$gi->m_transparentRed,
$gi->m_transparentGreen,
$gi->m_transparentBlue);
imagecolortransparent ($img2,$transparent);
}
?>`
webmaster at webnetwizard dotco dotuk
11-Jan-2005 01:24
`I'm feeling particularly altruistic today, so here is the code to accompany the text I submitted about a script that 'intelligently' detects if an uploaded GIF has transparency or not, and creates a thumbnail accordingly:
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
{
$note = "Image was successfully uploaded.\n";
// Now create thumbnail version of the uploaded image and save it to
// folder "$fixPath/images/thumbs". First, we need to create a
// resource "handle" for a copy of the uploaded image
$img_error = false;
list($width, $height, $type) = getimagesize($uploadfile);
switch($type) // Allowed types: image/gif,image/jpeg,image/x-png
{
case 1: // GIF
$src = @imagecreatefromgif($uploadfile);
if (!$src)
$img_error = true;
break;
case 2: // JPEG
.
.
.
case 3: // PNG
.
.
.
default:
$img_error = true;
}
// resource handle for source image now in $src
// Now calculate scaling factor and put in $sf
if(!$img_error)
{
if($width >= $height && $width > 100)
$sf = $width / 100;
elseif($height > 100)
$sf = $height / 100;
else
$sf = 1;
$newwidth = intval($width / $sf);
$newheight = intval($height / $sf);
switch($type) // Allowed types: image/gif,image/jpeg,image/x-png
{
case 1: // GIF
$tpcolor = imagecolorat($src, 0, 0);
// in the real world, you'd better test all four corners, not just one!
$dest = imagecreate($newwidth, $newheight);
// $dest automatically has a black fill...
imagepalettecopy($dest, $src);
imagecopyresized($dest, $src, 0, 0, 0, 0, $newwidth, $newheight,
$width, $height);
$pixel_over_black = imagecolorat($dest, 0, 0);
// ...but now make the fill white...
$bg = imagecolorallocate($dest, 255, 255, 255);
imagefilledrectangle($dest, 0, 0, $newwidth, $newheight,
$bg);
imagecopyresized($dest, $src, 0, 0, 0, 0, $newwidth, $newheight,
$width, $height);
$pixel_over_white = imagecolorat($dest, 0, 0);
// ...to test if transparency causes the fill color to show through:
if($pixel_over_black != $pixel_over_white)
{
// Background IS transparent
imagefilledrectangle($dest, 0, 0, $newwidth, $newheight,
$tpcolor);
imagecopyresized($dest, $src, 0, 0, 0, 0, $newwidth,
$newheight, $width, $height);
imagecolortransparent($dest, $tpcolor);
imagegif($dest, "$fixPath/images/thumbs/$userfile");
}
else // Background (most probably) NOT transparent
imagegif($dest, "$fixPath/images/thumbs/$userfile");
break;
case 2: // JPEG
.
.
.
case 3: // PNG
.
.
.
break;
}
imagedestroy($src);
imagedestroy($dest);
}
else
$note = "Error: Uploaded image but could not create thumbnail";
header("location: $fixPath/index.php?next=$next&nb=$nb&userfile=$userfile");
}
else
$note = "Could not find anything to upload\n";`
webmaster at webnetwizard dotco dotuk
11-Jan-2005 06:09
`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.`
emilus at galeria-m dot art dot pl
30-Sep-2004 02:02
`To resize or copy image (gif [with gd>=2.0.28] or png) with transparency.
-Take current transparency color
-create a new image with palette like old one and fill new image with transparent color
-set transparent color
-copy resized image
$colorTransparent = imagecolortransparent($im);
$im2 = imagecreate($new_w,$new_h);
imagepalettecopy($im2,$im);
imagefill($im2,0,0,$colorTransparent);
imagecolortransparent($im2, $colorTransparent);
imagecopyresized($im2,$im,0,0,0,0,$new_w,$new_h,$imsx,$imsy);`
php_comment et 5mm de
27-Sep-2004 10:02
`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);
?>`
shelley_hina at yahoo dot com
04-Mar-2004 11:35
`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`
php at kybert dot com
14-Nov-2003 05:30
`i had problems with the example by sandhawk at spies dot com because my png overlay, and the jpeg canvas were using different color depths, so, this function corrects this:
[code]
function WatermarkImage($CanvasImage, $WatermarkImage /* MUST BE PHG */, $Opacity=10, $Quality=75)
{
// create true color canvas image:
$canvas_src = imagecreatefromjpeg($CanvasImage);
$canvas_w = ImageSX($canvas_src);
$canvas_h = ImageSY($canvas_src);
$canvas_img = imagecreatetruecolor($canvas_w, $canvas_h);
imagecopy($canvas_img, $canvas_src, 0,0,0,0, $canvas_w, $canvas_h);
imagedestroy($canvas_src); // no longer needed
// create true color overlay image:
$overlay_src = imagecreatefrompng($WatermarkImage);
$overlay_w = ImageSX($overlay_src);
$overlay_h = ImageSY($overlay_src);
$overlay_img = imagecreatetruecolor($overlay_w, $overlay_h);
imagecopy($overlay_img, $overlay_src, 0,0,0,0, $overlay_w, $overlay_h);
imagedestroy($overlay_src); // no longer needed
// setup transparent color (pick one):
$black = imagecolorallocate($overlay_img, 0x00, 0x00, 0x00);
$white = imagecolorallocate($overlay_img, 0xFF, 0xFF, 0xFF);
$magenta = imagecolorallocate($overlay_img, 0xFF, 0x00, 0xFF);
// and use it here:
imagecolortransparent($overlay_img, $white);
// copy and merge the overlay image and the canvas image:
imagecopymerge($canvas_img, $overlay_img, 0,0,0,0, $overlay_w, $overlay_h, $Opacity);
// output:
header("Content-type: image/jpeg");
imagejpeg($canvas_img, '', $Quality);
imagedestroy($overlay_img);
imagedestroy($canvas_img);
}
// call function with opcity set to 50% and 95% quality
WatermarkImage("canvas.jpg", "overlay.png", 50, 95);
[/code]`
creator79>>web>>de
08-Oct-2003 06:44
`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().`
calar at gamigo dot de
01-Aug-2003 06:53
`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`
cladiniz at zipmail dot com dot br
17-Mar-2003 06:19
`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 ...`
sandhawk at spies dot com
05-Mar-2003 11:04
`here's a little snippet of code that i got for getting images to overlay with transparency(based off text overlay above).
$olay_img = imagecreatefromPNG("overlay.png); //NOTE I recomment you use png for this, artifacts from jpeg images can look bad when you overlay them
$src_img = ImageCreateFromJPEG("image.jpg");
$src_w = ImageSX($src_img);
$src_h = ImageSY($src_img);
$can_img = imagecreatetruecolor($src_w, $src_h);
$black = ImageColorAllocate ($olay_img, 0, 0, 0);
ImageColorTransparent($olay_img, $black);
imagecopy($can_img, $src_img, 0,0,0,0, $src_w, $src_h);
imagecopymerge($can_img, $olay_img, 0,0,0,0, ImageSX($olay_img), ImageSY($olay_img),100); //Imagecopy won't work, you must used imagecopymerge
ImageJPEG($can_img,'',100);
imagedestroy($src_img);
imagedestroy($olay_img);
imagedestroy($can_img);`
Jonatan
30-Dec-2002 10:35
`I have found that ImageColorTransparent() works on true color images if and only if the transparent color is black.
(PHP 4.2.3/GD 2.0)`
marshall.AT.byterage.net
04-May-2002 07:50
`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!`
06-Dec-2001 07:18
`when a color is defined as transparent, anytime that color is used in another function (before or after transparency definition) will be transparent
ex:
$red = ImageColorAllocate ($im, 233, 14, 91);
$blue = ImageColorAllocate ($im, 0x20, 0x60, 0xaa);
imagefilledarc ($im, 100, 100, 80, 80, 0, 60, $blue, IMG_ARC_PIE);//arc is transparent even though is not defined yet
$trans=imagecolortransparent($im,$blue);//affects all occurances of blue, before and after this line
ALSO:
ImageString ($im, 10, 5, 5, "Transparent Text String", $blue);
---is equivalent to----
ImageString ($im, 10, 5, 5, "Transparent Text String", $trans);`
add a note
imageconvolution imagecolorstotal

Last updated: Fri, 26 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST