| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class QRImage |
| 5 |
* |
| 6 |
* @filesource QRImage.php |
| 7 |
* @created 05.12.2015 |
| 8 |
* @package chillerlan\QRCode\Output |
| 9 |
* @author Smiley <smiley@chillerlan.net> |
| 10 |
* @copyright 2015 Smiley |
| 11 |
* @license MIT |
| 12 |
*/ |
| 13 |
namespace YoastSEO_Vendor\chillerlan\QRCode\Output; |
| 14 |
|
| 15 |
use YoastSEO_Vendor\chillerlan\QRCode\QRCode; |
| 16 |
use YoastSEO_Vendor\chillerlan\QRCode\Data\QRMatrix; |
| 17 |
/** |
| 18 |
* Converts the matrix into images, raw or base64 output |
| 19 |
*/ |
| 20 |
class QRImage extends \YoastSEO_Vendor\chillerlan\QRCode\Output\QROutputAbstract |
| 21 |
{ |
| 22 |
const transparencyTypes = [\YoastSEO_Vendor\chillerlan\QRCode\QRCode::OUTPUT_IMAGE_PNG, \YoastSEO_Vendor\chillerlan\QRCode\QRCode::OUTPUT_IMAGE_GIF]; |
| 23 |
protected $moduleValues = [ |
| 24 |
// light |
| 25 |
\YoastSEO_Vendor\chillerlan\QRCode\Data\QRMatrix::M_DATA => [255, 255, 255], |
| 26 |
\YoastSEO_Vendor\chillerlan\QRCode\Data\QRMatrix::M_FINDER => [255, 255, 255], |
| 27 |
\YoastSEO_Vendor\chillerlan\QRCode\Data\QRMatrix::M_SEPARATOR => [255, 255, 255], |
| 28 |
\YoastSEO_Vendor\chillerlan\QRCode\Data\QRMatrix::M_ALIGNMENT => [255, 255, 255], |
| 29 |
\YoastSEO_Vendor\chillerlan\QRCode\Data\QRMatrix::M_TIMING => [255, 255, 255], |
| 30 |
\YoastSEO_Vendor\chillerlan\QRCode\Data\QRMatrix::M_FORMAT => [255, 255, 255], |
| 31 |
\YoastSEO_Vendor\chillerlan\QRCode\Data\QRMatrix::M_VERSION => [255, 255, 255], |
| 32 |
\YoastSEO_Vendor\chillerlan\QRCode\Data\QRMatrix::M_QUIETZONE => [255, 255, 255], |
| 33 |
\YoastSEO_Vendor\chillerlan\QRCode\Data\QRMatrix::M_TEST => [255, 255, 255], |
| 34 |
// dark |
| 35 |
\YoastSEO_Vendor\chillerlan\QRCode\Data\QRMatrix::M_DARKMODULE << 8 => [0, 0, 0], |
| 36 |
\YoastSEO_Vendor\chillerlan\QRCode\Data\QRMatrix::M_DATA << 8 => [0, 0, 0], |
| 37 |
\YoastSEO_Vendor\chillerlan\QRCode\Data\QRMatrix::M_FINDER << 8 => [0, 0, 0], |
| 38 |
\YoastSEO_Vendor\chillerlan\QRCode\Data\QRMatrix::M_ALIGNMENT << 8 => [0, 0, 0], |
| 39 |
\YoastSEO_Vendor\chillerlan\QRCode\Data\QRMatrix::M_TIMING << 8 => [0, 0, 0], |
| 40 |
\YoastSEO_Vendor\chillerlan\QRCode\Data\QRMatrix::M_FORMAT << 8 => [0, 0, 0], |
| 41 |
\YoastSEO_Vendor\chillerlan\QRCode\Data\QRMatrix::M_VERSION << 8 => [0, 0, 0], |
| 42 |
\YoastSEO_Vendor\chillerlan\QRCode\Data\QRMatrix::M_TEST << 8 => [0, 0, 0], |
| 43 |
]; |
| 44 |
/** |
| 45 |
* @see imagecreatetruecolor() |
| 46 |
* @var resource |
| 47 |
*/ |
| 48 |
protected $image; |
| 49 |
/** |
| 50 |
* @var int |
| 51 |
*/ |
| 52 |
protected $scale; |
| 53 |
/** |
| 54 |
* @var int |
| 55 |
*/ |
| 56 |
protected $length; |
| 57 |
/** |
| 58 |
* @see imagecolorallocate() |
| 59 |
* @var int |
| 60 |
*/ |
| 61 |
protected $background; |
| 62 |
/** |
| 63 |
* @return string |
| 64 |
* @throws \chillerlan\QRCode\Output\QRCodeOutputException |
| 65 |
*/ |
| 66 |
public function dump() |
| 67 |
{ |
| 68 |
if ($this->options->cachefile !== null && !\is_writable(\dirname($this->options->cachefile))) { |
| 69 |
throw new \YoastSEO_Vendor\chillerlan\QRCode\Output\QRCodeOutputException('Could not write data to cache file: ' . $this->options->cachefile); |
| 70 |
} |
| 71 |
$this->setImage(); |
| 72 |
$moduleValues = \is_array($this->options->moduleValues[\YoastSEO_Vendor\chillerlan\QRCode\Data\QRMatrix::M_DATA]) ? $this->options->moduleValues : $this->moduleValues; |
| 73 |
foreach ($this->matrix->matrix() as $y => $row) { |
| 74 |
foreach ($row as $x => $pixel) { |
| 75 |
$this->setPixel($x, $y, \imagecolorallocate($this->image, ...$moduleValues[$pixel])); |
| 76 |
} |
| 77 |
} |
| 78 |
$imageData = $this->dumpImage(); |
| 79 |
if ((bool) $this->options->imageBase64) { |
| 80 |
$imageData = 'data:image/' . $this->options->outputType . ';base64,' . \base64_encode($imageData); |
| 81 |
} |
| 82 |
return $imageData; |
| 83 |
} |
| 84 |
/** |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
protected function setImage() |
| 88 |
{ |
| 89 |
$this->scale = $this->options->scale; |
| 90 |
$this->length = $this->moduleCount * $this->scale; |
| 91 |
$this->image = \imagecreatetruecolor($this->length, $this->length); |
| 92 |
$this->background = \imagecolorallocate($this->image, ...$this->options->imageTransparencyBG); |
| 93 |
if ((bool) $this->options->imageTransparent && \in_array($this->options->outputType, $this::transparencyTypes, \true)) { |
| 94 |
\imagecolortransparent($this->image, $this->background); |
| 95 |
} |
| 96 |
\imagefilledrectangle($this->image, 0, 0, $this->length, $this->length, $this->background); |
| 97 |
} |
| 98 |
/** |
| 99 |
* @param $x |
| 100 |
* @param $y |
| 101 |
* @param $color |
| 102 |
* @return void |
| 103 |
*/ |
| 104 |
protected function setPixel($x, $y, $color) |
| 105 |
{ |
| 106 |
\imagefilledrectangle($this->image, $x * $this->scale, $y * $this->scale, ($x + 1) * $this->scale - 1, ($y + 1) * $this->scale - 1, $color); |
| 107 |
} |
| 108 |
/** |
| 109 |
* @return string |
| 110 |
* @throws \chillerlan\QRCode\Output\QRCodeOutputException |
| 111 |
*/ |
| 112 |
protected function dumpImage() |
| 113 |
{ |
| 114 |
\ob_start(); |
| 115 |
try { |
| 116 |
\call_user_func([$this, $this->options->outputType !== null ? $this->options->outputType : \YoastSEO_Vendor\chillerlan\QRCode\QRCode::OUTPUT_IMAGE_PNG]); |
| 117 |
} catch (\Exception $e) { |
| 118 |
throw new \YoastSEO_Vendor\chillerlan\QRCode\Output\QRCodeOutputException($e->getMessage()); |
| 119 |
} |
| 120 |
// @codeCoverageIgnoreEnd |
| 121 |
$imageData = \ob_get_contents(); |
| 122 |
\imagedestroy($this->image); |
| 123 |
\ob_end_clean(); |
| 124 |
return $imageData; |
| 125 |
} |
| 126 |
/** |
| 127 |
* @return void |
| 128 |
*/ |
| 129 |
protected function png() |
| 130 |
{ |
| 131 |
\imagepng($this->image, $this->options->cachefile, \in_array($this->options->pngCompression, \range(-1, 9), \true) ? $this->options->pngCompression : -1); |
| 132 |
} |
| 133 |
/** |
| 134 |
* Jiff - like... JitHub! |
| 135 |
* @return void |
| 136 |
*/ |
| 137 |
protected function gif() |
| 138 |
{ |
| 139 |
\imagegif($this->image, $this->options->cachefile); |
| 140 |
} |
| 141 |
/** |
| 142 |
* @return void |
| 143 |
*/ |
| 144 |
protected function jpg() |
| 145 |
{ |
| 146 |
\imagejpeg($this->image, $this->options->cachefile, \in_array($this->options->jpegQuality, \range(0, 100), \true) ? $this->options->jpegQuality : 85); |
| 147 |
} |
| 148 |
} |
| 149 |
|