1

I am rather new to using the command line and php. That being said I have been trying to figure out how to use ImageMagick with the exec() function. I have this currently,

$command="/usr/local/lib/ImageMagick  convert images/a.pdf images/a.png"; 

if(exec($command)){
    echo 'yes';
}
else{
    echo 'no';
}

Which is returning 'no'. I believe I am missing something about how to execute convert from the correct directory. Is my $command set up properly? (I was given the path to ImageMagick from my web host, Lunarpages).

I have read through some of the other questions regarding ImageMagick but I haven't found much to help me set up my command.

Thanks for any help,
Levi

Levi
  • 12,214
  • 14
  • 43
  • 47

2 Answers2

4

What your command is currently attempting to do is execute a program named /usr/local/lib/ImageMagick which I am guessing is not what you were intending. If that is the path to ImageMagick and you want to use the convert utility you need to modify your command to the following:

/usr/local/lib/ImageMagick/convert images/a.pdf images/a.png

At which point it should work without any issues! You may want to dig further into what the convert command can do for you!

X-Istence
  • 16,324
  • 6
  • 57
  • 74
  • I tried this and I am still getting the error, I tried adding/removing forward slashes for paths to filenames such as 'images/a.pdf' to '/images/a.pdf'. It didn't work for me but do you think the paths could be read incorrectly? – Levi Nov 28 '09 at 05:26
  • 1
    Try adding a PHP diagnostic output `echo getcwd();` command to show the current working directory. Perhaps when `exec()` runs the working directory is not as expected (just above `images`). – wallyk Nov 28 '09 at 05:40
  • I am indeed in the correct spot, just above images (in /gallery/). However if I am just above images will exec() try and run from /gallery/usr/local/lib/ImageMagick/convert ? Or will exec() work from the root? – Levi Nov 28 '09 at 05:49
  • 1
    When you run exec() it will run in whatever the current working directory is. In this case in gallery. Exec should be giving you back any errors, so instead of doing an if() statement with just yes and no, echo exec(...) instead so you get to see the error. – X-Istence Nov 28 '09 at 16:27
  • Do note that on their Wiki they state a different path for ImageMagick: http://wiki.lunarpages.com/Special_Server_Paths – X-Istence Nov 28 '09 at 16:33
  • So try /usr/local/bin/convert instead of the above command and see if that works. – X-Istence Nov 28 '09 at 16:33
  • I tried to echo exec() and got nothing, just a blank page. I had sent in a ticket asking about ImageMagick, it can be accessed at the following paths:
    /usr/local/bin/
    /usr/lib/ImageMagick
    /usr/local/lib/ImageMagick
    /usr/share/ImageMagick
    If it is working in the current directory will exec('usr/local/bin/convert images/a.pdf images/a.png') try to find ImageMagick in /gallery/usr/local/lib/ImageMagick/ ?
    – Levi Nov 28 '09 at 20:49
  • The path is now correct, I have successfully converted a png to a jpg, I am still having trouble converting a pdf however, so I will post back when I figure it out. – Levi Nov 30 '09 at 14:40
0

use the exec() the correct way as your exec returns a string by default and the execution results is passed back via one of it's parameters as such :

$command="/usr/local/lib/ImageMagick/convert images/a.pdf images/a.png"; 

exec($command,$output,$result);
if ($result == true ){
    echo 'yes';
}
else{
    echo 'no, here's what happened with command output';
    print_r($output);
}

refer to http://php.net/manual/en/function.exec.php