将文字装入框中 [英] fitting text into the box
问题描述
在我的网站上我允许用户使用他们在图片上指定的文本行创建图片
on my website i allow users to create pictures with line of text they specify drawn on the picture
目前我用于该imagemagick转换 - 我指定了svg模板和让转换做其余的
currently i use for that imagemagick convert - i specify svg template and let convert do the rest
这里是代码的一部分,它负责输出图片中的文字
here is part of code which is responsible for outputting text in the picture
<text text-anchor="middle" x="50%%" y="%s"
font-family="Times New Roman" font-size="55"
style="fill:rgb(255,255,255);">
%s
</text>
我的问题是,如果用户提供非常长的字符串,则文本不适合图像。
my problem is that if user provides very long strings, the text doesn't fit in the image.
如果文字不适合图片,我希望文字自动调整为较小的字体。是否可以使用svg模板?如果不是,可能是其他解决方案
i'd like text to be resized automatically to smaller font if it doesn't fit the picture. is it possible to do with svg templates? if not, what could be other solutions
推荐答案
通过放弃svg并使用imagemagick convert和mvg template完成所有事情来解决
solved by abandoning svg and doing everything with imagemagick convert and mvg template
这里是简化的脚本示例,以防任何人追求类似的东西
here's the simplified script example in case anyone's pursuing something similar
脚本在画布上放置图像和标题。标题是用单独的转换命令创建的,保存为临时文件然后放到画布上
script is putting an image and a title on the canvas. title is created with separate convert command, saved as temporary file and then put onto the canvas
#!/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
这篇关于将文字装入框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!