4

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);
?>
Giorgio
  • 1,603
  • 5
  • 29
  • 52
  • do a var_dump($exec) to see what is generated, might be a problem with spaces – JorgeeFG Jul 15 '13 at 12:50
  • string(56) "/opt/local/bin/convert -adaptive-resize 150% 1.png 1.tif" – Giorgio Jul 15 '13 at 12:52
  • try these: `error_reporting(E_ALL); var_dump($_SERVER); echo getcwd();`, also look at apache's error.log. – rr- Jul 15 '13 at 12:53
  • So is 1.png in the same path of the script? – JorgeeFG Jul 15 '13 at 12:53
  • 1
    Does 1.png have read permissions for user www-data? or even has it write permissions in the directory? (for creating the new file) (if you are in linux) – JorgeeFG Jul 15 '13 at 12:55
  • I'm on a mac platform. How can I check it? – Giorgio Jul 15 '13 at 12:58
  • Open a terminal, `cd` into the directory and do `ls -la`. Look at the permissions of `.` and `1.png`. – Cobra_Fast Jul 15 '13 at 13:00
  • `ls -al DIRECTORY|grep '\s\./'|cut -d ' ' -f 1` will show you permissions of given DIRECTORY. – rr- Jul 15 '13 at 13:01
  • 2
    permission of . drwxrwxrwx, permission of 1.png -rw-r--r-- – Giorgio Jul 15 '13 at 13:05
  • This script used to work with an older version of MAMP. Now I've updated it and doesn't work anymore. Makes me think that there are permissions problems. – Giorgio Jul 15 '13 at 14:39
  • Question edited with new details... – Giorgio Jul 16 '13 at 08:37
  • What if you try `__DIR__` . '/myscript.sh'? – hek2mgl Jul 17 '13 at 17:36
  • @hek2mgl I've tried but it still doesn't work. – Giorgio Jul 17 '13 at 18:45
  • Is there an error when you execute the command (`echo exec($exec);`)? Your PHP script may not have access to `/opt/local/bin/convert` when running as your HTTP server (as opposed to CLI mode). Such an error would show in the return value of `exec()`. – RandomSeed Jul 19 '13 at 14:24
  • Do spaces appear in $post? If true, please use escapeshellcmd(): http://www.php.net/manual/de/function.escapeshellcmd.php – SteAp Jul 19 '13 at 22:34
  • Are you even in the right folder? Does `` point to the right directory? Does `` return `1`? – yunzen Jul 22 '13 at 09:47
  • I'm trying this solution: http://stackoverflow.com/questions/7163497/resolved-mamp-php-cant-exec-convert-after-homebrew-imagemagick-install – Giorgio Jul 24 '13 at 16:36
  • Finally found the solution. You can find it here: http://stackoverflow.com/questions/7163497/resolved-mamp-php-cant-exec-convert-after-homebrew-imagemagick-install – Giorgio Jul 25 '13 at 16:00

10 Answers10

8

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.

Robbie
  • 17,605
  • 4
  • 35
  • 72
2

Consider using php imagick instead of exec function.

mr. Pavlikov
  • 982
  • 5
  • 7
2

This looks like a permission issue to me. First you need to debug your php call by using system() instead of exec() (to get the full output) on terminal:

php -r 'system("/opt/local/bin/convert -adaptive-resize 150% 1.png 1.tif");'

I bet then you will see the exact error. There are 2 cases in my opinion:

  • The destination folder doesn't have write permission, in that case you might want to change that folder's permission to 777.

  • The /opt/local/bin/convert doesn't have execute permission, in that case you have to check each folder permission from /opt to /opt/local, etc. and give each of them at least permission 755.

In both cases, use ls -la to get permission of each folder.

Hope this helps.

Hieu Nguyen
  • 8,563
  • 2
  • 36
  • 43
  • When I run this command everything works as expected and the tif file is created. It gives me no errors. – Giorgio Jul 23 '13 at 23:30
2

I would use PHP imagemagick's interface for this. Something like this:

<?php
$image = new Imagick( "image.png" );
$height=$image->getImageHeight()*1.5; // 150% size
$width=$image->getImageWidth()*1.5;  // 150% size
$image->resizeImage( $width, $height,  imagick::FILTER_LANCZOS, 1 );
$image->setImageFormat( "tif" );
$image->writeImage( "image.tif" );
$image->clear(); 
$image->destroy(); 
?>
Arnestig
  • 2,285
  • 18
  • 30
2

THIS ANSWER WORKS ON LINUX/MAC

I know this is an old question - but I had a similar problem. A way to execute shell commands without having path issues is to create a new login shell use

/bin/bash -l 

so bash acts as like it is a login shell and all pathinfo is set. use -c to add the command as string

$exec = "/bin/bash -lc 'convert -adaptive-resize 150% ".$pos.".png".$pos.".tif'";
exec($exec,$out,$rcode);

you can use this to get the path to convert by

/bin/bash -lc 'which convert' 

and work without the bash part

It is still required that the permissions to convert are set and that the user php runs on is allowed to login. So a login shell in /etc/passwd (ubuntu/debian) must be set

lumos0815
  • 3,908
  • 2
  • 25
  • 25
1

As far as i see, you're trying to use ImageMagick to do convert an image.

PHP has an Imagick library which needs to be installed. If you have root access, you can easily do it, and use the imagick library wrapper functions to convert the image (no need to run command line).

Here is the documentation to php Imagick

You can check with phpinfo(); if imagick is installed.

beerwin
  • 9,813
  • 6
  • 42
  • 57
  • 1
    `Imagick` is nothing but a wrapper around the executable, OPs problem should persist... – Cobra_Fast Jul 15 '13 at 12:59
  • It's NOT a wrapper around the executable. If you read the source code of Imagick (which you can, as it's open source), you will see that. ( https://github.com/mkoppanen/imagick/ ) – griffin Nov 29 '13 at 13:51
1

See if you have correct permissions for write.

In linux common username for Apache server is www-data

In the console:

cd /var/www (or where you are saving your php files)
ls -la (You will see permissions)
sudo chown www-data:youruser thisfolder (change primary user to www-data and group to youruser)
sudo chmod 775 thisfilder (change permissions, write read exec for user and usergroup and read for others.
JorgeeFG
  • 5,651
  • 12
  • 59
  • 92
  • So, if my username is Giorgio. I would be typing: sudo chown www-data:Giorgio thisfolder ? I've tried to type the path instead of "thisfolder". But I get this error chown: Giorgio : invalid argument. – Giorgio Jul 15 '13 at 14:41
  • In the console write `groups` or `groups giorgio` to see your gid info. http://stackoverflow.com/questions/350141/in-unix-linux-how-to-you-find-out-what-group-a-given-user-is-in-via-command-line – JorgeeFG Jul 15 '13 at 14:50
  • Sorry if I'm asking again Jorge. when I type groups I get "staff com.apple.sharepoint.group.1 _lpadmin _appserveradm _appserverusr admin". What should I do now? – Giorgio Jul 15 '13 at 17:27
  • Maybe you are from group staff or admin. It doesnt matter that much, the important one is the www-data that is the apache, dont know if changes in Mac. To test you can just give 777 permissions to the folder and files (chmod -R 777 thefolder) without making chown – JorgeeFG Jul 15 '13 at 17:54
1

You may also try like this to execute shell script in PHP:

<?php echo shell_exec('sh ./convertpngtif.sh');  ?>

or

<?php
$exec = file_get_contents("./convertpngtif.sh");
echo shell_exec($exec);
?>
Balaji Kandasamy
  • 4,446
  • 10
  • 40
  • 58
1

I would diagnose the error by printing the error message first:

$out, $ret;
$exec = "/opt/local/bin/convert -adaptive-resize 150% ".$pos.".png ".$pos.".tif 2>&1";
exec($exec, $out, $ret);
var_dump($out);

It is VERY possible for imagemagick to cease running from exec() even though it runs properly from CLI because of permission problem, as other people have stated.

Ismail Faruqi
  • 482
  • 4
  • 16
0

How about using a temporary target file:

$source = dirname( __FILE__ ) . '/' . $pos.".png";
$target = tempnam( '/tmp/', 'tif-' );
rename( $target, $target . '.tif' );
$target .= '.tif';

$command = '/opt/local/bin/convert';

assert( file_exists( $command ));
assert( file_exists( $source ));
assert( is_readable( $source ));
assert( ! file_exists( $target ));

$exec = $command . " -adaptive-resize 150% ". escapeshellarg( $source ) . " " . escapeshellarg( $target );
exec( $exec );
SteAp
  • 11,853
  • 10
  • 53
  • 88