2012-09-01 46 views
5

我對ImageMagick很新 - 我讀過一些文檔,現在我來到了第一個真實世界的情況。我有圖像(300 * 500),並且我有一些文字需要儘可能最好(最大可能)在最上面的圖像的20%中進行裝配。所以文本必須在300 * 100的矩形中,圖像的其餘部分保持不變。這是甚至可能與圖像magick?什麼是最好的辦法做到這一點ImageMagick - 文本到矩形

我正在尋找命令行或PHP擴展解決方案。下面

Simple ilustration

回答

7

簡單ilustration既然你沒有提供用於測試和應用一些文本樣本圖像,我創建了一個與下面的命令:

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

使用所得到的圖像輸入,運行這三個命令,看看它是如何工作的(在Linux或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 

點所得的圖象:

output example 1   output example 2

(輸出不完全匹配您給出的框架 - 但是這只是因爲我的測試文件仍然有一個白色的,未填充邊界(如圖像的一部分)我不打算刪除...)

換句話說:只是不打擾指定任何字體大小使用-fontsize。只給出應該有文本註釋的區域的大小。然後ImageMagick會自動選擇最匹配的字體大小並使用它。