【问题标题】:fitting text into the box将文本放入框中
【发布时间】:2011-08-21 11:44:39
【问题描述】:

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

目前我使用该 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>

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

如果不适合图片,我希望文本自动调整为更小的字体。可以使用 svg 模板吗?如果没有,还有什么其他解决方案

【问题讨论】:

标签: svg imagemagick autoresize


【解决方案1】:

您可以在 SVG 模板中添加一个脚本,该脚本在 SVG 加载时调用,并使用 getComputedTextLength() 调整字体大小。这是一个有点老套的解决方案,但它似乎有效。

这是一个简单的示例,它在其中绘制一个框和一些文本。无论文本有多长,都应调整文本大小以始终适合框(至少到点):

要在加载 SVG 时调用代码,请在 SVG 标记中包含 onload="int(evt)"

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> 
<svg xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     width="400" height="80"
     onload="init(evt)"> 

然后是实际的脚本:

  <script type="text/ecmascript">
    <![CDATA[
    
    function init(evt)
    {
        if ( window.svgDocument == null )
        {
            svgDocument = evt.target.ownerDocument;
        }

        maximum_length = 300;
        my_text = svgDocument.getElementById('text-to-resize');

        for (var font_size=55; font_size>0; font_size--)
        {
            if(my_text.getComputedTextLength() < maximum_length){break;}
            my_text.setAttributeNS(null, "font-size", font_size);
        }

    }
    
    ]]>
  </script>

我只是使用了一个 for 循环来减小字体大小,直到文本长度小于指定的最大值;我确信有更好的方法来调整文本大小。

最后是实际的文本和框:

<rect id="rect1" x="20" y="10" width="320" height="50" fill="white" stroke="black"/>
<text id="text-to-resize"
      text-anchor="middle"
      x="170" y="50"
      font-family="Times New Roman" font-size="55">
 whatever text
</text>
</svg>

如果您更改文本,字体大小应更改以适合框内。您可能还想更改 x 和 y 值以正确居中文本。

【讨论】:

  • 啊,这将是一个很好的解决方案,但不幸的是 imagemagick convert 在将 svg 转换为 jpg 时不执行 ecmascript :(
  • 哦,真可惜。如果没有 Ecmascript,我不知道该怎么做。
【解决方案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

【讨论】:

    猜你喜欢
    • 2021-11-30
    • 2010-12-19
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多