Skip to content

Commit

Permalink
:octocat:
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed Sep 19, 2023
1 parent 132520b commit cf7e82b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions examples/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
22 changes: 18 additions & 4 deletions examples/imagickConvertSVGtoPNG.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
* @copyright 2023 smiley
Expand All @@ -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){
Expand All @@ -52,8 +61,13 @@ public function dump(string $file = null):string{
$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){
Expand Down

0 comments on commit cf7e82b

Please sign in to comment.