Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php ImageMagick-将文本转换为矩形_Php_Image Processing_Imagemagick - Fatal编程技术网

Php ImageMagick-将文本转换为矩形

Php ImageMagick-将文本转换为矩形,php,image-processing,imagemagick,Php,Image Processing,Imagemagick,我对ImageMagick非常陌生——我已经阅读了一些文档,现在我了解了第一个真实世界的情况。我有图像(300*500),我有一些文本,需要尽可能最好地(尽可能最大地)在图像顶部的20%中进行拟合。因此文本必须在300*100的矩形中,图像的其余部分保持不变。这甚至可以通过图像魔法实现吗?最好的方法是什么 我正在寻找命令行或php扩展解决方案。 下面是简单的说明 由于您没有提供用于测试和应用某些文本的示例图像,因此我使用以下命令创建了一个示例图像: convert

我对ImageMagick非常陌生——我已经阅读了一些文档,现在我了解了第一个真实世界的情况。我有图像(300*500),我有一些文本,需要尽可能最好地(尽可能最大地)在图像顶部的20%中进行拟合。因此文本必须在300*100的矩形中,图像的其余部分保持不变。这甚至可以通过图像魔法实现吗?最好的方法是什么

我正在寻找命令行或php扩展解决方案。 下面是简单的说明


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

convert                               \
   http://i.stack.imgur.com/RfJG6.png \
  -crop 312x513+579+0 +repage         \
   so#12231624-right.png
使用生成的图像作为输入,运行以下三个命令以查看其工作方式(在Linux或Mac OS X上):

结果图像:

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

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

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