LabelImageData.php 1.2 KB

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