imagickConvertSVGtoPNG.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * SVG to raster conversion example using ImageMagick
  4. *
  5. * Please note that conversion via ImageMagick may not always produce ideal results,
  6. * especially when using CSS styling (external or via <defs>), also it depends on OS and Imagick version.
  7. *
  8. * Using the Inkscape command line may be the better option:
  9. *
  10. * @see https://wiki.inkscape.org/wiki/Using_the_Command_Line
  11. * @see https://github.com/chillerlan/php-qrcode/discussions/216
  12. *
  13. * @created 19.09.2023
  14. * @author smiley <smiley@chillerlan.net>
  15. * @copyright 2023 smiley
  16. * @license MIT
  17. */
  18. use chillerlan\QRCode\Data\QRMatrix;
  19. use chillerlan\QRCode\Output\QRMarkupSVG;
  20. use chillerlan\QRCode\Output\QROutputInterface;
  21. use chillerlan\QRCode\QRCode;
  22. use chillerlan\QRCode\QROptions;
  23. require_once __DIR__.'/../vendor/autoload.php';
  24. class SVGConvert extends QRMarkupSVG{
  25. /** @inheritDoc */
  26. protected function header():string{
  27. [$width, $height] = $this->getOutputDimensions();
  28. // we need to specify the "width" and "height" attributes so that Imagick knows the output size
  29. $header = sprintf(
  30. '<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',
  31. $this->options->cssClass,
  32. $this->getViewBox(),
  33. $this->options->svgPreserveAspectRatio,
  34. $this->options->eol,
  35. ($width * $this->scale), // use the scale option to modify the size
  36. ($height * $this->scale)
  37. );
  38. if($this->options->svgAddXmlHeader){
  39. $header = sprintf('<?xml version="1.0" encoding="UTF-8"?>%s%s', $this->options->eol, $header);
  40. }
  41. return $header;
  42. }
  43. /** @inheritDoc */
  44. public function dump(string $file = null):string{
  45. $base64 = $this->options->outputBase64;
  46. // we don't want the SVG in base64
  47. $this->options->outputBase64 = false;
  48. $svg = $this->createMarkup($file !== null);
  49. // now convert the output
  50. $im = new Imagick;
  51. $im->readImageBlob($svg);
  52. $im->setImageFormat($this->options->imagickFormat);
  53. if($this->options->quality > -1){
  54. $im->setImageCompressionQuality(max(0, min(100, $this->options->quality)));
  55. }
  56. $imageData = $im->getImageBlob();
  57. $im->destroy();
  58. $this->saveToFile($imageData, $file);
  59. if($base64){
  60. // use finfo to guess the mime type
  61. $imageData = $this->toBase64DataURI($imageData, (new finfo(FILEINFO_MIME_TYPE))->buffer($imageData));
  62. }
  63. return $imageData;
  64. }
  65. }
  66. // SVG from the basic example
  67. $options = new QROptions;
  68. $options->version = 7;
  69. $options->outputType = QROutputInterface::CUSTOM;
  70. $options->outputInterface = SVGConvert::class;
  71. $options->imagickFormat = 'png32';
  72. $options->scale = 20;
  73. $options->outputBase64 = false;
  74. $options->drawLightModules = true;
  75. $options->markupDark = '';
  76. $options->markupLight = '';
  77. $options->drawCircularModules = true;
  78. $options->circleRadius = 0.4;
  79. $options->connectPaths = true;
  80. $options->keepAsSquare = [
  81. QRMatrix::M_FINDER_DARK,
  82. QRMatrix::M_FINDER_DOT,
  83. QRMatrix::M_ALIGNMENT_DARK,
  84. ];
  85. $options->svgDefs = '
  86. <linearGradient id="rainbow" x1="1" y2="1">
  87. <stop stop-color="#e2453c" offset="0"/>
  88. <stop stop-color="#e07e39" offset="0.2"/>
  89. <stop stop-color="#e5d667" offset="0.4"/>
  90. <stop stop-color="#51b95b" offset="0.6"/>
  91. <stop stop-color="#1e72b7" offset="0.8"/>
  92. <stop stop-color="#6f5ba7" offset="1"/>
  93. </linearGradient>
  94. <style><![CDATA[
  95. .dark{fill: url(#rainbow);}
  96. .light{fill: #eee;}
  97. svg{ width: 530px; height: 530px; }
  98. ]]></style>';
  99. // render the SVG and convert to the desired ImageMagick format
  100. $image = (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
  101. header('Content-type: image/png');
  102. echo $image;