Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Adding Text to Images - Page 16

October 22, 2001

The GD library enables us to superimpose text onto an image using the string() function and the fonts that are built into the library. In the example below, the first parameter of the string() function is the font. There are five built-in fonts: gdSmallFont, gdMediumBoldFont, gdTinyFont, gdLargeFont and gdGiantFont. The second and third parameters of the string() function are the x and y coordinates where the text will start. The fourth parameter contains the string that will be drawn and the fifth parameter is the color index to use for the text. Remember that we cannot simply specify a color without allocating the color in the image map first with the colorResolve() function on line 3.

1  use GD;
2  my $image = GD::Image->newFromJpeg("66012-1.jpg");
3  my $black = $image->colorResolve(0,0,0);
4  $image->string(gdGiantFont,2,10,"Eat More Chiken",$black);
5  open(FILE, ">text1.jpg") || die "Cannot open text1.jpg: $!\n";
6  print FILE $image->jpeg;

The built-in fonts do the job, but they don't compare to the image quality of True Type Fonts. Fortunately, we can use TTF fonts by using the stringTTF() function to draw text rather than using the string() function, which only uses the built-on fonts.

1  use GD;
2  my $image = GD::Image->newFromJpeg("66012-1.jpg");
3  my $black = $image->colorResolve(0,0,0);
4  $image->stringTTF($black,"/usr/share/fonts/ttf/windows/TANGON.TTF"
  ,36,0,2,30,"Eat More Chiken");
5  open(FILE, ">text2.jpg") || die "Cannot open text2.jpg: $!\n";
6  print FILE $image->jpeg;

[The red lines above are one line. They have been split for formatting purposes.]

The stringTTF() function requires seven parameters, the first being the index for the color of the text. On line 3 of the example above, we allocate the color black, which we will use for the text color. The second parameter of the stringTTF() function on line 4 is the full path to the True Type Font file. The third parameter is the point-size, which determines how big the characters should be when the string is drawn. The fourth parameter is the angle in which the text will be drawn. For example, to draw the text vertically from top to bottom, we would pass 90 as the angle parameter. Below, we want to draw the text from left to right horizontally, so we pass in a 0 for this parameter. The fifth and sixth parameters are the starting x and y coordinates where the drawing of the text starts. In this case, we want to draw the text starting two pixels in from the left side of the image and 30 pixels in from the top. The reason we specified 30 rather than 2 is that the text is drawn from the bottom up starting at the y coordinate, which means that we need to create extra padding to fit the text. The last parameter contains the text string that will be drawn in the image.

Using a Style to Draw a Dotted Line - Page 15
Weaving Magic With Regular Expressions
Creating Thumbnail Images - Page 17


Up to => Home / Authoring / Languages / Perl / Weave




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers