imagickImageAsBackground.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * ImageMagick with image as background example
  4. *
  5. * @created 08.09.2023
  6. * @author smiley <smiley@chillerlan.net>
  7. * @copyright 2023 smiley
  8. * @license MIT
  9. */
  10. use chillerlan\QRCode\{QRCode, QRCodeException, QROptions};
  11. use chillerlan\QRCode\Common\EccLevel;
  12. use chillerlan\QRCode\Output\QRImagick;
  13. require_once __DIR__.'/../vendor/autoload.php';
  14. class QRImagickImageAsBackground extends QRImagick{
  15. /**
  16. * @inheritDoc
  17. */
  18. protected function getDefaultModuleValue(bool $isDark):ImagickPixel{
  19. // RGBA, adjust opacity to increase contrast
  20. return $this->prepareModuleValue(($isDark) ? '#00000040' : '#ffffffa0');
  21. }
  22. /**
  23. * @inheritDoc
  24. */
  25. protected function createImage():Imagick{
  26. $imagick = new Imagick($this->options->background);
  27. $width = $imagick->getImageWidth();
  28. $height = $imagick->getImageHeight();
  29. // crop the background if necessary
  30. if(($width / $height) !== 1){
  31. $cropsize = ($width > $height) ? $height : $width;
  32. $imagick->cropImage($cropsize, $cropsize, 0, 0);
  33. }
  34. // scale the background to fit the size of the QR Code
  35. if($imagick->getImageWidth() !== $this->length){
  36. $imagick->scaleImage($this->length, $this->length, true);
  37. }
  38. if($this->options->quality > -1){
  39. $imagick->setImageCompressionQuality(max(0, min(100, $this->options->quality)));
  40. }
  41. return $imagick;
  42. }
  43. }
  44. /**
  45. * augment the QROptions class
  46. *
  47. * @property string $background
  48. */
  49. class ImageAsBackgroundOptions extends QROptions{
  50. protected string $background;
  51. /**
  52. * check background image
  53. *
  54. * @throws \chillerlan\QRCode\QRCodeException
  55. */
  56. protected function set_background(string $background):void{
  57. if(!file_exists($background) || !is_file($background) || !is_readable($background)){
  58. throw new QRCodeException('invalid background file');
  59. }
  60. // @todo: check/validate desired image format
  61. $this->background = $background;
  62. }
  63. }
  64. /*
  65. * Runtime
  66. */
  67. $options = new ImageAsBackgroundOptions;
  68. $options->background = __DIR__.'/background.jpg'; // setting from the augmented options
  69. $options->version = 5;
  70. $options->outputInterface = QRImagickImageAsBackground::class; // use the custom output class
  71. $options->eccLevel = EccLevel::H;
  72. $options->outputBase64 = false;
  73. $options->scale = 10;
  74. $options->drawLightModules = true;
  75. $options->svgUseFillAttributes = false;
  76. $options->invertMatrix = false;
  77. $options->quietzoneSize = 1;
  78. // dump the output, with an additional logo
  79. $out = (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
  80. if(PHP_SAPI !== 'cli'){
  81. header('Content-type: image/png');
  82. echo $out;
  83. }
  84. exit;