Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php exec command to convert an image

I'm trying to use the imagemagick command "convert" to create a .tif image from a .png one.

What I've come with is:

$exec = "/opt/local/bin/convert -adaptive-resize 150% ".$pos.".png ".$pos.".tif";
exec($exec);

If I run into CLI "which convert" I get that path: /opt/local/bin/convert . I've also tried without the path, only /opt/local/bin/convert -adaptive-resize 150% ".$pos.".png ".$pos.".tif and /etc/local/bin/convert -adaptive-resize 150% ".$pos.".png ".$pos.".tif.

If I'm running that command into the terminal it works as expected but when I'm trying to use it from the PHP script it doesn't work.

EDIT: I've also tried without success to create a .sh file with the following code:

#! /bin/bash
convert -adaptive-resize 150% 1.png 1.tif
convert -adaptive-resize 150% 2.png 2.tif
convert -adaptive-resize 150% 3.png 3.tif
convert -adaptive-resize 150% 4.png 4.tif
convert -adaptive-resize 150% 5.png 5.tif
convert -adaptive-resize 150% 6.png 6.tif
convert -adaptive-resize 150% 7.png 7.tif
convert -adaptive-resize 150% 8.png 8.tif
convert -adaptive-resize 150% 9.png 9.tif

If I run it from terminal it works like a charm. Instead if I try to execute it from a simple PHP file it doesn't create any .tif file.

<?php
$exec = "./convertpngtif.sh";
exec($exec);
?>
like image 213
Giorgio Avatar asked Nov 30 '22 03:11

Giorgio


1 Answers

The clue is in Hieu Nguyen answer above: when you run from command line with PHP -r, it works. When you run in the browser, it doesn't. This leads to one of three possible issues.

The critical thing to remember is that when you run exec, the script, user, path and working directory will be run with the same user/permissions that PHP is running at.

When running PHP in command line, PHP will be run as the user is who you are logged in as ("you"), and therefore exec() will also run "you". This will also be the same user that you use to write the shell script in. The permissions are therefore the same as yours, and it works (great).

When PHP runs in Apache (or IIS) then is may or may not be "you". In your case, it obviously isn't as the script doesn't work - PHP is probably running as "apache" or "http" or "www" or some similarly named user.

This leads to one of three possible issues. So what to check:

1) and 2) Path AND working directory.

Been suggested above one at a time, but it's safer to stipulate absolute paths (i.e. full paths, starting at root /) to all files - the executable command, the input file and the output file. This rules out any differences in paths or working directories that may arise. If you uses shell script, stipulate full paths to the shell script and full paths within it (or set paths / directories at the top)

3) Permissions

Again suggested above, but everyone's been concentrating on the files. But have you checked permissions of the convert executable command itself?

Also with the shell script, have you set permissions of that to executable for the user PHP is running as? rwxr--r-- owned by "you" isn't satisfactory if you're running as a different user from within apache.

There are a couple of other solutions if struggling with those:

  1. you can make PHP run as "you" even in Apache using fastCGI install. This way you know the user/path/working directory that you are running both PHP scripts as. (I'd still recommend full paths, but still alleviates permission issues)

  2. you can debug the script by logging in as "apache" or "www" or "http" etc and setting permissions / paths so your script now works.

like image 111
Robbie Avatar answered Dec 04 '22 05:12

Robbie