Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageMagick - Text into rectangle

I'm very new to ImageMagick - I've read some documentation and now I came to first real world situation. I have image (300*500) and I have some text which need to be fitted as best as possible (biggest possible) in 20% of the top most of image. So text has to be in 300*100 rectangle the rest of the image stays the same. Is this even possible with image magick? Whats the best way to do this

I'm looking for command line or php extension solution. Simple ilustration below

Simple ilustration

like image 808
ewooycom Avatar asked Sep 01 '12 21:09

ewooycom


1 Answers

Since you didn't provide a sample image for testing and applying some text to, I created one with the following command:

convert                               \
   http://i.stack.imgur.com/RfJG6.png \
  -crop 312x513+579+0 +repage         \
   so#12231624-right.png

Using the resulting image as an input, run these three commands to see how it would work (on Linux or Mac OS X):

width=$(identify -format %W so#12231624-right.png)

convert                  \
  -background '#0008'    \
  -gravity center        \
  -fill white            \
  -size ${width}x100     \
   caption:"This is a sample text to test \
the automatic sizing of fonts by ImageMagick." \
   so#12231624-right.png \
 +swap                   \
 -gravity north          \
 -composite              \
  output1.png

convert                  \
  -background '#0008'    \
  -gravity center        \
  -fill white            \
  -size ${width}x100     \
   caption:"This is a even longer sample text. \
It also serves to test if automatic sizing of fonts \
by ImageMagick works as expected: just don't specify \
any fontsize, and let ImageMagick go for the best fit..." \
   so#12231624-right.png \
 +swap                   \
 -gravity north          \
 -composite              \
  output2.png

Resulting images:

output example 1 output example 2

(The output doesn't match exactly your given frame -- but that's just because my test file still has a white, unfilled border (as part of the image) which I didn't bother to remove...)

In other words: just do not bother to specify any font size using -fontsize. Only give the size of the region which should have the text annotation. Then ImageMagick will automatically pick the best matching font size and use it.

like image 138
Kurt Pfeifle Avatar answered Sep 27 '22 23:09

Kurt Pfeifle