imagemagick convert does not work through php - 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

Related

How to use tesseract ocr with php from any location

I have installed tesseract in C:\Program Files (x86)\Tesseract-OCR. In command prompt if I am executing with tesseract 123.png sample, then it works. But I can not use tesseract with php. I have tried many libraries and codes for the same.
Someone could help me on this please?
If you needs to use Tesseract in php code, the way to do this is using the http://php.net/manual/en/function.exec.php or http://php.net/manual/en/function.shell-exec.php functions, both allow you to execute bash codes like in "command prompt", but you needs to have more attention to use that, this open several security issues because the client input data sometimes is malicious. Hope it helps!
A working examples:
exec("tesseract C:/your/path/file.png C:/output/file");
Another thing, in part C:/output/file, you don't need to put .txt in output path, tesseract always do the output as .txt file extension.
If the Tesseract is not present on "Environment Variables" the solution is to pass the full executable file path:
shell_exec('"C:/Program Files (x86)/Tesseract-OCR/tesseract.exe" C:/path/to/image C:/output/path/')

FFMPEG not converting mp3 to ogg

I have a php upload script that converts MP3 files after they have been uploaded. I am keeping the mp3 file which is why I am doing it afterwards. A copy is created and then that is converted to ogg. The problem i am having is with FFMPEG as it is just not doing anything. I have never used FFMPEG or a tool like it before, nor have I used PHP's exec() function so I really have no idea how to use it but I have had a thorough look around for my answer before I came here.
exec("ffmpeg -i ".$target_path."".$hash_filename.".".$path_extension." ".$hash_filename.".ogg");
What the function could look like with data instead of variables:
ffmpeg -i ../uploads/ee78d5deb564901626067cc0008456ed.mp3 ee78d5deb564901626067cc0008456ed.ogg
I have checked with SSH and FFMPEG is located under the usual usr/bin/ffmpeg. I wondered if I should be targeting the file from where FFMPEG is or where the phpscript is but I have had no luck with either. Perhaps I am still targeting it from the wrong place?
I have checked and the codecs required for mp3 to ogg conversion are installed. I did also try adding those codecs into the command but the same thing happened. I understand FFMPEG automatically chooses which codecs to use so I ommited them.

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.

php shell_exec doesn't work with imagemagick commands

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.

pdftk unable to save on different folder

I'm using pdftk for flattening PDF files on server. For this purpose I use PHP.
This is my code in PHP. Btw, I'm using this on WAMP.
passthru("pdftk editablepdf/jason.pdf output flattenpdf/flattened.pdf flatten");
The pdftk.exe file and the accompanying .dll file both exist in the www directory.
editablepdf and flattenpdf are two separate directories in the www directory.
After executing the script and checking, i find the pdfs haven't been saved.
When i tried running pdftk via command prompt, it worked fine. But the same is not happening here. Is it something that's gotta do with passthru??
Thanks and Regards
Sameer
was able to solve after looking around.
Here's the working line -
passthru("pdftk editablepdf\\jason.pdf output flattenpdf\\flattened.pdf flatten");
Since \ acts as escape sequence character, i'm using "\ \" to denote the folder levels.

Categories