how can I best fit (largest possible) text in a rectangle box?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
bikash00789
Posts: 5
Joined: 2016-04-05T12:09:53-07:00
Authentication code: 1151

how can I best fit (largest possible) text in a rectangle box?

Post by bikash00789 »

I am very new imagemagick in PHP code and I am trying to best fit (largest possible) in a rectangle box, width and height would be dynamically given, for now:100px , height:200px , depending on the string lenght it should best fit into it? I found lot of GD code but did not understand. Please help me on this?e on this?
http://stackoverflow.com/questions/1223 ... -rectangle

like this? but not able to put in imagemagick
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how can I best fit (largest possible) text in a rectangle box?

Post by fmw42 »

See label: or caption: or pango: at http://www.imagemagick.org/Usage/text/

Please always provide your IM version and platform, since syntax may differ.
bikash00789
Posts: 5
Joined: 2016-04-05T12:09:53-07:00
Authentication code: 1151

Re: how can I best fit (largest possible) text in a rectangle box?

Post by bikash00789 »

@fmw42: what code it is .. its not php? can you give me some idea?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how can I best fit (largest possible) text in a rectangle box?

Post by fmw42 »

Imagemagick is not PHP code. It is command line based. You can, however, call the command line in PHP exec. Or you can use one of the PHP API programs, such as Imagick.

If you use PHP exec, you can do this, for example as:

Code: Select all

<?php
exec("path2/convert -size WxH -background somecolor -fill someothercolor -font path2/somefont.ttf caption:"Your Text Here" result.png 2>&1",$out,$returnval);
foreach($out as $text)
{echo "$text<br>";}
?>
where WxH is your desired size, somecolor and someothercolor are any valid IM color and somefont.ttf is your desired font file.

see http://www.imagemagick.org/Usage/text/#caption_bestfit and http://www.imagemagick.org/script/color.php

If you want to use Imagick (PHP API), then see http://us3.php.net/manual/en/book.imagick.php
Post Reply