Link to home
Start Free TrialLog in
Avatar of MDauphinais1
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($im, 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",$width="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_img,$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=imagecreatetruecolor($new_w,$new_h);
            $src_img=ImageCreateFrompng($orig_image);
            imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,ImageSX($src_img),ImageSY($src_img));
            Imagepng($dst_img, "$name");
      }
}


$insert = imagecreatefromjpeg("albumart.png");
$image = imagecreatefrompng("currentlyplaying.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($background, false);
$transparentColor = imagecolorallocatealpha($background, 255, 255, 255, 127);
imagefilledrectangle($background, 0, 0, 440, 45, $transparentColor);

imagecopymerge($background,$foreground,$overlapX,$overlapY,0,0,$insertWidth,$insertHeight,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);
?>
SOLUTION
Avatar of cx323
cx323

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Graham N.
Graham N.
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MDauphinais1
MDauphinais1

ASKER

I'm fine with full transparency because that's what I want.

So all I have to do is change where it says png to gif?   I did that and now the background color is white.
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("albumart.png");

should read

$insert = imagecreatefrompng("albumart.png");

or change the format of albumart.png to albumart.jpg
Thanks for the help!  And thanks for catching that error also.