imagickImageAsBackground.php 2.6 KB

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