ImageMagick - 文本转换为矩形

ewo*_*com 5 php imagemagick image-processing

我是ImageMagick的新手 - 我已经阅读了一些文档,现在我来到了第一个现实世界的情况.我有图像(300*500),我有一些文本需要尽可能最好地安装(最大可能)在20%的最顶层的图像.所以文本必须是300*100矩形,图像的其余部分保持不变.这甚至可以用图像魔法吗?什么是最好的方法来做到这一点

我正在寻找命令行或php扩展解决方案.简单的ilustration下面

简单的ilustration

Kur*_*fle 7

由于您没有提供用于测试和应用某些文本的示例图像,因此我使用以下命令创建了一个:

convert                               \
   http://i.stack.imgur.com/RfJG6.png \
  -crop 312x513+579+0 +repage         \
   so#12231624-right.png
Run Code Online (Sandbox Code Playgroud)

使用生成的图像作为输入,运行这三个命令以查看它是如何工作的(在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
Run Code Online (Sandbox Code Playgroud)

结果图像:

输出示例1 输出示例2

(输出与您给定的帧完全不匹配 - 但这只是因为我的测试文件仍然有一个白色的,未填充的边框(作为图像的一部分),我没有费心去除......)

换句话说:只是不用费心去指定任何字体大小-fontsize.仅给出应该具有文本注释的区域的大小.然后ImageMagick将自动选择最匹配的字体大小并使用它.