PHP Imagemagick PDF to GIF - php

I have the following code which is supposed to convert the first page of PDF to thumbnail:
<?php
$strPDF = "http://www.domain.com/b.pdf";
exec("/usr/bin/convert \"{$strPDF}[0]\" -colorspace RGB -geometry 200 \"output.gif\"");
?>
My host server is Siteground, and apparently /usr/bin/convert is where ImageMagick convert function is. This is my first time using ImageMagick and I'm not sure if it is doing anything. Is my code correct? And if it is, I can't seem to find output.gif.

Give the output file a full path. Example : /home/marlboro/www/output.gif
Also, first download the PDF to your local machine (use curl/wget).
Before using exec from PHP you should be 100% sure the method works when called manually from shell!

Related

PHP Convert SVG To Jpg Missing Elements

I am using php imagemagic to convert svg to jpg and i use the below command for the conversion.
convert -density 250 source.svg target.jpg
I can able to convert svg to jpg successfully but some elements in svg are missing. Please check below example
Input SVG
Output JPG
Here you can clearly see that the light grey shades on the sides are replaced with white color. Can someone let me know how to fix this.
FYI here is the link to download the actual SVG
https://drive.google.com/file/d/1vC5yaXds7ogcsTWjaDzkXZZFyKzLCsgf/view?usp=sharing
Which SVG renderer are you using? It could be the internal Imagemagick MSVG/XML, the RSVG delegate or Inkscape (in order of increasing functionality). You can find out by adding -verbose to your command line.
convert -verbose -density 250 source.svg target.jpg
I used Imagemagick 6.9.10.11 Q16 Mac OSX with its internal MSVG/XML renderer, RSVG 2.42.2_2 and Inkscape 0.92.3_4. All three produced different results. The RSVG was the worst. The Inkscape was the best. Here are my results using your command.
convert -density 250 MSVG:source.svg target_msvg.jpg
convert -density 250 RSVG:source.svg target_rsvg.jpg
convert -density 250 source.svg target_inkscape.jpg
Usually RSVG does better than Imagemagick's MSVG. But here it seems to do worse. It could be due to the way I modified my delegates.xml file in order to be able to run RSVG while Inkscape was installed. Also the MSVG renderer has been improved over the last few releases. So an older version may not produce as good of a result.
Finally I found a workaround for this. I converted svg to canvas image (base64 format) via javascript and through php i have converted the base64 data to jpg image.

Converting SVG to another format fail due to delegate not being found

I have PHP code running on Debian stretch with ImageMagick. It tries to convert SVG to another format. Here is how it starts
$im = new Imagick();
$im->readImageBlob($svg);
The variable $svg contains valid SVG code in a string. If I copy this string to a text file with a .svg extension then it opens just fine. But readImageBlob throws an exception saying no delegate for this image format.
I have seen similar questions solved by installing more packages to the system. But I've already installed libxml2-dev, librsvg2-bin, libmagickcore-6.q16-3-extra and libfreetype6-dev.
I have no idea what else I am missing.
I had to prepend the $svg with <?xml version="1.0" ?> and it worked. It least it does read in the SVG and attempts to create png/jpeg. One piece of text gets misplaced during convertion though. So the task in general is still failed. But this is another issue. I think the question is answered.
What do you get from running the following command from PHP exec()
convert -list format
The line SVG should say RSVG or MSVG/XML. Does it show that? if you need RSVG, you will have to install that delegate and then reinstall Imagemagick so that Imagemagick can find it. Imagemagick is used by Imagick. They are not the same. The RSVG delegate can be found by a Google Search or from linuxfromscratch.org/blfs/view/svn/general/librsvg.html. Your svg file seems to render properly for me in Imagemagick using RSVG, but I am not sure what it should look like. It is just a graph set of horizontal lines.
I do not know much about using readImageBlob(). Just use readImage(), where you supply the path to a saved svg image file. That should work. Try that and see what you get.
Here is what I get using RSVG 2.42.2 in Imagemagick 6.9.10.3 Q16 Mac OSX were I have saved your text in a file called test.svg.
convert test.svg test.png
If I force the use of the Imagemagick MSVG/XML, it does not look as good.
convert MSVG:test.svg test2.png

imagemagick / ghostscript Error on conversion

We are using imagemagick to convert PDFs to JPEGs. Using the code below:
shell_exec('convert source.pdf output.jpg');
Im getting this error:
Segmentation fault
convert: Postscript delegate failed `/var/www/location/source.pdf': # error/pdf.c/ReadPDFImage/663.
convert: missing an image filename `/var/www/location/output.jpg' # error/convert.c/ConvertImageCommand/3011.
And works most of the time. There are some PDFs though that we can convert. Those are with alot of text and tables have some problem and do not convert at all.
I used GS script to convert and same thing. It does not convert.
shell_exec('gs -sDEVICE=jpeg -o output.jpg source.pdf');
Is there any way to convert those pdfs to jpg using imagemagick/gs?
What are things to avoid on pdf so that it will be converted to jpg using imagemagick/gs?

Resizing image with exec and convert in PHP

I'm trying to convert a png image in PHP the following way:
exec($cmd, $output, $return_code);
Where $cmd contains the following line of code:
/usr/bin/convert 'images/original/Id1741.png' -thumbnail x200 -quality '90' './cache/a3b84c5931d9619d12a9e244a310cb17_h200.png'
Calling this code on the command line works perfectly fine, but executing it on the webserver gives me the following error message:
Tried to execute : convert 'images/original/Id1741.png' -thumbnail x200 -quality '90' './cache/a3b84c5931d9619d12a9e244a310cb17_h200.png', return code: 1, output: Array()
If I remove the thumbnail option the command executes just fine on the webserver, but oviously it does not resize anything. So it's not a problem with permissions or the setup I guess.
PHP Version is 5.2.17.
ImageMagick Version is: 6.6.0-4 2012-04-26
Anyone ever had a similar issue and can help me with this?
Ok, I finally got it fixed. After redirecting stderr to a file I found the following error:
libgomp: Thread creation failed: Resource temporarily unavailable
Seems that my hoster 1&1 recently upgraded the ImageMagick version which apparently uses more memory than the old one (at least that's what the hoster says).
They recommend limiting the number of Threads created by ImageMagick:
putenv('MAGICK_THREAD_LIMIT=1');
I put this code into my init-script and now it works just fine!
You are converting to PNG, but you are setting -quality 90 (seemingly just analogue to the JPEG quality setting).
However, for PNG output, the -quality setting is very unlike JPEG's quality setting (which simply is an integer from 0 to 100).
For PNG it is composed by two single digits:
The first digit (tens) is (largely) the zlib compression level, and it may go from 0 to 9.
(However the setting of 0 has a special meaning: when you use it you'll get Huffman compression, not zlib compression level 0. This is often better... Weird but true.)
The second digit is the PNG data encoding filter type (before it is compressed):
0 is none,
1 is "sub",
2 is "up",
3 is "average",
4 is "Paeth", and
5 is "adaptive".
In practical terms that means:
For illustrations with solid sequences of color a "none" filter (-quality 00) is typically the most appropriate.
For photos of natural landscapes an "adaptive" filtering (-quality 05) is generally the best.
Maybe you want to revisit your -quality 90 setting in the light of this info.
Maybe you were aware of it already. In this case: my apologies for 'preaching to the choir'. :-)

OCR reading phone numbers with Tesseract

I am trying to complete a project that has to include some OCR. For the job I picked Tesseract OCR but the results are not optimal. I have tried to limit the character set to 1234567890- but the results are not good. Is there an optimal image size I can use or some way to train Tesseract to recognise this kind of string better?
The image is this:
And the result tesseract returns is 05175150152 which is not right, and it should be better since the image is not modified in any way. I use tesseract through PHP with exec with the following command:
"C:\Program Files\Tesseract-OCR\tesseract.exe" C:\wamp\www\a
dwords\phones\center_ctl09_ctl04.png sssd -l eng -psm 7 nobatch letters
Any ideas on what i am doing wrong?
The image resolution of 96 DPI is tough for any OCR engine. Try to rescale it to 300 DPI and you will have better results.
Additionally, JPEG is a lossy image format. Use a different one, like TIFF or PNG, if possible.

Categories