5

私はImageMagickに非常に慣れていません-私はいくつかのドキュメントを読みました、そして今私は最初の現実世界の状況に来ました。私は画像(300 * 500)を持っており、画像の最上部の20%に可能な限り(可能な限り最大で)合わせる必要のあるテキストがあります。したがって、テキストは300 * 100の長方形である必要があり、画像の残りの部分は同じままです。これはimagemagickでも可能ですか?これを行うための最良の方法は何ですか

コマンドラインまたはphp拡張ソリューションを探しています。以下の簡単な図解

シンプルなイラスト

4

1 に答える 1

8

テストしてテキストを適用するためのサンプル画像を提供しなかったため、次のコマンドを使用してサンプル画像を作成しました。

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

結果の画像を入力として使用して、次の3つのコマンドを実行し、画像がどのように機能するかを確認します(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

結果の画像:

出力例1 出力例2

(出力は指定されたフレームと正確に一致しませんが、これは、テストファイルに(画像の一部として)白く塗りつぶされていない境界線が残っているためです。これはわざわざ削除しませんでした...)

言い換えると、。を使用してフォントサイズを指定する必要はありません-fontsize。テキスト注釈が必要な領域のサイズのみを指定してください。次に、ImageMagickは自動的に最適なフォントサイズを選択して使用します。

于 2012-09-01T23:38:41.470 に答える