AbstractWriter.php
3 years ago
BinaryWriter.php
3 years ago
DebugWriter.php
3 years ago
EpsWriter.php
3 years ago
FpdfWriter.php
3 years ago
PngWriter.php
3 years ago
SvgWriter.php
3 years ago
WriterInterface.php
3 years ago
AbstractWriter.php
73 lines
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | /* |
| 5 | * (c) Jeroen van den Enden <info@endroid.nl> |
| 6 | * |
| 7 | * This source file is subject to the MIT license that is bundled |
| 8 | * with this source code in the file LICENSE. |
| 9 | */ |
| 10 | namespace WP2FA_Vendor\Endroid\QrCode\Writer; |
| 11 | |
| 12 | use WP2FA_Vendor\Endroid\QrCode\Exception\GenerateImageException; |
| 13 | use WP2FA_Vendor\Endroid\QrCode\Exception\InvalidLogoException; |
| 14 | use WP2FA_Vendor\Endroid\QrCode\Exception\MissingExtensionException; |
| 15 | use WP2FA_Vendor\Endroid\QrCode\QrCodeInterface; |
| 16 | abstract class AbstractWriter implements WriterInterface |
| 17 | { |
| 18 | protected function getMimeType(string $path) : string |
| 19 | { |
| 20 | if (\false !== \filter_var($path, \FILTER_VALIDATE_URL)) { |
| 21 | return $this->getMimeTypeFromUrl($path); |
| 22 | } |
| 23 | return $this->getMimeTypeFromPath($path); |
| 24 | } |
| 25 | private function getMimeTypeFromUrl(string $url) : string |
| 26 | { |
| 27 | /** @var mixed $format */ |
| 28 | $format = \PHP_VERSION > 80000 ? \true : 1; |
| 29 | $headers = \get_headers($url, $format); |
| 30 | if (!\is_array($headers) || !isset($headers['Content-Type'])) { |
| 31 | throw new InvalidLogoException(\sprintf('Content type could not be determined for logo URL "%s"', $url)); |
| 32 | } |
| 33 | return $headers['Content-Type']; |
| 34 | } |
| 35 | private function getMimeTypeFromPath(string $path) : string |
| 36 | { |
| 37 | if (!\function_exists('mime_content_type')) { |
| 38 | throw new MissingExtensionException('You need the ext-fileinfo extension to determine logo mime type'); |
| 39 | } |
| 40 | $mimeType = \mime_content_type($path); |
| 41 | if (!\is_string($mimeType)) { |
| 42 | throw new InvalidLogoException('Could not determine mime type'); |
| 43 | } |
| 44 | if (!\preg_match('#^image/#', $mimeType)) { |
| 45 | throw new GenerateImageException('Logo path is not an image'); |
| 46 | } |
| 47 | // Passing mime type image/svg results in invisible images |
| 48 | if ('image/svg' === $mimeType) { |
| 49 | return 'image/svg+xml'; |
| 50 | } |
| 51 | return $mimeType; |
| 52 | } |
| 53 | public function writeDataUri(QrCodeInterface $qrCode) : string |
| 54 | { |
| 55 | $dataUri = 'data:' . $this->getContentType() . ';base64,' . \base64_encode($this->writeString($qrCode)); |
| 56 | return $dataUri; |
| 57 | } |
| 58 | public function writeFile(QrCodeInterface $qrCode, string $path) : void |
| 59 | { |
| 60 | $string = $this->writeString($qrCode); |
| 61 | \file_put_contents($path, $string); |
| 62 | } |
| 63 | public static function supportsExtension(string $extension) : bool |
| 64 | { |
| 65 | return \in_array($extension, static::getSupportedExtensions()); |
| 66 | } |
| 67 | public static function getSupportedExtensions() : array |
| 68 | { |
| 69 | return []; |
| 70 | } |
| 71 | public abstract function getName() : string; |
| 72 | } |
| 73 |