+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Help generating a transparent png image using php

  1. #1
    purpleflame's Avatar
    purpleflame is offline x10Hosting Member purpleflame is an unknown quantity at this point
    Join Date
    Dec 2008
    Posts
    17

    Help generating a transparent png image using php

    So I am trying to use a php code to make a png image that has a transparent background with a sentence written on it. So far I have a transparent png image of the size I want and the code below. The only problem I have is that the background color comes out as black and not transparent. I am sure I have overlooked something really simple. If anyone can help, I would appreciate it. Thank you.

    PHP Code:
    <?php

    header
    ("Content-type: image/png");
    $string "Working";
    $im     imagecreatefrompng("http://i271.photobucket.com/albums/jj149/violetvalor/075400.png");
    $txtcolor imagecolorallocate($im10251153);
    imagestring($im333$string$txtcolor);
    imagepng($im);
    imagedestroy($im);

    ?>
    Address: http://purpleflame.exofire.net/phpte...timagetest.php

  2. #2
    leafypiggy's Avatar
    leafypiggy is offline Community Advocate leafypiggy is on a distinguished road
    Join Date
    Aug 2007
    Location
    Massachusetts
    Posts
    2,228

    Re: Help generating a transparent png image using php

    I may be wrong, but I don't think you can create transparent images using the gd library. I've never been able to when I try. Sorry!

    --Neil
    Neil Hanlon | x10Hosting Support Representative
    Neil[at]x10hosting.com
    █ I'm always happy to help. Just ask a question in Free Hosting
    Terms of Service IRC

  3. #3
    Scoochi2's Avatar
    Scoochi2 is offline x10 Sophmore Scoochi2 is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Southport!
    Posts
    185

    Re: Help generating a transparent png image using php

    As you can see from the image in my sig, I have successfully done this.
    EDIT: Well, ok then. It is hard to tell against a plain white background... but it is!


    Here's a quick method that you could use:
    PHP Code:
    <?php
    header
    ("Content-type: image/png");
    $image imagecreatetruecolor(500150)
       or die(
    "Cannot Initialize new GD image stream");
    $col_transparent imagecolorallocatealpha($image255255255127);
    $col_red   imagecolorallocate($image20000);

    imagefill($image00$col_transparent);  // set the transparent colour as the background.
    imagecolortransparent ($image$col_transparent); // actually make it transparent

    imagestring($image5105,  "Working"$col_red);
    imagepng($image);
    imagedestroy($image);
    ?>
    The whole trick here is to allocate a colour as being 'transparent' at the start, then setting it as the background colour and allocating it as actually being transparent. Of course, you don't necessary have to set it as the background. You could choose to only use it in certain places. Just draw it like any other colour.
    Last edited by Scoochi2; 12-26-2008 at 11:45 PM. Reason: plain bg!
    If anyone can see it, my post was meant for anyone who reads it. Don't take it personally or think I'm being condescending... :nuts:

  4. #4
    Livewire's Avatar
    Livewire is offline Abuse Compliance Officer Livewire is a glorious beacon of lightLivewire is a glorious beacon of light
    Join Date
    Jun 2005
    Location
    Behind a keyboard.
    Posts
    8,998

    Re: Help generating a transparent png image using php

    Quote Originally Posted by Scoochi2 View Post
    As you can see from the image in my sig, I have successfully done this.
    EDIT: Well, ok then. It is hard to tell against a plain white background... but it is!


    Here's a quick method that you could use:
    PHP Code:
    <?php
    header
    ("Content-type: image/png");
    $image imagecreatetruecolor(500150)
       or die(
    "Cannot Initialize new GD image stream");
    $col_transparent imagecolorallocatealpha($image255255255127);
    $col_red   imagecolorallocate($image20000);

    imagefill($image00$col_transparent);  // set the transparent colour as the background.
    imagecolortransparent ($image$col_transparent); // actually make it transparent

    imagestring($image5105,  "Working"$col_red);
    imagepng($image);
    imagedestroy($image);
    ?>
    The whole trick here is to allocate a colour as being 'transparent' at the start, then setting it as the background colour and allocating it as actually being transparent. Of course, you don't necessary have to set it as the background. You could choose to only use it in certain places. Just draw it like any other colour.
    I'm gunna vouch for this without trying it actually - the thing in the lower-right of my sig's using pretty much the same method to get the background transparent.


    One note though: in imagecolorallocatealpha($image, 255, 255, 255, 127);

    As long as the last number (alpha) is 127 (it's on a scale from 0 (opaque) to 127 (fully transparent)), the first one's don't matter. Mine's using all 0's so I don't get confused which one's alpha


    TOS breakers will be suspended regardless of race, creed, national origin, hair color, or favorite food. Thanks for your understanding!

  5. #5
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: Help generating a transparent png image using php

    you can do it as scoochi said. i use transparent png's resized w/ gd library for parts of my site.

    for the most part, those look like the fundamentals for making it work.

  6. #6
    purpleflame's Avatar
    purpleflame is offline x10Hosting Member purpleflame is an unknown quantity at this point
    Join Date
    Dec 2008
    Posts
    17

    Re: Help generating a transparent png image using php

    I must have messed up somehow since I tried the following code, however, the browser only showed me the message:

    The image "http://purpleflame.exofire.net/phptests/textimagetest.php" cannot be displayed, because it contains errors.
    PHP Code:
    <?php

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

    $text "Working";

    $image  imagecreatetruecolor(400,75);
    $bg_color imagecolorallocatealpha($image000127);
    $fg_color imagecolorallocate($image10251153);
    //$font   = "arial.ttf";
    //imagettftext($image, 12, 0, 3, 3, $fg_color, $font, $text);

    imagefill($image00$bg_color);
    imagecolortrasparent($image$bg_color);

    imagestring($image433$text$fg_color);

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

    ?>
    I also tried including that line that begins with "or die", but it only gave an error message saying it cannot parse the logical operator "or". Thanks in advance.

  7. #7
    Scoochi2's Avatar
    Scoochi2 is offline x10 Sophmore Scoochi2 is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Southport!
    Posts
    185

    Re: Help generating a transparent png image using php

    I think it may only be a typo...

    imagecolortransparent($image, $bg_color);
    If anyone can see it, my post was meant for anyone who reads it. Don't take it personally or think I'm being condescending... :nuts:

  8. #8
    purpleflame's Avatar
    purpleflame is offline x10Hosting Member purpleflame is an unknown quantity at this point
    Join Date
    Dec 2008
    Posts
    17

    Re: Help generating a transparent png image using php

    (* '.' *) Thanks for catching that. That fixed it.

    So I found this other function to place text called imagettftext. However, its makes the same problem. I liked this function since it lets me use a normal font like arial. However, it seems that FreeType library needs to be available to use it. Is that causing the problem?

    More importantly, is there some way to wrap the text in case the string is too long? Thanks in advance.

  9. #9
    Scoochi2's Avatar
    Scoochi2 is offline x10 Sophmore Scoochi2 is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Southport!
    Posts
    185

    Re: Help generating a transparent png image using php

    Quote Originally Posted by purpleflame View Post
    So I found this other function to place text called imagettftext. However, its makes the same problem. I liked this function since it lets me use a normal font like arial. However, it seems that FreeType library needs to be available to use it. Is that causing the problem?
    Not really, you just need the .TTF file for the font that you wish to use on the server.
    PHP Code:
    <?php
    // Set the enviroment variable for GD
    putenv('GDFONTPATH=' realpath('.'));

    // Name the font to be used (note the lack of the .ttf extension)
    $font 'SomeFont';

    // Print the text, using the predefined font file
    imagettftext($image2001020$col_red$font'Working');
    ?>
    As for the wrapped text, a quick Google search yielded the following. I've not tested it, but it looks ok. You could change it slightly to use imagettftext instead of imagestring.

    Also, I would do some major editing on it so that it doesn't give the image, it just adds the text... Maybe I'll do that tomorrow
    PHP Code:
    <?php
    function make_wrapped_txt($txt$color 000000$space 4$font 4$w 300)
    {
        if (
    strlen($color) != 6)
            
    $color 000000;
        
    $int hexdec($color);
        
    $h imagefontheight($font);
        
    $fw imagefontwidth($font);
        
    $txt explode("\n"wordwrap($txt, ($w $fw), "\n"));
        
    $lines count($txt);
        
    $im imagecreate($w, (($h $lines) + ($lines $space)));
        
    $bg imagecolorallocate($im255255255);
        
    $color imagecolorallocate($im0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8),
            
    0xFF $int);
        
    $y 0;
        foreach (
    $txt as $text)
        {
            
    $x = (($w - ($fw strlen($text))) / 2);
            
    imagestring($im$font$x$y$text$color);
            
    $y += ($h $space);
        }
        
    header('Content-type: image/jpeg');
        die(
    imagejpeg($im));
    }
    ?>
    Last edited by Scoochi2; 12-27-2008 at 06:04 PM.
    If anyone can see it, my post was meant for anyone who reads it. Don't take it personally or think I'm being condescending... :nuts:

  10. #10
    purpleflame's Avatar
    purpleflame is offline x10Hosting Member purpleflame is an unknown quantity at this point
    Join Date
    Dec 2008
    Posts
    17

    Re: Help generating a transparent png image using php

    Alright. So I tried to incorporate that as best as I could. However, I still get the error that it contains errors so it cannot be displayed. Thanks again.

    PHP Code:
    <?php

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

    $TEXT "123456789012345678901234567890123456789012345678901234567890";

    $width 300;
    $font 3;
    $space 3;

    $fh imagefontheight($font);
    $fw imagefontwidth($font);
    $txt explode("\n"wordwrap($TEXT, ($width $fw), "\n"));
    $lines count($txt);

    $image imagetruecolor($width, (($fh $lines) + ($lines $space)));
    $bg_color imagecolorallocatealpha($image000127);
    $fg_color imagecolorallocate($image10251153);

    imagefill($image00$bg_color);
    imagecolortransparent($image$bg_color);

    $y 0;
    foreach (
    $txt as $text)
    {
        
    $x = (($w - ($fw strlen($text))) / 2);
        
    imagestring($image$font$x$y$text$fg_color);
        
    $y += ($fh $space);
    }

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

    ?>

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Places to learn php
    By JaWasabi in forum Scripts & 3rd Party Apps
    Replies: 9
    Last Post: 01-13-2009, 02:03 AM
  2. Replies: 1
    Last Post: 12-20-2008, 05:44 PM
  3. Noticia De upgrade PHP espaņol
    By figu120 in forum General
    Replies: 2
    Last Post: 11-22-2007, 04:42 PM
  4. Important PHP Information
    By Bryon in forum News and Announcements
    Replies: 0
    Last Post: 11-21-2007, 02:08 PM
  5. "PHP Startup: Invalid Library" - Interesting error
    By javaguy78 in forum Free Hosting
    Replies: 5
    Last Post: 03-27-2007, 02:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers