Best Fit Labels - Bounding Box

PerlMagick is an object-oriented Perl interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning PerlMagick.
Post Reply
clickdigital
Posts: 2
Joined: 2015-08-20T06:15:09-07:00
Authentication code: 1151

Best Fit Labels - Bounding Box

Post by clickdigital »

Hi
Firstly I am a newbie to both Perl and ImageMagick, I have extensively read the docs to no avail so I hope there is a patient expert who can point me in the right direction.

The project is a sign builder and the critical element we are addressing here is the width of the bounding box when text is added using the best fit approach. The issue is that although the width is resized it is not resizing to the maximum height set so there is effectively padding top and bottom. The result is the width returned is therefore about 30% inaccurate which is unacceptable.

Questions:
Is there an alternative approach that will allow me to completely fill the available height space ?
Is there an issue with my code which is causing this problem ?
Can the same approach be applied after arcs etc have been applied and will this return accurate results
Is there a way to explicitly measure the font bounding box height as an alternative may be to loop over the height and adjust until correct ? (not keen)

I have attached an image of the result which clearly shows the font is not resized to the maximum height, concequently making the width measurement inaccurate.
Image

Code: Select all

my $image;
$image = Image::Magick->new (size => "$display_width x $display_height" );
$image->Set(background=>"lightblue", fill=>"blue", font=>"/usr/share/fonts/truetype/arial.ttf");
$image->Set(size=>"x114");			#overall height required
$image->Read("label:Yaba Daba Dooo");
$width = $image->Get('width');
$height = $image->Get('height');
$image->Read("label:".$width.'|'.$height);   #returned to check output as this is a CGI returning to a browser
Regards
Gordon McLean
clickdigital.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Best Fit Labels - Bounding Box

Post by snibgo »

I'm not sure I understand the problem, but this might help.

"label:" fits text into a box without adjusting the aspect ratio of the text. So unless you give exactly the correct size of box, it will fill either left-to-right but leave gaps at the top and bottom, or vice versa.

If you want to fit a certain height and width, by changing the aspect ratio of the text, you can make it larger than it needs to be and resize exactly downwards, eg with "-resize 114x1400!".
snibgo's IM pages: im.snibgo.com
clickdigital
Posts: 2
Joined: 2015-08-20T06:15:09-07:00
Authentication code: 1151

Re: Best Fit Labels - Bounding Box

Post by clickdigital »

As I understand the label best fit allows you to leave the size open on side either height or width and the label will then fill the space and adjust the size accordingly. What I was not expecting was the gap at top and bottom

i.e. $image->Set(size=>"x114");

Thanks for your help
Gordon
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Best Fit Labels - Bounding Box

Post by snibgo »

If you want your text to fit exactly 114 pixels high, and you don't want the text aspect ratio changed, I'd use something like this:

Code: Select all

convert -pointsize 200 label:"Yaba daba do" -trim +repage -resize x114 t1.png
... or ...

Code: Select all

convert -size x200 label:"Yaba daba do" -trim +repage -resize x114 t2.png
The values in -pointsize or -size aren't critical, provided they make the -resize shrink instead of enlarging.

These are command-line commands. Sorry, I don't know Perl.
snibgo's IM pages: im.snibgo.com
Post Reply