diff --git a/examples/Readme.md b/examples/Readme.md index 902a8c4bb..0ea565de7 100644 --- a/examples/Readme.md +++ b/examples/Readme.md @@ -22,6 +22,7 @@ - [GD Image with rounded modules](./imageWithRoundedShapes.php): similar to the SVG "melted" modules example ([#215](https://github.com/chillerlan/php-qrcode/pull/215)) - [ImageMagick with logo](./imagickWithLogo.php): a logo on top of the QR Code - [ImageMagick with image as background](./imagickImageAsBackground.php): an image as full size background of the QR Code +- [ImageMagick SVG to raster conversion](./imagickConvertSVGtoPNG.php): use ImageMagick to convert SVG output to a raster image, e.g. PNG - [SVG with logo](./svgWithLogo.php): an SVG QR Code with embedded logo (that is also SVG) - [SVG with "melted" modules](./svgMeltedModules.php): an effect where the matrix appears to be like melted wax ([#127](https://github.com/chillerlan/php-qrcode/issues/127)) - [SVG with randomly colored modules](./svgRandomColoredDots.php): a visual effect using multiple colors for the matrix modules ([#136](https://github.com/chillerlan/php-qrcode/discussions/136)) diff --git a/examples/imagickConvertSVGtoPNG.php b/examples/imagickConvertSVGtoPNG.php index 40942d4ad..be1e81863 100644 --- a/examples/imagickConvertSVGtoPNG.php +++ b/examples/imagickConvertSVGtoPNG.php @@ -2,6 +2,14 @@ /** * SVG to raster conversion example using ImageMagick * + * Please note that conversion via ImageMagick may not always produce ideal results, + * especially when using CSS styling (external or via ), also it depends on OS and Imagick version. + * + * Using the Inkscape command line may be the better option: + * + * @see https://wiki.inkscape.org/wiki/Using_the_Command_Line + * @see https://github.com/chillerlan/php-qrcode/discussions/216 + * * @created 19.09.2023 * @author smiley * @copyright 2023 smiley @@ -22,14 +30,15 @@ class SVGConvert extends QRMarkupSVG{ protected function header():string{ [$width, $height] = $this->getOutputDimensions(); + // we need to specify the "width" and "height" attributes so that Imagick knows the output size $header = sprintf( - '%6$s', + '%4$s', $this->options->cssClass, $this->getViewBox(), $this->options->svgPreserveAspectRatio, - ($width * $this->scale), - ($height * $this->scale), - $this->options->eol + $this->options->eol, + ($width * $this->scale), // use the scale option to modify the size + ($height * $this->scale) ); if($this->options->svgAddXmlHeader){ @@ -52,8 +61,13 @@ class SVGConvert extends QRMarkupSVG{ $im->readImageBlob($svg); $im->setImageFormat($this->options->imagickFormat); + if($this->options->quality > -1){ + $im->setImageCompressionQuality(max(0, min(100, $this->options->quality))); + } + $imageData = $im->getImageBlob(); + $im->destroy(); $this->saveToFile($imageData, $file); if($base64){