imagick
imagick copied to clipboard
Inkscape is not called when using imagick
Hi there. Linked images in svg files using xlink:href both http and local are not shown after svg->pdf conversion
testing environment: Ubuntu 20.04 Php 8.1.8 pecl/imagick 3.7.0
Version: ImageMagick 7.1.0-43 Q16-HDRI x86_64 8718d6292:20220709 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP(4.5)
Delegates (built-in): bzlib cairo djvu fontconfig freetype gslib gvc jbig jng jp2 jpeg lcms lqr lzma openexr pangocairo png ps raqm raw rsvg tiff webp x xml zip zlib
Compiler: gcc (9.4)
MSVG* rw+ ImageMagick's own SVG internal renderer
RSVG* rw+ Librsvg SVG renderer (RSVG 2.48.9)
SVG* rw+ Scalable Vector Graphics (RSVG 2.48.9)
SVGZ* rw+ Compressed Scalable Vector Graphics (RSVG 2.48.9)
while by calling command line magick test.svg test.pdf
works simple php script fails
just to mention http linked images worked fine in magick 6(policy is correct in 7 and still doesn't work) this is not imagick extension problem for sure.
$imagick = new Imagick()
$imagick->readImageBlob(file_get_contents('tmp/test.svg'));
$imagick->setImageFormat('pdf');
$saving_handle = fopen('tmp/test.pdf', 'a+');
$imagick->writeImageFile($saving_handle);
correct result by using
magick test.svg test.pdf
or inkscape "test.svg" --export-filename="test.pdf"
php imagick result
when i remove inkscape from the system cli command stops working too. meaning that inkscape is being used as delegate
i put xlink:href="test.jpg"
but i change it to full path on the machine as relative doesn't work even with cli
test.svg
test.jpg
p.s. i think it's related to issue #551 and i tried calling new Imagick('tmp/test.svg') but getting following error : ImagickException with message 'delegate failed `'potrace' --svg --output '%o' '%i'' @ error/delegate.c/InvokeDelegate/1911'
also opened discussion and issue on magick repo a while ago
https://github.com/ImageMagick/ImageMagick/issues/5003
https://github.com/ImageMagick/ImageMagick/discussions/5001
Any help to solve that or report again to magick with more details would be highly appreciated.
Short version, I don't know.
For the example svg file you gave, changing from an absolute path of xlink:href="/test.jpg"
to <image xlink:href="./test.jpg"></image>
makes invoking ImageMagick through Imagick produce the correct image.
I don't know what the SVG spec says about whether relative or absolute paths are means to be supported or not.
I suspect that the difference between the command line tools and invoking ImageMagick through Imagick might be related to the processes being spawned in different directories, and so being able to find the file, or not.
Linked images in svg files using xlink:href both http
I strongly, strongly strongly, strongly strongly strongly recommend not allowing ImageMagick to download files like this.
First, images will be broken at random and it is unlikely you will be able to notice or understand why.
Second, ImageMagick is...not so good with security.
Writing some code that:
- Analyzes SVG files for external images.
- Downloads those images as temporary files with names you give them.
- Checking that they are valid image files.
- Re-writing the image links inside the SVG to reference those local links.
Would make your system be able to be debugged when it breaks, and not quite so likely to be compromised.
To be honest, I'm not planning to investigate further the exact details of what is happening with relative paths in SVGs in imagick. I have to draw the line somewhere, on which bugs I'm going to investigate.
As well as recommending downloading the external images yourself, I'm recommend invoking inkscape directly (exampl usage here) as there are just too many moving parts when converting SVG images through ImageMagick, and inkscape is almost certainly the most reliable SVG converter.
ImagickException with message 'delegate failed `'potrace' --svg --output '%o' '%i'' @ error/delegate.c/InvokeDelegate/1911'
I have no idea why that is happening. The only mention I can see of potrace in my delegates.xml file is:
<delegate decode="pnm" encode="trace" command=""potrace" --svg --output "%o" "%i""/>
And I can't see why a 'pnm' file is being involved.
tl:dr, sorry can't make it work reliably for everyone.
I'll leave the issue open, just incase you have a specific question that I might be able to help with, but trying to figure out what is happening with 3 layers of library involved is just too much.
First of all. Thanks for replying.
For the example svg file you gave, changing from an absolute path of xlink:href="/test.jpg" to
makes invoking ImageMagick through Imagick produce the correct image.
as i said above this only works from cli with full server path /home/folder/folder/tmp/test.jpg
i put xlink:href="test.jpg" but i change it to full path on the machine as relative doesn't work even with cli
I suspect that the difference between the command line tools and invoking ImageMagick through Imagick might be related to the processes being spawned in different directories, and so being able to find the file, or not.
yeah. it's probably that. just strange that it worked in imagick 6 and stopped in newer version
I strongly, strongly strongly, strongly strongly strongly recommend not allowing ImageMagick to download files like this. First, images will be broken at random and it is unlikely you will be able to notice or understand why. Second, ImageMagick is...not so good with security.
yeah. i totally understand that. but all svg files that i'm processing are being generated by me so it can't reference malicious external files
Writing some code that: Analyzes SVG files for external images. Downloads those images as temporary files with names you give them. Checking that they are valid image files. Re-writing the image links inside the SVG to reference those local links. Would make your system be able to be debugged when it breaks, and not quite so likely to be compromised. As well as recommending downloading the external images yourself, I'm recommend invoking https://github.com/Imagick/imagick/issues/552 as there are just too many moving parts when converting SVG images through ImageMagick, and inkscape is almost certainly the most reliable SVG converter.
this is what i'm switching to right now as there are no other solutions with imagick 7. it's not ideal to add those workarounds to what was just working before in imagick 6. i would have to: 1.parse svg to extract external images 2. download all of those locally in temporary folder 3. change links to full server path 4. call inkscape from php as a separate process to make a conversion 5. feed converted svg->pdf to imagick to proceed with other transformations
tl:dr, sorry can't make it work reliably for everyone.
totally understand. thanks for checking this out anyway. it's probably magick library problem
Closing as I just don't have infinite amounts of time.