+ Reply to Thread
Results 1 to 2 of 2

Thread: Generate text with transparent background

  1. #1
    AfTriX is offline Programming God
    Join Date
    Jan 2007
    Location
    Chicago
    Posts
    586
    Rep Power
    0

    Generate text with transparent background

    First some variables to define how the function will work.
    $theText is the text that we want to have in the image.
    The other variables should also be pretty easy to understand...
    I defined them in variables so you easily can change them to try different things.

    The first thing which is a little bit tricky is the "imageTTFBBox"-function.
    Not really self explaining hehe :-)
    What it does is depending on the parameters it gives back the size of the bounding box needed for this text, in pixels.
    You get an array with this information:
    0 lower left corner, X position
    1 lower left corner, Y position
    2 lower right corner, X position
    3 lower right corner, Y position
    4 upper right corner, X position
    5 upper right corner, Y position
    6 upper left corner, X position
    7 upper left corner, Y position

    The next step is to create the image (empty but with correct size to fit the text) with this function:
    imageCreateTrueColor(x, y)
    You give the function the width in x and y, and you get the values you see in the function call.

    imageSaveAlpha sets the mode to save all alpha channel information, which is needed to make a png file transparent.
    ImageAlphaBlending sets the blending mode off, which meens "simply" that the alpha information is preserved. (not that important, just set it to false hehe)

    imagecolorallocatealpha allocates a color for an image, and sets how transparent it should be. 0 is NO transparency and 127 is fully transparent.
    127 is prefect for us, but you can always mess around with the numbers to se what happens :-)
    This color variable is used for filling the "real" image with transparent color.

    The last part is pretty straight forward.
    Get textColor, and write the text in the image. After that we just save it with the name textImage.png.
    OBS, the web server needs write permissions to this catalog.
    So if you are going to use this in a live application/site, you should put the images in a separate directory and change permissions on that.

    Code:
    $theText "999 Tutorials, transparent text";
    $fontSize 15;
    $angle 25;
    $font "arial.ttf";

    $size imageTTFBBox($fontSize$angle$theFont$theText);
    $image imageCreateTrueColor(abs($size[2]) + abs($size[0]), abs($size[7]) + abs($size[1]));
    imageSaveAlpha($imagetrue);
    ImageAlphaBlending($imagefalse);

    $transparentColor imagecolorallocatealpha($image200200200127);
    imagefill($image00$transparentColor);

    $textColor imagecolorallocate($image200200200);
    imagettftext($image$fontSize00abs($size[5]), $textColor$font$theText);
    imagepng($image"textImage.png");
    imagedestroy($image); 
    If you think that the code is hard to understand, just read the text once again. If you still don't understand.... don't care :-) Just use it and get a beer instead!

    Ref:
    Front Page | Pixel Groovy
    Last edited by John; 01-06-2007 at 09:25 PM. Reason: Please use the [php], [code], and [html] functions when appropriate. Makes it easier to read. :)

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    xtraze is offline Programming God
    Join Date
    Dec 2006
    Location
    Sri lanka
    Posts
    911
    Rep Power
    0
    I think I will quickly create and use a .png file.
    Too long for me as I use png.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. png with transparent background and IE6
    By Alhazred in forum JavaScript and CSS
    Replies: 5
    Last Post: 10-14-2010, 02:17 PM
  2. how to make transparent background
    By kiddies in forum JavaScript and CSS
    Replies: 5
    Last Post: 08-11-2009, 08:57 AM
  3. Replies: 1
    Last Post: 04-26-2009, 05:27 PM
  4. PyGame Generate Background?
    By nmuntz in forum Python
    Replies: 2
    Last Post: 11-26-2008, 05:27 AM
  5. XP Transparent Icon Text
    By TcM in forum Computer Software/OS
    Replies: 2
    Last Post: 01-29-2008, 01:24 AM

Tags for this Thread

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