PHP Convert SVG To Jpg Missing Elements - php

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.

Related

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?

PHP Imagemagick PDF to GIF

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!

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.

How to extract alpha and other channels from a PNG file with PHP Gmagick?

I am trying to port 2 graphics magick commands to the Gmagick php library (mind: not Imagick).
The commands are:
gm convert -matte -flatten image.png output.png
gm convert -channel Matte -negate image.png output.png
Basically I am trying to extract the alpha channel from a transparent png into its own file and the image without alpha into another file from a php script.
I have the Imagick equivalents already but I am interested in Gmagick.
Any idea, anyone?
You can make use of Gmagick::separateimagechannel to get the channel (get the number of the matte channel). About removing the matte channel, I have no clue, but it should be possible.

Categories