簡體   English   中英

在 ImageMagick 中處理文本中的撇號的正確方法是什么

[英]What is the correct way to handle apostrophes in text in ImageMagick

我有一個腳本將 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 上運行)。

卷曲單引號在我的 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

在此處輸入圖片說明

您可以通過將注釋文本從文件或從stdin ImagMagick來避免轉義和引用的所有問題。 因此,如果您創建一個名為"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

更多關於如何編輯和安全影響的討論在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM