Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

php - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...