chillerlan / php-qrcode

A PHP QR Code generator and reader with a user-friendly API.
https://smiley.codes/qrcode/
Apache License 2.0
2.01k stars 302 forks source link

imagickFormat SVG producess invalid SVG #167

Closed muuvmuuv closed 2 years ago

muuvmuuv commented 2 years ago

Describe the bug

I am trying to process the Imagick image after generation but with no possible option it seems right for me.

I have the imagick ext installed on PHP 8.1 and "libmagickcore-dev" (seems the one with SVG support).

This is the cachefile output:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
  "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg width="105" height="105">
stroke:#000000000000;fill:#000000000000;  <rect x="0" y="0" width="5" height="5"/>
stroke:#000000000000;fill:#000000000000;  <rect x="5" y="0" width="5" height="5"/>
stroke:#000000000000;fill:#000000000000;  <rect x="10" y="0" width="5" height="5"/>
stroke:#000000000000;fill:#000000000000;  <rect x="15" y="0" width="5" height="5"/>

This is the output SVG from Imagick:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
  "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg width="105" height="105">
<g style="fill:black;fill-opacity:1;stroke:none;stroke-width:1;stroke-opacity:1;fill-rule:nonzero;">
<g style="">
  <rect x="0" y="0" width="5" height="5"/>
</g>
<g style="">
  <rect x="5" y="0" width="5" height="5"/>
</g>

Code sample

<?php

use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;

$qr = new QRCode(new QROptions([
    'outputType' => QRCode::OUTPUT_IMAGICK,
    // 'eccLevel' => QRCode::ECC_M,
    'imagickFormat' => 'svg',
    'imagickBG' => '#fff',
    'imageTransparent' => true,
    'imageTransparencyBG' => [100, 100, 100],
    'addQuietzone' => false,
    'markupLight' => '#fff',
    'markupDark' => '#000',
]));

$blob = $qr->render($content);

$img = new \Imagick();
$img->readImageBlob($blob);

$img->writeImage($file->path . '.post.svg');

// $img->setOption('png:compression-level', 6);
$img->resizeImage($size, $size, \Imagick::FILTER_LANCZOS, 1);
// $img->setImageFormat('png');

$img->writeImage($file->path . '.png');

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

Environment (please complete the following information):

Additional context Add any other context about the problem here.

codemasher commented 2 years ago

Hmm why would you try to output SVG via ImageMagick when you could instead use the SVG output instead? Rendering/converting SVG via IM is generally not recommended - you could use the InkScape command line interface for proper conversion. Also, with your current settings you could just use the PNG output instead.