Drew
Drew

Reputation: 6274

Escaping imagemagick text input with apostrophes

I'm drawing the text output from display-dhammapada to an existing image. The following CLI command works:

convert test.jpg 
    -pointsize 30 
    -draw "gravity northwest 
           fill black 
           text 120,200 '$( display-dhammapada)'" 
    result.png

...except when I receive a quote that has a contraction. Then the text input ends prematurely with an error such as:

convert: non-conforming drawing primitive definition `s' @ error/draw.c/DrawImage/3182

which was an apostrophe s case.

If I reverse the quotes from " ' ' " to ' " " ' then I end up with the literal text $( display-dhammapada) on my image.

What is the best way to sanitize my input in this case?

Upvotes: 0

Views: 389

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 208003

You could sanitise your quote through sed like this:

convert -size 1000x1000 xc:red -pointsize 30 -draw "gravity northwest fill black text 120,200 '$(./display-dhammapada|sed "s/'/\\\'/g")'" result.png

display-dhammapada

#!/bin/bash
printf "There's one there!"

enter image description here

FYI:

It may be an idea to write a general purpose sanitiser script that you can maintain and modify. You could consider not allowing it to pass @ as the first character which would make ImageMagick read a file and could permit attacks and you may not want to allow various other things through - e.g. trailing newlines, or semi-colons.

Upvotes: 1

Related Questions