Page 1 of 1

Best Fit Labels - Bounding Box

Posted: 2015-08-20T10:22:57-07:00
by clickdigital
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.

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.

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
Gordon McLean

Re: Best Fit Labels - Bounding Box

Posted: 2015-08-20T10:52:00-07:00
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!".

Re: Best Fit Labels - Bounding Box

Posted: 2015-08-20T12:38:20-07:00
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

Re: Best Fit Labels - Bounding Box

Posted: 2015-08-20T17:25:06-07:00
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.