ImageMagick Conversion from svg to png/jpg results in distorted image - imagemagick

Somehow the Convert routine screws up my graphics... It used to work before. Does anyone have a clue what might have gone wrong (I installed the latest version of ImageMagick 7.0.5-10 and imagick 3.4.3, image converted through commandline)
The input SVG
http://defreule.info/scale.svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 35.55 35.72"><defs><style>.a{fill:#706f6f;}</style></defs><title>youtube</title><path class="a" d="M17.61,15.42a0.44,0.44,0,0,0,.31-0.11A0.39,0.39,0,0,0,18,15V12.43a0.31,0.31,0,0,0-.12-0.25,0.48,0.48,0,0,0-.31-0.1,0.43,0.43,0,0,0-.29.1,0.32,0.32,0,0,0-.11.25V15a0.41,0.41,0,0,0,.1.3A0.38,0.38,0,0,0,17.61,15.42Z"/><path class="a" d="M19.75,20.2a0.85,0.85,0,0,0-.4.1,1.38,1.38,0,0,0-.37.3V18.72H18.11v5.84H19V24.22a1.1,1.1,0,0,0,.37.29,1,1,0,0,0,.45.09,0.71,0.71,0,0,0,.58-0.24,1.07,1.07,0,0,0,.2-0.7V21.28a1.29,1.29,0,0,0-.21-0.81A0.74,0.74,0,0,0,19.75,20.2Zm0,3.35a0.47,0.47,0,0,1-.07.3,0.28,0.28,0,0,1-.23.09,0.48,0.48,0,0,1-.21,0,0.73,0.73,0,0,1-.2-0.15V21.05a0.64,0.64,0,0,1,.18-0.13,0.45,0.45,0,0,1,.18,0,0.33,0.33,0,0,1,.27.11,0.51,0.51,0,0,1,.09.33v2.22Z"/><polygon class="a" points="11.65 19.57 12.65 19.57 12.65 24.55 13.62 24.55 13.62 19.57 14.63 19.57 14.63 18.72 11.65 18.72 11.65 19.57"/><path class="a" d="M16.6,23.52a1.22,1.22,0,0,1-.27.23,0.49,0.49,0,0,1-.24.09,0.21,0.21,0,0,1-.18-0.07,0.37,0.37,0,0,1-.05-0.22v-3.3H15v3.6a0.92,0.92,0,0,0,.15.58,0.53,0.53,0,0,0,.45.19,1,1,0,0,0,.5-0.14,1.85,1.85,0,0,0,.5-0.4v0.47h0.86V20.25H16.6v3.27Z"/><path class="a" d="M17.76,1a17,17,0,1,0,17,17A17,17,0,0,0,17.76,1Zm2.05,10.38h1V15a0.39,0.39,0,0,0,.06.24,0.23,0.23,0,0,0,.2.08,0.56,0.56,0,0,0,.27-0.1A1.35,1.35,0,0,0,21.6,15v-3.6h1v4.74h-1V15.6A2,2,0,0,1,21,16a1.23,1.23,0,0,1-.56.15A0.61,0.61,0,0,1,20,16a1,1,0,0,1-.17-0.63v-4Zm-3.6,1.08a1.08,1.08,0,0,1,.39-0.88,1.59,1.59,0,0,1,1.05-.33,1.43,1.43,0,0,1,1,.34,1.14,1.14,0,0,1,.38.89v2.45a1.24,1.24,0,0,1-.37,1,1.48,1.48,0,0,1-1,.35,1.43,1.43,0,0,1-1-.36,1.26,1.26,0,0,1-.38-1V12.47ZM13.57,9.7l0.71,2.57h0.07L15,9.7h1.11l-1.27,3.76v2.67H13.77V13.58L12.47,9.7h1.1ZM26.26,23.23a3.11,3.11,0,0,1-3.11,3.11H12.36a3.11,3.11,0,0,1-3.11-3.11v-2.5a3.11,3.11,0,0,1,3.11-3.11h10.8a3.11,3.11,0,0,1,3.11,3.11v2.5Z"/><path class="a" d="M22.34,20.14a1.3,1.3,0,0,0-.94.35,1.21,1.21,0,0,0-.37.91v1.93a1.37,1.37,0,0,0,.33,1,1.18,1.18,0,0,0,.91.36,1.28,1.28,0,0,0,1-.33,1.38,1.38,0,0,0,.32-1V23.12H22.68v0.2a0.94,0.94,0,0,1-.09.49,0.36,0.36,0,0,1-.3.11A0.32,0.32,0,0,1,22,23.79a1,1,0,0,1-.08-0.47V22.5h1.65v-1.1a1.29,1.29,0,0,0-.32-0.94A1.21,1.21,0,0,0,22.34,20.14Zm0.34,1.69H21.92V21.4A0.63,0.63,0,0,1,22,21a0.34,0.34,0,0,1,.29-0.12,0.33,0.33,0,0,1,.29.12,0.64,0.64,0,0,1,.09.39v0.43Z"/></svg>
Resulting Image
convert scale.svg test.png
Update
It seems that when i use file > save as in Illustrator to save my .svg the converter does work fine. However when I use file>export the svg looks fine, but converting to png fails. I am still interested in why this is.

Related

Rails, Webpack & FontAwesome, how to stop conversion to SVG?

I've added FontAwesome pro with Yarn to my Rails 6 application and added it to application.js like this:
import "#fortawesome/fontawesome-pro/js/all";
import "#fortawesome/fontawesome-pro/css/all.css";
Before (when using FontAwesome through the asset pipeline) the generated HTML came out like this:
<i class="fal fa-heart-rate"></i>
Now it comes out like this:
<svg class="svg-inline--fa fa-heart-rate fa-w-20" aria-hidden="true" focusable="false" data-prefix="fal" data-icon="heart-rate" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512" data-fa-i2svg=""><path fill="currentColor" d="M480 224c-6.53 0-12.44 3.98-14.84 10.06l-45.62 114.05-84-335.98C333.72 4.86 327.69.17 319.62 0c-7.5.19-13.84 5.53-15.31 12.86l-82.06 410.23-46.72-186.97A16.005 16.005 0 0 0 160 224H8c-4.42 0-8 3.58-8 8v16c0 4.42 3.58 8 8 8h139.5l60.97 243.88c1.78 7.14 8.22 12.12 15.53 12.12h.38c7.5-.19 13.84-5.53 15.31-12.86l82.06-410.23 78.72 314.97c1.69 6.73 7.53 11.62 14.44 12.09 7.62.28 13.38-3.59 15.94-10.03l60-149.94H632c4.42 0 8-3.58 8-8v-16c0-4.42-3.58-8-8-8H480z"></path></svg>
Is there a way to prevent this? I'm asking because I do stacking of icons which worked perfectly before and now it doesn't come out correctly anymore.
Apparently the solution is pretty easy.
If you don't include the JS, this will not happen and everything will be rendered as before.
So in the application.js:
import "#fortawesome/fontawesome-pro/css/all.css";
I removed the JS and the font-icon appeared as li tag, like before, so you only need to remove the line:
import "#fortawesome/fontawesome-pro/js/all";

ImageMagick takes long time to convert SVG with text to JPEG

We are using ImageMagick (Version: ImageMagick 6.9.1-7 Q16 x86_64) and its PHP extension Imagick on our LAMP stack to convert SVGs to JPEGs and it is taking an abnormally long amount of time when the SVG contains any text (12-13 seconds / file).
When the same thing is run as a standalone PHP script from the command line (or directly using IM’s convert) it converts quickly in under 1 second regardless whether it has text or not.
Worth of mention also is that we do not have this problem with GraphicsMagick.
(But it has some SVG bugs that are unresolved and prevent us from using it.)
Would anyone have an idea as to why the fonts take so long to process on the LAMP stack, or how to identify the root cause of the slowdown?
Sample SVG:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="344" height="243" viewBox="0 0 737 521">
<g class="main">
<title>Main</title>
<image xmlns:xlink="http://www.w3.org/1999/xlink" id="svg_12" height="218.4417" width="291.2556" y="32.2537" x="-10.893" xlink:href="/tmp/767756670842438737_7032fbfb3c364e6da226254687eb1edb.jpg" style="pointer-events:inherit">29.75235 32.253654 209.964875 218.441697</image>
<g font-size="normal" font-family="Allerta" class="textarea" id="svg_13" style="pointer-events:inherit">
<rect opacity="0" fill-opacity="0" stroke-opacity="0" stroke-width="2" fill="#000" id="svg_10" height="32" width="150.4384" y="293.06824" x="550.14683" style="pointer-events:inherit"/>
<text text-anchor="start" xml:space="preserve" fill="#000" font-size="21" y="293" x="550" id="svg_68" style="pointer-events:inherit">
<tspan dy="14" x="550" xml:space="preserve" id="svg_69" style="pointer-events:inherit">
hello </tspan>
<tspan dy="21" x="550" xml:space="preserve" id="svg_70" style="pointer-events:inherit">gc </tspan>
</text>
</g>
</g>
</svg>
and the code to convert:
$im = new Imagick();
$svg = file_get_contents($svgFile);
$svg = str_replace(array("\n", "\r", "\t"), '', $svg);
$im->readImageBlob($svg);
$im->setImageFormat("jpeg");
$im->writeImage($jpgFile);
$im->clear();
That SVG converts just fine for me.
The way to investigate this is to run the PHP script through strace with a command like:
strace -f -F /usr/local/bin/php testScript.php
But with the appropriate path for PHP on your system.
There is a high chance there is some error condition that is making 'something' take far longer than it should do. The strace command above will allow you to see all of the system calls that are being made, and how long each of them take.
I suspect what you will see some system call take almost all of the total time for processing, before returning an error code. If you can't figure out what it's output means, please attach the output to your question, or pastebin, and I will attempt to interpret the runes.
Warning - strace is capable of capturing information that you might not want revealed (e.g. secret keys) so care should be made before posting its output anywhere.

Grails svg image href referring the png is not showing

This is difficult to explain. I needed some fills in Coreldraw that I liked and saved as SVG. When Coreldraw saved my svg, I got one folder called soil_Images with soil_ImgID1.png inside and soil.svg. And I put inside of web-app/images project. For some reason, the image png is not shown.
I have my svg like in my gsp:
<image x="177" y="76" width="36" height="23" preserveAspectRatio="none" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/projectName/images/soil.svg"></image>
And my soil.svg has another image tag that shows true image that I want:
<image x="44" y="29" width="8156" height="11707" xlink:href="soil_Images/soil_ImgID1.png"/>
I tried to add a contextpath like: ${application.contextPath}/images/soil_Images/soil_ImgID1.png.
That didn't work. What am I missing?
In chrome resources, the image hasn't been downloaded.
Building off of Matt Busche's blog post on using the asset pipeline with SVG and PNG fallback images, try pointing to the asset pipeline location instead of hardcoding. Note the assetPath with src (the relative path you are using if not the default asset pipeline image location) and the name of the image.
<image xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="${assetPath(src: 'imgs/' + logoSVG)}" src="${assetPath(src: 'imgs/' + logoPNG)}"></image>

Render SVG Tag in PDF

I want to render a signature in a pdf.
my html code:
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<path stroke="navy" stroke-width="2" fill="none" d="<%= #image %>"/>
</svg>
#image = "M96,8 L96,9 L96,11 L96,14 L98,18 L101,22 L115,32 L126,43
L142,50 L165,56 L189,56 L207,56 L219,50 L230,46 L233,43 L235,41
L236,40 L238,39 L239,39"
how can I render this in pdf?
im using rails 3 + prawn.
OR how can i create with #image an png?
any advice?
I am not familiar with ruby on rails or prawn but if you look at the Graphics section of the PDF spec, specifically in the path section it shows how the commands are laid out.
You could potentially use this info to map from the SVG draw commands to the postscript/pdf draw commands.
A question that might help: How to insert a SVG file in a PDF document?
You can use the wicked_pdf gem which renders HTML and JavaScript into a PDF file. It uses the wkhtmltopdf lib.
To convert to PNG I would recommend the IMGKit gem, which uses wkhtmltoimage lib.
You can use https://github.com/mogest/prawn-svg
Can't confirm if it works, but as I needed to do the same as you.
I've found that after a simple google research I found this plugin.
I will test it as soon as I can.
Kind regards

Google PageSpeed & ImageMagick JPG compression

Given a user uploaded image, I need to create various thumbnails of it for display on a website. I'm using ImageMagick and trying to make Google PageSpeed happy. Unfortunately, no matter what quality value I specify in the convert command, PageSpeed is still able to suggest compressing the image even further.
Note that http://www.imagemagick.org/script/command-line-options.php?ImageMagick=2khj9jcl1gd12mmiu4lbo9p365#quality mentions:
For the JPEG ... image formats,
quality is 1 [provides the] lowest
image quality and highest compression
....
I actually even tested compressing the image using 1 (it produced an unusable image, though) and PageSpeed still suggests that I can still optimize such image by "losslessly compressing" the image. I don't know how to compress an image any more using ImageMagick. Any suggestions?
Here's a quick way to test what I am talking about:
assert_options(ASSERT_BAIL, TRUE);
// TODO: specify valid image here
$input_filename = 'Dock.jpg';
assert(file_exists($input_filename));
$qualities = array('100', '75', '50', '25', '1');
$geometries = array('100x100', '250x250', '400x400');
foreach($qualities as $quality)
{
echo("<h1>$quality</h1>");
foreach ($geometries as $geometry)
{
$output_filename = "$geometry-$quality.jpg";
$command = "convert -units PixelsPerInch -density 72x72 -quality $quality -resize $geometry $input_filename $output_filename";
$output = array();
$return = 0;
exec($command, $output, $return);
echo('<img src="' . $output_filename . '" />');
assert(file_exists($output_filename));
assert($output === array());
assert($return === 0);
}
echo ('<br/>');
}
The JPEG may contain comments, thumbnails or metadata, which can be removed.
Sometimes it is possible to compress JPEG files more, while keeping the same quality. This is possible if the program which generated the image did not use the optimal algorithm or parameters to compress the image. By recompressing the same data, an optimizer may reduce the image size. This works by using specific Huffman tables for compression.
You may run jpegtran or jpegoptim on your created file, to reduce it further in size.
To minimize the image sizes even more, you should remove all meta data. ImageMagick can do this by adding a -strip to the commandline.
Have you also considered to put your thumbnail images as inline-d base64 encoded data into your HTML?
This can make your web page load much faster (even though the size gets a bit larger), because it saves the browser from running multiple requests for all the image files (the images) which are referenced in the HTML code.
Your HTML code for such an image would look like this:
<IMG SRC="data:image/png;base64,
iVBORw0KGgoAAAANSUhEUgAAAM4AAABJAQMAAABPZIvnAAAABGdBTUEAALGPC/xh
BQAAAAFzUkdCAK7OHOkAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAA
OpgAABdwnLpRPAAAAAZQTFRFAAAA/wAAG/+NIgAAAAF0Uk5TAEDm2GYAAAABYktH
RACIBR1IAAAACXBIWXMAAABIAAAASABGyWs+AAAB6ElEQVQ4y+3UQY7bIBQG4IeQ
yqYaLhANV+iyi9FwpS69iGyiLuZYpepF6A1YskC8/uCA7SgZtVI3lcoiivkIxu/9
MdH/8U+N6el2pk0oFyibWyr1Q3+PlO2NqJV+/BnRPMjcJ9zrfJ/U+zQ9oAvo+QGF
d+npPqFQn++TXElkrEpEJhAtlTBR6dNHUuzIMhFnEhxAmJDkKxlmt7ATXDDJYcaE
r4Txqtkl42VYSH+t9KrD9b5nxZeog/LWGVHprGInGWVQUTvjDWXca5KdsowqyGSc
DrZRlGlQUl4kQwpUjiSS9gI9VdECZhHFQ2I+UE2CHJQfkNxTNKCl0RkURqlLowJK
1h1p3sjc0CJD39D4BIqD7JvvpH/GAxl2/YSq9mtHSHknga7OKNOHKyEdaFC2Dh1w
9VSJemBeGuHgMuh24EynK03YM1Lr83OjUle38aVSfTblT424rl4LhdglsUag5RB5
uBJSJBIiELSzaAeIN0pUlEeZEMeClC4cBuH6mxOlgPjC3uLproUCWfy58WPN/MZR
86ghc888yNdD0Tj8eAucasl2I5LqX19I7EmEjaYjSb9R/G1SYfQA7ZBuT5H6WwDt
UAfK1BOJmh/eZnKLeKvZ/vA8qonCpj1h6djfbqvW620Tva36++MXUkNDlFREMVkA
AAAldEVYdGRhdGU6Y3JlYXRlADIwMTItMDgtMjJUMDg6Mzc6NDUrMDI6MDBTUnmt
AAAAJXRFWHRkYXRlOm1vZGlmeQAyMDEyLTA4LTIyVDA4OjM3OjQ1KzAyOjAwIg/B
EQAAAA50RVh0bGFiZWwAImdvb2dsZSJdcbX4AAAAAElFTkSuQmCC"
ALT="google" WIDTH=214 HEIGHT=57 VSPACE=5 HSPACE=5 BORDER=0 />
And you would create the base64 encoded image data like this:
base64 -i image.jpg -o image.b64
Google performs those calculations based on it's WebP image format (https://developers.google.com/speed/webp/).
Despite giving performance gains though, it is currently supported only by chrome and opera (http://caniuse.com/webp)

Resources