| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Endroid\QrCode\ImageData; |
| 6 |
|
| 7 |
use Endroid\QrCode\Logo\LogoInterface; |
| 8 |
|
| 9 |
final class LogoImageData |
| 10 |
{ |
| 11 |
private function __construct( |
| 12 |
private readonly string $data, |
| 13 |
private \GdImage|null $image, |
| 14 |
private readonly string $mimeType, |
| 15 |
private readonly int $width, |
| 16 |
private readonly int $height, |
| 17 |
private readonly bool $punchoutBackground |
| 18 |
) { |
| 19 |
} |
| 20 |
|
| 21 |
public static function createForLogo(LogoInterface $logo): self |
| 22 |
{ |
| 23 |
error_clear_last(); |
| 24 |
$data = @file_get_contents($logo->getPath()); |
| 25 |
|
| 26 |
if (!is_string($data)) { |
| 27 |
$errorDetails = error_get_last()['message'] ?? 'invalid data'; |
| 28 |
throw new \Exception(sprintf('Could not read logo image data from path "%s": %s', $logo->getPath(), $errorDetails)); |
| 29 |
} |
| 30 |
|
| 31 |
if (false !== filter_var($logo->getPath(), FILTER_VALIDATE_URL)) { |
| 32 |
$mimeType = self::detectMimeTypeFromUrl($logo->getPath()); |
| 33 |
} else { |
| 34 |
$mimeType = self::detectMimeTypeFromPath($logo->getPath()); |
| 35 |
} |
| 36 |
|
| 37 |
$width = $logo->getResizeToWidth(); |
| 38 |
$height = $logo->getResizeToHeight(); |
| 39 |
|
| 40 |
if ('image/svg+xml' === $mimeType) { |
| 41 |
if (null === $width || null === $height) { |
| 42 |
throw new \Exception('SVG Logos require an explicitly set resize width and height'); |
| 43 |
} |
| 44 |
|
| 45 |
return new self($data, null, $mimeType, $width, $height, $logo->getPunchoutBackground()); |
| 46 |
} |
| 47 |
|
| 48 |
error_clear_last(); |
| 49 |
$image = @imagecreatefromstring($data); |
| 50 |
|
| 51 |
if (!$image) { |
| 52 |
$errorDetails = error_get_last()['message'] ?? 'invalid data'; |
| 53 |
throw new \Exception(sprintf('Unable to parse image data at path "%s": %s', $logo->getPath(), $errorDetails)); |
| 54 |
} |
| 55 |
|
| 56 |
// No target width and height specified: use from original image |
| 57 |
if (null !== $width && null !== $height) { |
| 58 |
return new self($data, $image, $mimeType, $width, $height, $logo->getPunchoutBackground()); |
| 59 |
} |
| 60 |
|
| 61 |
// Only target width specified: calculate height |
| 62 |
if (null !== $width && null === $height) { |
| 63 |
return new self($data, $image, $mimeType, $width, intval(imagesy($image) * $width / imagesx($image)), $logo->getPunchoutBackground()); |
| 64 |
} |
| 65 |
|
| 66 |
// Only target height specified: calculate width |
| 67 |
if (null === $width && null !== $height) { |
| 68 |
return new self($data, $image, $mimeType, intval(imagesx($image) * $height / imagesy($image)), $height, $logo->getPunchoutBackground()); |
| 69 |
} |
| 70 |
|
| 71 |
return new self($data, $image, $mimeType, imagesx($image), imagesy($image), $logo->getPunchoutBackground()); |
| 72 |
} |
| 73 |
|
| 74 |
public function getData(): string |
| 75 |
{ |
| 76 |
return $this->data; |
| 77 |
} |
| 78 |
|
| 79 |
public function getImage(): \GdImage |
| 80 |
{ |
| 81 |
if (!$this->image instanceof \GdImage) { |
| 82 |
throw new \Exception('SVG Images have no image resource'); |
| 83 |
} |
| 84 |
|
| 85 |
return $this->image; |
| 86 |
} |
| 87 |
|
| 88 |
public function getMimeType(): string |
| 89 |
{ |
| 90 |
return $this->mimeType; |
| 91 |
} |
| 92 |
|
| 93 |
public function getWidth(): int |
| 94 |
{ |
| 95 |
return $this->width; |
| 96 |
} |
| 97 |
|
| 98 |
public function getHeight(): int |
| 99 |
{ |
| 100 |
return $this->height; |
| 101 |
} |
| 102 |
|
| 103 |
public function getPunchoutBackground(): bool |
| 104 |
{ |
| 105 |
return $this->punchoutBackground; |
| 106 |
} |
| 107 |
|
| 108 |
public function createDataUri(): string |
| 109 |
{ |
| 110 |
return 'data:'.$this->mimeType.';base64,'.base64_encode($this->data); |
| 111 |
} |
| 112 |
|
| 113 |
private static function detectMimeTypeFromUrl(string $url): string |
| 114 |
{ |
| 115 |
$headers = get_headers($url, true); |
| 116 |
|
| 117 |
if (!is_array($headers)) { |
| 118 |
throw new \Exception(sprintf('Could not retrieve headers to determine content type for logo URL "%s"', $url)); |
| 119 |
} |
| 120 |
|
| 121 |
$headers = array_combine(array_map('strtolower', array_keys($headers)), $headers); |
| 122 |
|
| 123 |
if (!isset($headers['content-type'])) { |
| 124 |
throw new \Exception(sprintf('Content type could not be determined for logo URL "%s"', $url)); |
| 125 |
} |
| 126 |
|
| 127 |
return is_array($headers['content-type']) ? $headers['content-type'][1] : $headers['content-type']; |
| 128 |
} |
| 129 |
|
| 130 |
private static function detectMimeTypeFromPath(string $path): string |
| 131 |
{ |
| 132 |
if (!function_exists('mime_content_type')) { |
| 133 |
throw new \Exception('You need the ext-fileinfo extension to determine logo mime type'); |
| 134 |
} |
| 135 |
|
| 136 |
error_clear_last(); |
| 137 |
$mimeType = @mime_content_type($path); |
| 138 |
|
| 139 |
if (!is_string($mimeType)) { |
| 140 |
$errorDetails = error_get_last()['message'] ?? 'invalid data'; |
| 141 |
throw new \Exception(sprintf('Could not determine mime type: %s', $errorDetails)); |
| 142 |
} |
| 143 |
|
| 144 |
if (!preg_match('#^image/#', $mimeType)) { |
| 145 |
throw new \Exception('Logo path is not an image'); |
| 146 |
} |
| 147 |
|
| 148 |
// Passing mime type image/svg results in invisible images |
| 149 |
if ('image/svg' === $mimeType) { |
| 150 |
return 'image/svg+xml'; |
| 151 |
} |
| 152 |
|
| 153 |
return $mimeType; |
| 154 |
} |
| 155 |
} |