MDauphinais1
asked on
Create image with transparent background
OK, I have this code that I am using to create an image with a transparent background. In IE7 the image appears with the transparent background but in IE6 the background has a light color to it. Is this something in the code I can fix or something else?
<?
// SONG INFO IMAGE
// Create the image
$im = imagecreatetruecolor(440, 45);
imageSaveAlpha($im, true);
ImageAlphaBlending($im, false);
$transparentColor = imagecolorallocatealpha($i m, 255, 255, 255, 127);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
$darkblue = imagecolorallocate($im, 22, 156, 216);
$orange = imagecolorallocate($im, 220, 210, 60);
imagefilledrectangle($im, 0, 0, 440, 45, $transparentColor);
// Put text here
// CURRENT SONG TEXT
$songcombine = substr($currentsong, 0, 18);
$artistcombine = substr($currentartist, 0, 18);
$lineone = 'Currently Playing:';
$linetwo = $songcombine . " by " . $artistcombine;
$linethree = 'Vote Now!';
// LISTENER COUNT TEXT
$linefour = 'Listeners';
$linefive = $todaystophitzlisteners;
// Replace path by your own font path
$font = 'arial.ttf';
// CURRENT SONG INSERT
imagettftext($im, 14, 0, 50, 20, $white, $font, $lineone);
imagettftext($im, 10, 0, 50, 40, $white, $font, $linetwo);
imagettftext($im, 14, 0, 220, 20, $darkblue, $font, $linethree);
// LISTENER COUNT INSERT
imagettftext($im, 14, 0, 100, 20, $orange, $font, $linefour);
imagettftext($im, 18, 0, 133, 43, $orange, $font, $linefive);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im, "currentlyplaying.png");
imagedestroy($im);
$newfilename = "albumart.png";
$uploaddir = "";
resize_me ($albumart, $uploaddir, $newfilename, "25");
function resize_me ($img, $path, $name, $percent,$height="40",$wid th="40") {
//image file path
//the folder to save to
//the name to use when saving
//percent to resize
$orig_image = $img;
$image_stats = GetImageSize($orig_image);
$imagewidth = $image_stats[0];
$imageheight = $image_stats[1];
$img_type = $image_stats[2];
if($width == "")
{
$new_w = $imagewidth * $percent;
$new_w = round($new_w / 100);
}
else
$new_w = $width;
if($height == "")
{
$ratio = ($imagewidth / $new_w);
$new_h = round($imageheight / $ratio);
}
else
$new_h = $height;
if ($img_type=="2") {
$src_img = imagecreatefromjpeg($orig_ image);
$dst_img = imagecreatetruecolor($new_ w,$new_h);
imagecopyresampled($dst_im g,$src_img ,0,0,0,0,$ new_w,$new _h,imagesx ($src_img) ,imagesy($ src_img));
imagejpeg($dst_img, "$name");
} elseif ($img_type=="3") {
$dst_img=imagecreatetrueco lor($new_w ,$new_h);
$src_img=ImageCreateFrompn g($orig_im age);
imagecopyresampled($dst_im g,$src_img ,0,0,0,0,$ new_w,$new _h,ImageSX ($src_img) ,ImageSY($ src_img));
Imagepng($dst_img, "$name");
}
}
$insert = imagecreatefromjpeg("album art.png");
$image = imagecreatefrompng("curren tlyplaying .png");
$image = image_overlap($image, $insert);
function image_overlap($background, $foreground){
$insertWidth = imagesx($foreground);
$insertHeight = imagesy($foreground);
$imageWidth = imagesx($background);
$imageHeight = imagesy($background);
$overlapX = 0;
$overlapY = 3;
imageSaveAlpha($background , true);
ImageAlphaBlending($backgr ound, false);
$transparentColor = imagecolorallocatealpha($b ackground, 255, 255, 255, 127);
imagefilledrectangle($back ground, 0, 0, 440, 45, $transparentColor);
imagecopymerge($background ,$foregrou nd,$overla pX,$overla pY,0,0,$in sertWidth, $insertHei ght,100);
return $background;
}
// Create some colors
$white = imagecolorallocate($image, 255, 255, 255);
$grey = imagecolorallocate($image, 128, 128, 128);
$black = imagecolorallocate($image, 0, 0, 0);
$darkblue = imagecolorallocate($image, 22, 156, 216);
$orange = imagecolorallocate($image, 220, 210, 60);
// Put text here
// CURRENT SONG TEXT
$songcombine = substr($currentsong, 0, 18);
$artistcombine = substr($currentartist, 0, 18);
$lineone = 'Currently Playing:';
$linetwo = $songcombine . " by " . $artistcombine;
$linethree = 'Vote Now!';
// LISTENER COUNT TEXT
$linefour = 'Listeners';
$linefive = $todaystophitzlisteners;
// Replace path by your own font path
$font = 'arial.ttf';
// CURRENT SONG INSERT
imagettftext($image, 14, 0, 50, 20, $white, $font, $lineone);
imagettftext($image, 10, 0, 50, 40, $white, $font, $linetwo);
imagettftext($image, 14, 0, 230, 20, $darkblue, $font, $linethree);
// LISTENER COUNT INSERT
imagettftext($image, 14, 0, 350, 20, $orange, $font, $linefour);
imagettftext($image, 18, 0, 370, 43, $orange, $font, $linefive);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($image, "currentlyplaying2.png");
imagedestroy($image);
?>
<?
// SONG INFO IMAGE
// Create the image
$im = imagecreatetruecolor(440, 45);
imageSaveAlpha($im, true);
ImageAlphaBlending($im, false);
$transparentColor = imagecolorallocatealpha($i
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
$darkblue = imagecolorallocate($im, 22, 156, 216);
$orange = imagecolorallocate($im, 220, 210, 60);
imagefilledrectangle($im, 0, 0, 440, 45, $transparentColor);
// Put text here
// CURRENT SONG TEXT
$songcombine = substr($currentsong, 0, 18);
$artistcombine = substr($currentartist, 0, 18);
$lineone = 'Currently Playing:';
$linetwo = $songcombine . " by " . $artistcombine;
$linethree = 'Vote Now!';
// LISTENER COUNT TEXT
$linefour = 'Listeners';
$linefive = $todaystophitzlisteners;
// Replace path by your own font path
$font = 'arial.ttf';
// CURRENT SONG INSERT
imagettftext($im, 14, 0, 50, 20, $white, $font, $lineone);
imagettftext($im, 10, 0, 50, 40, $white, $font, $linetwo);
imagettftext($im, 14, 0, 220, 20, $darkblue, $font, $linethree);
// LISTENER COUNT INSERT
imagettftext($im, 14, 0, 100, 20, $orange, $font, $linefour);
imagettftext($im, 18, 0, 133, 43, $orange, $font, $linefive);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im, "currentlyplaying.png");
imagedestroy($im);
$newfilename = "albumart.png";
$uploaddir = "";
resize_me ($albumart, $uploaddir, $newfilename, "25");
function resize_me ($img, $path, $name, $percent,$height="40",$wid
//image file path
//the folder to save to
//the name to use when saving
//percent to resize
$orig_image = $img;
$image_stats = GetImageSize($orig_image);
$imagewidth = $image_stats[0];
$imageheight = $image_stats[1];
$img_type = $image_stats[2];
if($width == "")
{
$new_w = $imagewidth * $percent;
$new_w = round($new_w / 100);
}
else
$new_w = $width;
if($height == "")
{
$ratio = ($imagewidth / $new_w);
$new_h = round($imageheight / $ratio);
}
else
$new_h = $height;
if ($img_type=="2") {
$src_img = imagecreatefromjpeg($orig_
$dst_img = imagecreatetruecolor($new_
imagecopyresampled($dst_im
imagejpeg($dst_img, "$name");
} elseif ($img_type=="3") {
$dst_img=imagecreatetrueco
$src_img=ImageCreateFrompn
imagecopyresampled($dst_im
Imagepng($dst_img, "$name");
}
}
$insert = imagecreatefromjpeg("album
$image = imagecreatefrompng("curren
$image = image_overlap($image, $insert);
function image_overlap($background,
$insertWidth = imagesx($foreground);
$insertHeight = imagesy($foreground);
$imageWidth = imagesx($background);
$imageHeight = imagesy($background);
$overlapX = 0;
$overlapY = 3;
imageSaveAlpha($background
ImageAlphaBlending($backgr
$transparentColor = imagecolorallocatealpha($b
imagefilledrectangle($back
imagecopymerge($background
return $background;
}
// Create some colors
$white = imagecolorallocate($image,
$grey = imagecolorallocate($image,
$black = imagecolorallocate($image,
$darkblue = imagecolorallocate($image,
$orange = imagecolorallocate($image,
// Put text here
// CURRENT SONG TEXT
$songcombine = substr($currentsong, 0, 18);
$artistcombine = substr($currentartist, 0, 18);
$lineone = 'Currently Playing:';
$linetwo = $songcombine . " by " . $artistcombine;
$linethree = 'Vote Now!';
// LISTENER COUNT TEXT
$linefour = 'Listeners';
$linefive = $todaystophitzlisteners;
// Replace path by your own font path
$font = 'arial.ttf';
// CURRENT SONG INSERT
imagettftext($image, 14, 0, 50, 20, $white, $font, $lineone);
imagettftext($image, 10, 0, 50, 40, $white, $font, $linetwo);
imagettftext($image, 14, 0, 230, 20, $darkblue, $font, $linethree);
// LISTENER COUNT INSERT
imagettftext($image, 14, 0, 350, 20, $orange, $font, $linefour);
imagettftext($image, 18, 0, 370, 43, $orange, $font, $linefive);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($image, "currentlyplaying2.png");
imagedestroy($image);
?>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Your server may not support GIF creation as it is a proprietary format - most PHP implementations don't support GIF becuase of this - so you may have to stay with PNG. By the way you have an error in your PHP script:
$insert = imagecreatefromjpeg("album art.png");
should read
$insert = imagecreatefrompng("albuma rt.png");
or change the format of albumart.png to albumart.jpg
$insert = imagecreatefromjpeg("album
should read
$insert = imagecreatefrompng("albuma
or change the format of albumart.png to albumart.jpg
ASKER
Thanks for the help! And thanks for catching that error also.
ASKER
So all I have to do is change where it says png to gif? I did that and now the background color is white.