2

This works within my PHP code

$output = array();
exec("ls /Applications/XAMPP/htdocs/MY_APP/images", $output);
var_dump($output);

Now I need to use ImageMagick's "convert" command to convert a PNG file to PDF file. But the following doesn't do anything and returns no errors.

$output = array()
exec("convert /Applications/XAMPP/htdocs/MY_APP/images/test.png /Applications/XAMPP/htdocs/MY_APP/images/test.pdf", $output);
var_dump($output);

Is it a permission issue? I gave chmod 777 to the images folder. What else should I check? When I run the command from the terminal, it works fine.

6
  • Does your Apache user have permissions to run convert? Commented Feb 15, 2013 at 5:03
  • What version of PHP version is this? This sounds like a lot like the old issues with safe_mode. Commented Feb 15, 2013 at 5:07
  • does your program work when you run it from the command line as root? what about as another user? Commented Feb 15, 2013 at 5:08
  • Do I need to give a specific permission to run a specific command?? Commented Feb 15, 2013 at 5:18
  • My PHP version is 5.3.15 - in which the safe mode has been deprecated - right? Commented Feb 15, 2013 at 5:18

4 Answers 4

2

Try changing convert to the full path to convert e.g. usr/local/bin/convert

This can be found with:

<?php
echo "<pre>";
system("type convert"); 
echo "</pre>";
?>

or

<?php
echo "<pre>";
system('which convert',$path); print_r($path); 
echo "</pre>";
?> 

I have just reread your post and notice you are on XAMPP and so the answer above may not work.

Try putting everything in the same folder to cut out any problems with paths etc.

exec("convert test.png test.pdf");

Also do you have ghostscript installed? To prove it works and not a ghostscript problem try saving as a jpg instead of a pdf.

Sign up to request clarification or add additional context in comments.

Comments

1

If your PHP code is being executed via you web server (e.g. Apache) then the Apache process (httpd) might be running under a restricted unix user (e.g. apache or httpd). That restricted unix user wouldn't usually have write permissions in /Applications/XAMPP/htdocs/MY_APP/images/ directory where you're trying to generate pdf. Try this command instead and see if this works:

exec("convert /Applications/XAMPP/htdocs/MY_APP/images/test.png /tmp/test.pdf", $output);
var_dump($output);

Above command tries to generate pdf in /tmp directory which usually is writable for Apache unix user.

I note that you've given 777 to images folder. Pls understand that chmod 777 to images folder alone is not enough. You need to give write permission to Apache user in all parent directories as well e.g. MY_APP, htdocs, XAMPP, Applications etc which is a big security risk, I must add.

7 Comments

he says he set chmod 777 on the folder, allowing writing for himself, the group and other.
@user1073122 chmod 777 in images folder is not enough. OP needs to give write permission to Apache user in all parents directories as well.
I tried to "/tmp/test.pdf", but it didn't work. I just get the output printed out as "array(0) { }"
I understand the security risk. So I wouldn't mind just making it work into the tmp folder. But that doesn't seem to be working either.
Ok can you try putting ini_set('safe_mode', 'Off'); at the start of your script.
|
1

I have had the same problem: safe mode was off, but still I couldn't execute Image Magick's convert.

I found out that, for some reason, php can't see the script (even including the path in include_path through php.ini). Checking the log, I found that the error was:

sh: convert: command not found

Trying to call the script using the fullpath (I checked it using which convert) I finally managed to make it run normally through exec.

So, supposing the convert fullpath is /usr/local/bin, try running the following:

<?php    
    $output = array();
    exec("/usr/local/bin/convert /Applications/XAMPP/htdocs/MY_APP/images/test.png /Applications/XAMPP/htdocs/MY_APP/images/test.pdf", $output);
    var_dump($output);
?>

Comments

0

If you have this problem under Windows, try exec('identify pathtoyourfile'). If that works but exec('convert ...') does not you are probably inadvertently calling Window's convert utility instead of IM's convert. The solution is to rename IM's convert to, say, cconvert. Alternatively, check if IM is at the beginning of your PATH environment variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.