imagick icon indicating copy to clipboard operation
imagick copied to clipboard

Convert SVG to JPG/PNG

Open wojoj93 opened this issue 1 year ago • 3 comments

hi, I have a problem with converting svg file to jpg/png via imagick: here is code:

$image = new \Imagick(); $image->readImage('old.svg'); $image->setImageFormat('png'); $image->setImageCompressionQuality(100); $image->writeImage('new.png'); $image->destroy();

result: SVG: Zaznaczenie_1210 PNG/JPG: Zaznaczenie_1211

Could someone tell me what I do wrong?

wojoj93 avatar May 30 '23 09:05 wojoj93

Can you provide the original SVG as an svg, rather than the PNG that is appears to be on github?

Also, can you say how you installed Imagick please?

Danack avatar May 30 '23 10:05 Danack

Thank you for your reply. Here is an original svg: qr

Installation process:

Imagick

RUN apt-get install -y libmagickwand-dev
&& pecl install imagick
&& docker-php-ext-enable imagick COPY imagick/policy.xml /etc/ImageMagick-6/policy.xml

wojoj93 avatar May 30 '23 10:05 wojoj93

Okay, I get the same error using Imagick/ImageMagick.

I'd suggest opening an issue at their repo here: https://github.com/ImageMagick/ImageMagick6

However....you probably just want to get your stuff working.

There have been so many SVG problems with ImageMagick, I pretty strongly recommend using Inkscape to convert files instead of Imagick/ImageMagick.

inkscape can be install through apt and then to convert a file, it needs to be invoked with something like:

inkscape "old.svg" --export-filename="./output_inkscape_directly.png" \
  --export-dpi="96" \
  --export-background="white" \
  --export-background-opacity="255" > inkscape_output.txt 2>&1

I should probably write this down in the docs.

Inkscape is a dedicated SVG converter, it's always going to be more reliable than ImageMagick, where the maintainers have many other things to worry about.

Danack avatar Jun 01 '23 12:06 Danack