php shell_exec doesn't work with imagemagick commands - php

I try to convert a rgb color to transparent
When i use this code in the command line, it works perfect.
convert -transparent 'RGB(249,249,255)' /home/me/web/my.png /home/me/web/mynew.png
But when i use it with php:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$output = shell_exec("convert -transparent 'RGB(249,249,255)' /home/me/web/my.png /home/me/web/mynew.png");
?>
i get the error:
convert: unable to open image `/home/me/web/mynew.png': # error/blob.c/OpenBlob/2498.
Has anyone a idea?

you have to use relative paths to your homedir (because of shell_exec) otherwise you have to use absolute paths.
But why dont you use imagemagick for php?

To me that looks like an access-rights issue. Your web server tries to write a file inside your home directory.
Normally your home-directory is protected in such a way that no one except yourself can write files in there. Therefore your web server can not create the new file while you can when using the terminal.
Check the access rights to the folder that shall contain the new image and allow the user that is running the web server to write to that folder.

Related

output file directory using ghostscript

I needed to parse a PDF file to images using php. I've done it with the help of ghostscript. Here is the script:
$result = exec("gs -sDEVICE=jpeg -sOutputFile=%03d.jpeg $pdfname.pdf");
But I need the final images to be in another folder. I've tried writing -sOutputFile=/img/%03d or -sOutputFile=img/%03d and in a lot of similar ways...but the program doesn't even work when I add these things.
You need to create the /img/ or ./img directories before you run Ghostscript.
Ghostscript will not automatically create them if they don't exist.

imagemagick convert does not work through php

imagemagick convert does not work through php shell_exec but does through a shell.
In a shell convert file.pdf file.png works fine. But when I have this within a php file as shell_exec('convert file.pdf file.png'); Then I get no output! I have the permissions to do this, so I think it isn't that that's the problem; I have checked the directory I am in by way of getcwd() and this is also ok.
I know shell_exec works because I have used it earlier in the code and that works fine.
Any ideas?
I got the solution thanks to Crontab from another thread. I quote from there:
[I]f you're trying to convert a PDF to a PNG using ImageMagick ...
even using the full path to ImageMagick's convert won't work if the
script's PATH doesn't contain the path location to Ghostscript also.
Without messing with any user paths, you could add:
putenv("PATH=/usr/local/bin:/usr/bin:/bin");
Or something similar depending on your setup. The gs executable has
to be in your script user's path somewhere or ImageMagick will fail to
convert PDF or EPS files.
Try the full path to convert, i.e. shell_exec('/usr/bin/convert file.pdf file.png);. You can use which convert to find the location on your system.
There are several reason why this could happen, but I suggest reading this page and the user comments:
http://php.net/manual/en/function.shell-exec.php

PHP exec image processing not working within single.php

I am trying to process an externally hosted image using a php command.
Here is the code:
exec('convert -resize "568x150" http://www.temp.com/temp.jpg scripts/newtemp.jpg');
If I run this command in it's own file called for example test.php in my wordpress directory then it works fine, resizes the image and saves it into the /scripts folder as newtemp.jpg
The problem occurs when I put this command into my single.php, then the codes doesn't seem to work and the image is not saved.
Is there an obvious reason this wont work within the single.php?
Convert is ImageMagick, right? You could just use its native php extension and don't have to hassle with exec-ing it yourself.

dynamic image crop in php

hi i am using smart resizer (http://shiftingpixel.com/2008/03/03/smart-image-resizer/).
when i am giving path of files which exist on my server .this script gives me proper result.
but i path of another server , in this case this script is not giving proper result.
For using this script i have to pass this value in img src like following
but i want to use this as
how can i do this.
I've just looked quickly into the script and saw that it is not possible to resize remote files ;-).
So you either have to adapt the script to be able to resize remote files (which means that it has to be stored locally and then be resized) or you use another script ;-).

When using system to zip a directory lots of output is shown

I'm trying to write a script that allows an admin of a photo uploading system download all their photos at once.
Currently I am using
system('zip -r '.$_SERVER['DOCUMENT_ROOT'].'/zip.zip '.$_SERVER['DOCUMENT_ROOT'].'/images/photo-uploads';
to zip the files but this seems to echo names and locations all the files onto the page.
Is there anyway to get around this? If not what is the best way to zip files on server.
You might use exec('zip -r '.$_SERVER['DOCUMENT_ROOT'].'/zip.zip '.$_SERVER['DOCUMENT_ROOT'].'/images/photo-uploads'); instead.
You can use ZipArchive extension instead (if you are allowed to) of calling system zip like that, because it makes your code non-portable.
You can also use output buffering:
ob_start();
system('zip -r '.$_SERVER['DOCUMENT_ROOT'].'/zip.zip '.$_SERVER['DOCUMENT_ROOT'].'/images/photo-uploads';
ob_end_clean();
This will stop any output from being shown from the system command.

Categories