There should be a whole bunch of escapeshellarg() in there. Who knows what the text contains? Code like this is exactly why PHP devs get a bad rep. Please don't post PHP code online unless you've made it reasonably secure, because somebody somewhere is going to copy that and get pwned.
Also, why not use PHP's own imagick extension? It exposes a neat object-oriented interface, it will be much easier for people to read and tweak, and you won't have to escape anything.