imagickImageAsBackground.php 2.6 KB

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