| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Endroid\QrCode\ImageData; |
| 6 |
|
| 7 |
use Endroid\QrCode\Label\LabelInterface; |
| 8 |
|
| 9 |
final class LabelImageData |
| 10 |
{ |
| 11 |
private function __construct( |
| 12 |
private readonly int $width, |
| 13 |
private readonly int $height |
| 14 |
) { |
| 15 |
} |
| 16 |
|
| 17 |
public static function createForLabel(LabelInterface $label): self |
| 18 |
{ |
| 19 |
if (str_contains($label->getText(), "\n")) { |
| 20 |
throw new \Exception('Label does not support line breaks'); |
| 21 |
} |
| 22 |
|
| 23 |
if (!function_exists('imagettfbbox')) { |
| 24 |
throw new \Exception('Function "imagettfbbox" does not exist: check your FreeType installation'); |
| 25 |
} |
| 26 |
|
| 27 |
$labelBox = imagettfbbox($label->getFont()->getSize(), 0, $label->getFont()->getPath(), $label->getText()); |
| 28 |
|
| 29 |
if (!is_array($labelBox)) { |
| 30 |
throw new \Exception('Unable to generate label image box: check your FreeType installation'); |
| 31 |
} |
| 32 |
|
| 33 |
return new self( |
| 34 |
intval($labelBox[2] - $labelBox[0]), |
| 35 |
intval($labelBox[0] - $labelBox[7]) |
| 36 |
); |
| 37 |
} |
| 38 |
|
| 39 |
public function getWidth(): int |
| 40 |
{ |
| 41 |
return $this->width; |
| 42 |
} |
| 43 |
|
| 44 |
public function getHeight(): int |
| 45 |
{ |
| 46 |
return $this->height; |
| 47 |
} |
| 48 |
} |