PHP escapeshellcmd() breaks ImageMagick convert command using '>' character
I'm using ImageMagick 6.6.9 on an Ubuntu 12 server. The problem I have is that my ImageMagick convert command, which I run through PHP using the shell_exec function (but have also tried exec), has characters in it which the PHP escapeshellcmd function escapes with a \
Here is my PHP code:
$result = exec(escapeshellcmd($convertString));
Here is my sample convert command:
/usr/bin/convert "/Users/rich/Sites/example/1234.JPG" -quality 85 -auto-orient -thumbnail "640x88>" "/Users/rich/Sites/example/1234-thumb.jpg"
And finally here is the convert command after it has been run through escapeshellcmd:
/usr/bin/convert "/Users/rich/Sites/example/1234.JPG" -quality 85 -auto-orient -thumbnail "640x88\>" "/Users/rich/Sites/example/1234-thumb.jpg"
The problem is that the escaped \> character results in an ImageMagick error:
convert: invalid argument for option -thumbnail': 640x88> @ error/convert.c/ConvertImageCommand/2770.
Does anyone know of a way that I can get around this? I have trawled the ImageMagick documentation and while they acknowledge the problem they don't seem to provide any other way of resizing an image without using the special unix characters:
The Only Shrink Flag ('>' flag) is a special character in both UNIX Shell and in Window batch scripts, and you will need to escape that character (using backslash '>' in shell, and '^>' in windows batch). It is also special in and HTML web pages, so PHP scripts also may need some special handling.
Many thanks in advance.
It turns out that I had extra " (quote marks) around the -thumbnail "640x88\>" size options. For reference, the correct, escaped convert command should have been:
/usr/bin/convert "/Users/rich/Sites/example/1234.JPG" -quality 85 -auto-orient -thumbnail 640x88\> "/Users/rich/Sites/example/1234-thumb.jpg"