imagickConvertSVGtoPNG.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. * @noinspection PhpComposerExtensionStubsInspection
  19. */
  20. declare(strict_types=1);
  21. use chillerlan\QRCode\{QRCode, QROptions};
  22. use chillerlan\QRCode\Data\QRMatrix;
  23. use chillerlan\QRCode\Output\{QRCodeOutputException, QRMarkupSVG};
  24. require_once __DIR__.'/../vendor/autoload.php';
  25. class SVGConvert extends QRMarkupSVG{
  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. public function dump(string|null $file = null):string{
  44. $base64 = $this->options->outputBase64;
  45. // we don't want the SVG in base64
  46. $this->options->outputBase64 = false;
  47. $svg = $this->createMarkup($file !== null);
  48. // now convert the output
  49. $im = new Imagick;
  50. $im->readImageBlob($svg);
  51. $im->setImageFormat($this->options->imagickFormat);
  52. if($this->options->quality > -1){
  53. $im->setImageCompressionQuality(max(0, min(100, $this->options->quality)));
  54. }
  55. $imageData = $im->getImageBlob();
  56. $im->destroy();
  57. $this->saveToFile($imageData, $file);
  58. if($base64){
  59. // use finfo to guess the mime type
  60. $mime = (new finfo(FILEINFO_MIME_TYPE))->buffer($imageData);
  61. if($mime === false){
  62. throw new QRCodeOutputException('unable to detect mime type');
  63. }
  64. $imageData = $this->toBase64DataURI($imageData);
  65. }
  66. return $imageData;
  67. }
  68. }
  69. // SVG from the basic example
  70. $options = new QROptions;
  71. $options->version = 7;
  72. $options->outputInterface = SVGConvert::class;
  73. $options->imagickFormat = 'png32';
  74. $options->scale = 20;
  75. $options->outputBase64 = false;
  76. $options->drawLightModules = true;
  77. $options->svgUseFillAttributes = false;
  78. $options->drawCircularModules = true;
  79. $options->circleRadius = 0.4;
  80. $options->connectPaths = true;
  81. $options->keepAsSquare = [
  82. QRMatrix::M_FINDER_DARK,
  83. QRMatrix::M_FINDER_DOT,
  84. QRMatrix::M_ALIGNMENT_DARK,
  85. ];
  86. $options->svgDefs = '
  87. <linearGradient id="rainbow" x1="1" y2="1">
  88. <stop stop-color="#e2453c" offset="0"/>
  89. <stop stop-color="#e07e39" offset="0.2"/>
  90. <stop stop-color="#e5d667" offset="0.4"/>
  91. <stop stop-color="#51b95b" offset="0.6"/>
  92. <stop stop-color="#1e72b7" offset="0.8"/>
  93. <stop stop-color="#6f5ba7" offset="1"/>
  94. </linearGradient>
  95. <style><![CDATA[
  96. .dark{fill: url(#rainbow);}
  97. .light{fill: #eee;}
  98. svg{ width: 530px; height: 530px; }
  99. ]]></style>';
  100. // render the SVG and convert to the desired ImageMagick format
  101. $image = (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
  102. header('Content-type: image/png');
  103. echo $image;