imagickConvertSVGtoPNG.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\{QRCode, QROptions};
  19. use chillerlan\QRCode\Data\QRMatrix;
  20. use chillerlan\QRCode\Output\QRMarkupSVG;
  21. require_once __DIR__.'/../vendor/autoload.php';
  22. class SVGConvert extends QRMarkupSVG{
  23. /** @inheritDoc */
  24. protected function header():string{
  25. [$width, $height] = $this->getOutputDimensions();
  26. // we need to specify the "width" and "height" attributes so that Imagick knows the output size
  27. $header = sprintf(
  28. '<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',
  29. $this->options->cssClass,
  30. $this->getViewBox(),
  31. $this->options->svgPreserveAspectRatio,
  32. $this->options->eol,
  33. ($width * $this->scale), // use the scale option to modify the size
  34. ($height * $this->scale)
  35. );
  36. if($this->options->svgAddXmlHeader){
  37. $header = sprintf('<?xml version="1.0" encoding="UTF-8"?>%s%s', $this->options->eol, $header);
  38. }
  39. return $header;
  40. }
  41. /** @inheritDoc */
  42. public function dump(string|null $file = null):string{
  43. $base64 = $this->options->outputBase64;
  44. // we don't want the SVG in base64
  45. $this->options->outputBase64 = false;
  46. $svg = $this->createMarkup($file !== null);
  47. // now convert the output
  48. $im = new Imagick;
  49. $im->readImageBlob($svg);
  50. $im->setImageFormat($this->options->imagickFormat);
  51. if($this->options->quality > -1){
  52. $im->setImageCompressionQuality(max(0, min(100, $this->options->quality)));
  53. }
  54. $imageData = $im->getImageBlob();
  55. $im->destroy();
  56. $this->saveToFile($imageData, $file);
  57. if($base64){
  58. // use finfo to guess the mime type
  59. $imageData = $this->toBase64DataURI($imageData, (new finfo(FILEINFO_MIME_TYPE))->buffer($imageData));
  60. }
  61. return $imageData;
  62. }
  63. }
  64. // SVG from the basic example
  65. $options = new QROptions;
  66. $options->version = 7;
  67. $options->outputInterface = SVGConvert::class;
  68. $options->imagickFormat = 'png32';
  69. $options->scale = 20;
  70. $options->outputBase64 = false;
  71. $options->drawLightModules = true;
  72. $options->svgUseFillAttributes = false;
  73. $options->drawCircularModules = true;
  74. $options->circleRadius = 0.4;
  75. $options->connectPaths = true;
  76. $options->keepAsSquare = [
  77. QRMatrix::M_FINDER_DARK,
  78. QRMatrix::M_FINDER_DOT,
  79. QRMatrix::M_ALIGNMENT_DARK,
  80. ];
  81. $options->svgDefs = '
  82. <linearGradient id="rainbow" x1="1" y2="1">
  83. <stop stop-color="#e2453c" offset="0"/>
  84. <stop stop-color="#e07e39" offset="0.2"/>
  85. <stop stop-color="#e5d667" offset="0.4"/>
  86. <stop stop-color="#51b95b" offset="0.6"/>
  87. <stop stop-color="#1e72b7" offset="0.8"/>
  88. <stop stop-color="#6f5ba7" offset="1"/>
  89. </linearGradient>
  90. <style><![CDATA[
  91. .dark{fill: url(#rainbow);}
  92. .light{fill: #eee;}
  93. svg{ width: 530px; height: 530px; }
  94. ]]></style>';
  95. // render the SVG and convert to the desired ImageMagick format
  96. $image = (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
  97. header('Content-type: image/png');
  98. echo $image;