Re: scaling text
[email protected] (zentara)
| Newsgroups | perl.beginners |
|---|---|
| Message-ID | <[email protected]> |
On Fri, 23 May 2008 12:41:45 -0400, [email protected] ("R. Hicks") wrote: >zentara wrote: ><snip> >> You really didn't say how you are making the badges, >> like is there a limited defined bounding box that each >> name must fit in? Or can you adjust the name-box size >> to accomodate longer names? etc. etc. >> >> zentara >> >> >I am being handed pre-made badges of 121x120. The first line of pixels >is to be taken as the color of the text and sheared off to make a >120x120 image. I can work that out later. > >What I need to do is get the users name, create an image from that, get >its size (I guess that is called a bounding box) and then scale and >overlay that image onto the pre-designed one. > >R Another idea is GD::Text::Align It has a bounding_box method, that you can put in a loop to determine the right font size before printing. Either way.... make an image of the name and shrink to fit, or adjust font/point size to fit, you will need some logic to get the maximum visible name size. Intersesting problem. If it wasn't too many, and you needed the best results, I would probably go with a canvas tool, that let you adjust font size of the name and drag it into optimum position with the mouse. Drom GD::Text::Align $align->bounding_box(x, y, angle) Return the bounding box of the string to draw. This returns an eight? element list (exactly like the GD::Image->stringTTF method): (x1,y1) lower left corner (x2,y2) lower right corner (x3,y3) upper right corner (x4,y4) upper left corner Note that upper, lower, left and right are relative to the string, not to the canvas. The bounding box can be used to make decisions about whether to move the string or change the font size prior to actually drawing the string. Here is an example that will place centered text along the bottom of the image, and use a font size 1/10 the image height. You can try to figure out the bounding box, but if all your names are relatively constant in length, you can find the longest name, then left and right pad the shorter names so as to get the font best for the longest name, and use that for all names. Then they will all be centered and same font size. #!/usr/bin/perl use warnings; use strict; use GD; use GD::Text::Align; @ARGV or die "Need an image file\n"; for my $img_file (@ARGV) { my $gd = GD::Image->new($img_file) or die; my ($w, $h) = $gd->getBounds(); print "$w $h\n"; my $gdt = GD::Text::Align->new($gd, valign => 'bottom', halign => 'center', text => 'Some Text', colour => $gd->colorResolve(0,0,0), ) or die; $gdt->set_font('arial', $h/10) or die; # need arial.ttf in cwd $gdt->draw($w/2, $h, 0) or die; my @bb = $gdt->bounding_box($w, $h/2 + 2, 0); print "@bb\n"; open(GD, ">$0.png") or die; binmode GD; print GD $gd->png; close GD; } __END__ zentara -- I'm not really a human, but I play one on earth. http://zentara.net/japh.html