I'm trying to have my web app create thumbnails for uploaded PDFs with the following command:
exec( "/usr/local/bin/convert '/path/to/file.pdf'[0] -background white -alpha remove -thumbnail 600x '/path/to/file.pdf.png'", $output, $return );
When I run this command from the command line it works, both when I run it as root and when I run it as _www (the user of the web server) using:
sudo -u _www /usr/local/bin/convert '/path/to/file.pdf'[0] -background white -alpha remove -thumbnail 600x '/path/to/file.pdf.png'
But when the exec command runs, the thumbnail is not created. The $output array is empty, and the $return value is 1 (general error).
Furthermore, when I generate thumbnails for other kinds of files, I have no problems at all. For example for GIFs I use:
exec( "/usr/local/bin/convert '/path/to/file.gif'[0] -thumbnail 600x '/path/to/file.gif'", $output, $return );
I have no problems with JPGs, PNGs or BMPs either. This leads me to think that the problem may be related to GhostScript, as only PDFs require GhostScript to generate thumbnails. As a last comment, the thumbnail generation fails both in my localhost and in my server.
I'm out of ideas, anyone has one? Thanks!
In the end, I had two different problems, one in my localhost, another in my server.
In my localhost, GhostScript was not in the PATH of the exec environment. To solve this, I added the following line just before the command:
putenv( 'PATH=' . getenv( 'PATH' ) . ':' . dirname( '/usr/local/bin/gs' ) );
This fixed the problem in my localhost. But when I pushed the new code to the server, it still didn't work. So I checked the PATH in my server and GhostScript was already in there. I then figured out that my server was using an old version of GhostScript, with no "alpha" support. So I simplified the command to:
exec( "/usr/local/bin/convert '/path/to/file.pdf'[0] -thumbnail 600x '/path/to/file.pdf.png'", $output, $return );
And now both my localhost and server work, yay!
Related
echo exec("convert ddd.jpg ddd.png");
return me Invalid Parameter - ddd.png
it is working if I run it on command line, But for php give me this.
also I have checked phpinfo() there is no imageMagick on Enviroment PATH.
But I have added Enviroment PATH by automatical build in.
any ideas?
I feel the problem is PATH is not shown on phpinfo() Enviroment PATH
my server is windows server 2008, using IIS , php5.6
upodate if I run coonvert.exe only
exec('"c:\Program Files\ImageMagick-6.9.3-Q8\convert.exe"',$output,$return)
it will return me same info like in command line.But if I run convert only then give me this.
Array ( [0] => Must specify a file system ) 4
question solved.....
I restarted my server then look phpinfo()
the c:\Program Files\ImageMagick-6.9.3-Q8 is on the list now.....
sorry I am stupid...
Windows has its own convert command,
So you need to use full path to imageMagic's one.
Quick Help for skew images and using tesseract ocr:
shell_exec('"C:\Program Files\ImageMagick-7.0.8-Q16\convert.exe" C:\inetpub\wwwroot\custom\ocr\js\zzz.jpg -quality 100 -resize 1024x768 C:\inetpub\wwwroot\custom\ocr\js\mynew.png');
shell_exec('"C:\Program Files\ImageMagick-7.0.8-Q16\convert.exe" C:\inetpub\wwwroot\custom\ocr\js\mynew.png -gravity North -chop 0x550 C:\inetpub\wwwroot\custom\ocr\js\s3.png');
shell_exec('"C:\Program Files\ImageMagick-7.0.8-Q16\convert.exe" C:\inetpub\wwwroot\custom\ocr\js\s3.png -deskew 6% C:\inetpub\wwwroot\custom\ocr\js\s4.png');
shell_exec('"C:\Program Files\Tesseract-OCR\tesseract" "C:\inetpub\wwwroot\custom\ocr\js\s4.png" output_datas --psm 6');
My issue
I am using cairosvg on linux Ubuntu 14.04 LTS with PHP and apache. I am running it with the php function system(). This is the command I am running.
cairosvg -f pdf -o /var/www/dev/output.pdf /var/www/dev/input.svg
When I run this in terminal, I get the expected results, it generates the pdf file as expected. However when I run this in php using system(), it still generates the pdf file, but does not use the correct fonts.
What I've tried
The fonts are installed. The file permissions for both the folder and the fonts have been set to 777. Apache has been set as a co owner to the font folder and the font. File paths are absolute. echo on system() gives no errors. I have tried using equivalent alternatives to system() as well.
1st Major UPDATE: I've tried running this ImageMagick command to check the difference in available fonts. convert -list font On Terminal I see my installed fonts, but not on the system() call.
2nd UPDATE I ran printenv command in both terminal and system() and saw they had different values. I set HOME and USER environment variables using proc_open(), but I get the same results. Code is as follows.
$command = "cairosvg -f pdf -o /var/www/dev/output.pdf /var/www/dev/input.svg";
$descriptorspec = array();
$pipes = array();
$env = array(
'HOME' => '/home/ntwdev',
'USER' => 'ntwdev',
);
$resource = proc_open(
$command,
$descriptorspec,
$pipes,
$cwd = null,
$env
);
Why are fonts only coming through when I run the command in terminal and not in system()
You can try to execute some command to re build the font cache before the create PDF command?
I can think of:
$ xset fb rehash
From the man page:
The rehash argument resets the font path to its current value, causing the server to reread the
font databases in the current font path. This is generally only used when adding new fonts to a
font directory (after running mkfontdir to recreate the font database).
If you have fonts in special directories you could:
$ xset +fp /usr/share/fonts/misc
The issue was that cairosvg and/or the apache user of www-data could only access the fonts if it was in the /usr/share/fonts folder, regardless of file or user permissions.
I'm doing some ImageMagick stuff. Here is my command:
/usr/bin/convert /home/setsail/public_html/microsite/images
/tmp/fe0e3b88601d254befc115ca6a50365b.png -alpha set -channel alpha -background none
-vignette 0x3 -resize 66x89 /home/setsail/public_html/microsite/images/oval_thumb
/77bda03b6358b89efbe747ae414bd75f.png
This code feathers and resizes the image. It works fine on localhost (I'm using xampp on localhost) both in the shell and in php code, and works fine on my dedicated server in the shell.
But, it doesn't work at all on dedicated server with php code. Here is my code:
$cmd = "convert ".realpath($temp1)." -alpha set -channel alpha -background none -vignette 0x3 resize ".$width."x".$height." ".$dest_img;
exec($cmd);
ImageMagick is properly installed on server and active as well as I see it in phpinfo()
Any ideas why it's happening and what should i do?
The command convert may not be in the $PATH environment variable being used by the web server. Use the full path to convert:
// Substitute the full path for /usr/bin
$cmd = "/usr/bin/convert ".realpath($temp1)." -alpha set -channel alpha -background none -vignette 0x3 resize ".$width."x".$height." ".$dest_img;
$results = array();
$return_code = NULL;
exec($cmd, $results, $return_code);
// Check the output of the command for error:
var_dump($results);
var_dump($return_code);
To find convert's location, from a shell on your server do
$ which convert
I am trying to add the gotham feature found in Instagram via PHP and ImageMagick (http://net.tutsplus.com/tutorials/php/create-instagram-filters-with-php/) The script given doesn't work, but the commands do. When I generate the commands and copy and paste them to a command line, they work fine. Yet when I try to execute them with exec(), they do not work. I looked at the output given and it says the command is not found. I have safety mode off in php.ini and exec() is not a disabled function. I have tried adding ampersands and '2>&1' but to no avail. Why is it that exec() does not understand the command? Here is the script
function gotham($image) {
exec("convert " . $image . " -modulate 120,10,100 -fill '#222b6d' -colorize 20 -gamma 0.5 -contrast -contrast " . $image . " 2>&1", $output1);
exec("convert " . $image . " -bordercolor black -border 20x20 " . $image . " 2>&1", $output2);
}
Here is what comes from the var_dump of the $output variables:
array(1) { [0]=> string(30) "sh: convert: command not found" } array(1) { [0]=> string(30) "sh: convert: command not found" }
Seems you have path problem. Try these
exec("/path/to/Imagick/bin/convert -version", $output2);
Or
exec("usr/local/bin/convert -version", $output);
First determine where imagemagick installed on your system. You can do this from the terminal by running
whereis convert
To troubleshoot the permission issues, login as the user running the web server. This is usually apache/nobody/www-data depending on your distro and/or setup. In terminal run the following:
su - www-data
The above switches the current user to the one running the web server. In my case this www-data.
Now run the convert command with the full path (as determined by whereis convert from earlier and arguments in your php script. If there are no permission issues, this should execute fine. In most cases, you should see the same error as when running this from the php script in the web server.
Check permissions of the convert program by running
ls -l `whereis convert`
I'm trying to add SVG to .png conversion into my Yii app, using the Batik rasterizing library. Currently I'm working locally on a Mac running OSX 10.6.7, PHP 5.3.4 and Java 1.6.0_24.
When I run the PHP script that deals with the conversion, using...
$output = shell_exec(java -jar batik/batik-rasterizer.jar -m image/png -d pdf_temp/file.png -w 800 pdf_temp/file.svg)
Batik fails with the (unhelpful) error:
About to transcode 1 SVG file(s)
Converting file.svg to pdf_temp/file.png ... file_1310581599.png
Error while converting SVG
However, using...
java -jar batik/batik-rasterizer.jar -m image/png -d pdf_temp/file.png -w 800 pdf_temp/file.svg
...in Terminal works a treat and saves the .png file in the /pdf_temp/ directory along with its original .svg
So what's different about the PHP/apache environment that's causing Batik to fail when Terminal is fine?
EDIT:
After chatting with some colleagues we think it could be the fact that Java needs to run as root - which it does from Terminal but not within the web server.
Adding...
2>&1 1> /dev/null
To my Batik shell_exec() command reveals the following error:
Exception in thread "main" java.lang.InternalError: Can't connect to window server - not enough permissions.
Any ideas as to how I can run Java as root with the web server environment? Or allow it to perform the functions I require?
-Djava.awt.headless=true
http://www.emcken.dk/weblog/archives/25-svg-rasterizer-with-batik.html
I am currently working with Batik and Yii as well. Check the output and the source files' path. I provided the whole path and it is working now. It seems that Yii doesn't stand where your view is, instead Yii current location is your webroot. In my case I fixed like this:
define ('BATIK_PATH', '/var/www/wattquotes/protected/views/calculator/reports/batik/batik-rasterizer.jar');
$typeString = ' -m image/jpeg';
$outfile = ' /var/www/wattquotes/protected/views/calculator/reports/batik/temp/pic.jpg';
$width = ' -w 600';
$tempName = ' /var/www/wattquotes/protected/views/calculator/reports/batik/temp/proposal.svg';
$output = shell_exec("java -jar ". BATIK_PATH ." $typeString -d $outfile $width $tempName");
It works!