PDA

View Full Version : Transparent Image Using GD



benslayton
06-26-2007, 03:48 AM
How do I make the Background transparent in this:

<?php
// Set the content-type
header("Content-type: image/png");
//Font Type
$fontType = $_GET["font"];
$fontFolder = "fonts/";
$fontEXT = ".ttf";
$font = $fontFolder . $fontType . $fontEXT;

//Font Size
$fontSize = $_GET["fsize"];
// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
$grey = imagecolorallocate($im, 128, 128, 128);
$red = imagecolorallocate($im, 255, 0, 0);
$blue = imagecolorallocate($im, 0, 0, 255);
$green = imagecolorallocate($im, 0, 255, 0);
$yellow = imagecolorallocate($im, 255, 255, 0);
$orange = imagecolorallocate($im, 255, 165, 0);
$pink = imagecolorallocate($im, 255, 0, 255);

//create background
imagefilledrectangle($im, 0, 0, 399, 29, $blue);

// The text to draw
$text = $_GET["text"];

// Add some shadow to the text
imagettftext($im, $fontSize, 0, 0, $fontSize, $black, $font, $text);

// Add the text
//imagettftext($im, $fontSize, 0, 10, 20, $white, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

I am not quite sure how to do it.

alexjewell
06-26-2007, 01:20 PM
I found a code that makes a different image (depending on the browser) with transparency:



function png_image($image,$width=-1,$height=-1,$extra="") {
global $_SERVER;
static $static_browser;
if (!is_array($static_browser)) {
$useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
if ((strpos($useragent,'win') || strpos($useragent,'microsoft')) and strpos($useragent,'msie')) {
preg_match('#msie ([0-9\.]+)#', $useragent, $regs);
$static_browser[0] = $regs[1];
} else $static_browser[0] = 0;
}
$width =intval($width);
$height =intval($height);
if (($width<0 || $height<0) && file_exists($image)) {
if ($info = @getimagesize($image)) {
if ($info[0]>0 && $width<0) $width = $info[0];
if ($info[1]>0 && $height<0) $height = $info[1];
}
}
$extra = trim($extra);
if ($extra) $extratag .= $extra." ";
if ($width>0) $widthtag = "width=\"".$width."\" ";
if ($height>0) $heighttag = "height=\"".$height."\" ";
if (($static_browser[0]>=5.5) and file_exists($image)) {
return "<img style=\"FILTER: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='$image', sizingMethod='scale'); WIDTH: ".$width."px; HEIGHT: ".$height."px\" src=\"data/spacer.gif\" ".$widthtag.$heighttag.$extratag."/>";
} else {
return "<img src=\"$image\" ".$widthtag.$heighttag.$extratag."/>";
}
}


An example of how to use it:



<?php echo png_image('data/bg_main.png',800,100,'border="0"'); ?>


I'd check out this php functions too:
http://us.php.net/imagecolortransparent
http://us.php.net/imagesavealpha

Twey
06-26-2007, 03:47 PM
No, that doesn't make an image, it outputs an <img> element -- in XHTML, which is probably not what's wanted. You probably want to use the long-named imagecolorallocatealpha (http://uk.php.net/manual/en/function.imagecolorallocatealpha.php)():
imagefill($im, 0, 0, imagecolorallocatealpha($im, 0, 0, 0, 0));

alexjewell
06-26-2007, 04:16 PM
oh, yeah, I misunderstood. Nevermind.