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"

Need Your Help

Sync data on android

android web-services android-asynctask sync getjson

Currently, I'm working on android app that displays reports from my local server with public IP address. I've already designed the web services (C#) to access the data from the SQL Server from my s...

php screws up utf-8 charachters from mysql database using mysqli

php mysql utf-8 mysqli cjk

I have some chinese characters (e.g. 中文(简体)) stored in a mysql database with content type utf8_bin.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.