将文字放入框中

Pav*_* K. 7 svg imagemagick autoresize

在我的网站上,我允许用户使用他们在图片上指定的文字行创建图片

目前我用于imagemagick转换 - 我指定svg模板,让转换做其余的

这是代码的一部分,负责输出图片中的文本

  <text text-anchor="middle" x="50%%" y="%s"
        font-family="Times New Roman" font-size="55"
        style="fill:rgb(255,255,255);">
    %s
  </text>
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果用户提供非常长的字符串,则文本不适合图像.

如果文本不适合图片,我希望文本自动调整为较小的字体.是否可以使用svg模板?如果没有,可能是其他解决方案

Pav*_* K. 2

通过放弃 svg 并使用 imagemagick Convert 和 mvg 模板完成所有操作来解决

这是简化的脚本示例,以防有人追求类似的东西

脚本正在画布上放置图像和标题。标题是使用单独的转换命令创建的,保存为临时文件,然后放在画布上

#!/bin/sh

TEMPLATE="
push graphic-context
viewbox 0 0 600 600 
affine 1 0 0 1 0.0 0.0
push graphic-context
fill 'rgb(0,0,0)'
rectangle 0,0 600,600
pop graphic-context
push graphic-context
image Over 38,38 338,338 '%s' 
pop graphic-context
push graphic-context
image Over 36,400 529,55 '%s' 
pop graphic-context
pop graphic-context
";

#1. creating label fitting specified proportions 
#2. converting mvg template to jpeg image (and trimming it in process) 
#3. removing temp file with label

convert -size 529x55 -background black -family "Times New Roman" -gravity center -fill white label:"$2" "tmp/$1title.jpg" && printf "$TEMPLATE"  "images/$1.jpg" "tmp/$1title.jpg"  | convert mvg:- -trim +repage -bordercolor black -border 36  "images/$1converted.jpg" && rm "tmp/$1title.jpg"

#script parameters: $1 is image id, $2 is title text
Run Code Online (Sandbox Code Playgroud)