Lost Password?


  #1 (permalink)  
Old 01-04-2007, 03:07 AM
AfTriX AfTriX is offline
Programming God
 
Join Date: Jan 2007
Location: Sri Lanka
Posts: 596
Rep Power: 0
AfTriX is an unknown quantity at this point
Default 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.

PHP 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-07-2007 at 12:25 AM. Reason: Please use the [php], [code], and [html] functions when appropriate. Makes it easier to read. :)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 01-08-2007, 03:13 AM
xtraze xtraze is offline
Programming God
 
Join Date: Dec 2006
Location: Sri lanka
Posts: 921
Rep Power: 0
xtraze is on a distinguished road
Send a message via MSN to xtraze Send a message via Skype™ to xtraze
Default

I think I will quickly create and use a .png file.
Too long for me as I use png.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Grab Text within IE xxarmoxx Visual Basic Programming 1 04-06-2007 09:42 AM
HTML Basic Formatting clookid Tutorials 14 03-06-2007 04:10 PM
Glass text - Photoshop ahsan16 Photoshop Tutorials 3 01-11-2007 11:35 PM
Make Text Fade In Chan Website Design 2 01-10-2007 01:46 PM


All times are GMT -5. The time now is 07:05 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 97%

Ads