0

我有一个脚本,它将 ImageMagick 命令行命令放在一起以对 PDF 文档进行处理。这些最终会非常长,但这是一个此类命令的代表性示例(为便于阅读,添加了行返回:

 magick.exe -density 300 -compress ZIP ( "c:\IMtemp\32.pdf[0]" -fill "#000" -stroke "#062430" -font Arial
 -pointsize 12 -draw "text 535,515 'This is my test text'") "c:\IMtemp\final.pdf"

该命令工作正常。但是,文本是动态的并且来自用户输入。如果用户要包含撇号,命令行最终将是:

 magick.exe -density 300 -compress ZIP ( "c:\IMtemp\32.pdf[0]" -fill "#000" -stroke "#062430" -font Arial
 -pointsize 12 -draw "text 535,515 'This is my test text; it's just great'") "c:\IMtemp\final.pdf"

当然,这会失败,因为撇号过早地结束了文本块。我的第一个想法是像这样逃避撇号:

 magick.exe -density 300 -compress ZIP ( "c:\IMtemp\32.pdf[0]" -fill "#000" -stroke "#062430" -font Arial
 -pointsize 12 -draw "text 535,515 'This is my test text; it\'s just great'") "c:\IMtemp\final.pdf"

但这不起作用。相反,撇号被忽略,并且出现文本“这是我的测试文本;它非常棒”。所以然后我想也许我可以使用替代字符并尝试了这个:

 magick.exe -density 300 -compress ZIP ( "c:\IMtemp\32.pdf[0]" -fill "#000" -stroke "#062430" -font Arial
 -pointsize 12 -draw "text 535,515 'This is my test text; it’s just great'") "c:\IMtemp\final.pdf"

这导致文本“这是我的测试文本;它非常棒”,我假设是因为 IM 没有默认为 UTF-8。我在文档中读到,这可以通过向 IM 提供代码来规避并尝试过:

 magick.exe -density 300 -compress ZIP ( "c:\IMtemp\32.pdf[0]" -fill "#000" -stroke "#062430" -font Arial
 -pointsize 12 -draw "text 535,515 'This is my test text; it\u2019s just great'") "c:\IMtemp\final.pdf"

 magick.exe -density 300 -compress ZIP ( "c:\IMtemp\32.pdf[0]" -fill "#000" -stroke "#062430" -font Arial
 -pointsize 12 -draw "text 535,515 'This is my test text; it\x{2019}s just great'") "c:\IMtemp\final.pdf"

但是这些只是将反斜杠之后的所有内容显示为纯文本。

我不在乎如何保留撇号,只要其中的任何内容看起来都足够正确以供人类阅读和专业。我如何实现这一目标?

我在 Lucee 服务器上通过 cfExecute 运行 IM(在 Windows / IIS 上运行)。

4

2 回答 2

2

卷曲单引号适用于我的 Mac OSX Sierra 和 ImageMagick 7.0.11.3 上的 ImageMagick

magick -size 500x100 xc:white -fill "#000" -stroke "#062430" -font Arial -pointsize 18 \
-draw "text 30,30 'This is my test text; it’s just great'" x.png

在此处输入图像描述

于 2021-03-11T16:43:11.510 回答
2

您可以通过将注释文本文件或从stdin. 因此,如果您创建一个名为 say 的文件,"annotation.txt"其中包含:

Weird stuff with ', @ and ".

您可以告诉ImageMagick从该文件中读取注释(使用@filename)并将其放置在您的图像上,如下所示:

magick -size 640x240 xc:magenta -gravity center -pointsize 32 -annotate -40+90 @annotation.txt result.png

在此处输入图像描述


同样,如果您想从其他命令将注释泵入ImageMagick ,您可以告诉ImageMagick像这样读取它stdin

echo "Whatever @ '" | magick -size 640x240 xc:magenta -gravity center -pointsize 32 -annotate -40+90 @- result.png

在此处输入图像描述


正如 Fred 在评论中提醒我的那样,您需要确保您的policy.xml文件允许这样做。它的位置可以通过以下方式找到:

identify -list configure | grep CONFIGURE_PATH

更多关于如何编辑和安全隐患的讨论在这里

于 2021-03-11T18:28:44.177 回答