Results 1 to 4 of 4

Thread: Transparent Image Using GD

  1. #1
    Join Date
    Mar 2006
    Posts
    600
    Thanks
    5
    Thanked 4 Times in 4 Posts

    Default Transparent Image Using GD

    How do I make the Background transparent in this:
    PHP Code:
    <?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(40030);

    // Create some colors
    $white imagecolorallocate($im255255255);
    $black imagecolorallocate($im000);
    $grey imagecolorallocate($im128128128);
    $red imagecolorallocate($im25500);
    $blue imagecolorallocate($im00255);
    $green imagecolorallocate($im02550);
    $yellow imagecolorallocate($im2552550);
    $orange imagecolorallocate($im2551650);
    $pink imagecolorallocate($im2550255);

    //create background
    imagefilledrectangle($im0039929$blue);

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

    // Add some shadow to the text
    imagettftext($im$fontSize00$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.

  2. #2
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    I found a code that makes a different image (depending on the browser) with transparency:

    PHP Code:
    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<|| $height<0) && file_exists($image)) {
        if (
    $info = @getimagesize($image)) {
         if (
    $info[0]>&& $width<0$width $info[0];
         if (
    $info[1]>&& $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 Code:
    <?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
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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():
    Code:
    imagefill($im, 0, 0, imagecolorallocatealpha($im, 0, 0, 0, 0));
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    oh, yeah, I misunderstood. Nevermind.
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

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
  •