imagickConvertSVGtoPNG.php 3.0 KB

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