ImageMagick - 文本为矩形 [英] ImageMagick - Text into rectangle

查看:151
本文介绍了ImageMagick - 文本为矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

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

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

使用生成的图像作为输入,运行这三个命令以查看它是如何工作的(在Linux或Mac OS X上):

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

结果图片:

 

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

(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...)

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

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.

这篇关于ImageMagick - 文本为矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆