smiley %!s(int64=2) %!d(string=hai) anos
pai
achega
cf7e82ba7e
Modificáronse 2 ficheiros con 19 adicións e 4 borrados
  1. 1 0
      examples/Readme.md
  2. 18 4
      examples/imagickConvertSVGtoPNG.php

+ 1 - 0
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))

+ 18 - 4
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 <defs>), 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 <smiley@chillerlan.net>
  * @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(
-			'<svg xmlns="http://www.w3.org/2000/svg" class="qr-svg %1$s" viewBox="%2$s" preserveAspectRatio="%3$s" width="%4$s" height="%5$s">%6$s',
+			'<svg xmlns="http://www.w3.org/2000/svg" class="qr-svg %1$s" viewBox="%2$s" preserveAspectRatio="%3$s" width="%5$s" height="%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){