SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    GD and transparent background

    I have this code:

    PHP Code:
    header("Content-type: image/png");

    $string "My text";

    $font  2;
    $width  imagefontwidth($font) * strlen($string);
    $height imagefontheight($font);

    $image imagecreatetruecolor ($width,$height);
    $white imagecolorallocate ($image,255,255,255);
    $black imagecolorallocate ($image,0,0,0);
    imagefill($image,0,0,$white);

    imagestring ($image,$font,0,0,$string,$black);

    imagepng ($image);
    imagedestroy($image); 
    How can I simply make sure the background is transparent?

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    51
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Try something like this:

    PHP Code:
    header("Content-type: image/png");

    $string "My text";

    $font  2;
    $width  imagefontwidth($font) * strlen($string);
    $height imagefontheight($font);

    $image imagecreatetruecolor ($width,$height);
    imagealphablending($image false);
    imagesavealpha($image true);

    $transparent imagecolorallocatealpha($image 255255255127);
    $white imagecolorallocate ($image,255,255,255);
    $black imagecolorallocate ($image,0,0,0);
    imagefill($image,0,0,$transparent);

    imagestring ($image,$font,0,0,$string,$black);

    imagepng ($image);
    imagedestroy($image); 

  3. #3
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect! Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •