|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Framework\File\Pdf\ImageResource; |
| 10 | + |
| 11 | +use Exception; |
| 12 | +use finfo; |
| 13 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 14 | +use Magento\Framework\Filesystem; |
| 15 | +use Magento\Framework\Filesystem\Directory\ReadInterface; |
| 16 | +use Zend_Pdf_Exception; |
| 17 | + |
| 18 | +class ImageFactory |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var \Magento\Framework\Filesystem |
| 22 | + */ |
| 23 | + private Filesystem $filesystem; |
| 24 | + |
| 25 | + /** |
| 26 | + * @param \Magento\Framework\Filesystem $filesystem |
| 27 | + */ |
| 28 | + public function __construct(Filesystem $filesystem) |
| 29 | + { |
| 30 | + $this->filesystem = $filesystem; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * New zend image factory instance |
| 35 | + * |
| 36 | + * @param string $filename |
| 37 | + * @return \Zend_Pdf_Resource_Image_Jpeg|\Zend_Pdf_Resource_Image_Png|\Zend_Pdf_Resource_Image_Tiff|object |
| 38 | + * @throws \Magento\Framework\Exception\FileSystemException |
| 39 | + * @throws \Zend_Pdf_Exception |
| 40 | + * @SuppressWarnings(PHPMD.LongVariable) |
| 41 | + */ |
| 42 | + public function factory(string $filename) |
| 43 | + { |
| 44 | + $mediaReader = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA); |
| 45 | + if (!$mediaReader->isFile($filename)) { |
| 46 | + #require_once 'Zend/Pdf/Exception.php'; |
| 47 | + throw new Zend_Pdf_Exception("Cannot create image resource. File not found."); |
| 48 | + } |
| 49 | + $tempFilenameFromBucketOrDisk = $this->createTemporaryFileAndPutContent($mediaReader, $filename); |
| 50 | + $tempResourceFilePath = $this->getFilePathOfTemporaryFile($tempFilenameFromBucketOrDisk); |
| 51 | + $typeOfImage = $this->getTypeOfImage($tempResourceFilePath, $filename); |
| 52 | + $zendPdfImage = $this->getZendPdfImage($typeOfImage, $tempResourceFilePath); |
| 53 | + $this->removeTemoraryFile($tempFilenameFromBucketOrDisk); |
| 54 | + return $zendPdfImage; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Create a temporary file and put content of the original file into it |
| 59 | + * |
| 60 | + * @param ReadInterface $mediaReader |
| 61 | + * @param string $filename |
| 62 | + * @return resource |
| 63 | + * @throws \Magento\Framework\Exception\FileSystemException |
| 64 | + * @throws \Zend_Pdf_Exception |
| 65 | + * @SuppressWarnings(PHPMD.LongVariable) |
| 66 | + */ |
| 67 | + protected function createTemporaryFileAndPutContent(ReadInterface $mediaReader, string $filename) |
| 68 | + { |
| 69 | + $tempFilenameFromBucketOrDisk = tmpfile(); |
| 70 | + if ($tempFilenameFromBucketOrDisk === false) { |
| 71 | + #require_once 'Zend/Pdf/Exception.php'; |
| 72 | + throw new Zend_Pdf_Exception('Cannot create temporary file'); |
| 73 | + } |
| 74 | + fwrite($tempFilenameFromBucketOrDisk, $mediaReader->readFile($filename)); |
| 75 | + return $tempFilenameFromBucketOrDisk; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Returns the path of the temporary file or nothing |
| 80 | + * |
| 81 | + * @param resource $tempFilenameFromBucketOrDisk |
| 82 | + * @return string |
| 83 | + * @SuppressWarnings(PHPMD.LongVariable) |
| 84 | + */ |
| 85 | + protected function getFilePathOfTemporaryFile($tempFilenameFromBucketOrDisk): string |
| 86 | + { |
| 87 | + try { |
| 88 | + return stream_get_meta_data($tempFilenameFromBucketOrDisk)['uri']; |
| 89 | + } catch (Exception $e) { |
| 90 | + return ''; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Get mime-type in safe way except internal errors |
| 96 | + * |
| 97 | + * @param string $filepath |
| 98 | + * @param string $baseFileName |
| 99 | + * @return mixed|string |
| 100 | + * @throws \Zend_Pdf_Exception |
| 101 | + */ |
| 102 | + protected function getTypeOfImage(string $filepath, string $baseFileName) |
| 103 | + { |
| 104 | + if (class_exists('finfo', false) && !empty($filepath)) { |
| 105 | + $finfo = new finfo(FILEINFO_MIME_TYPE); |
| 106 | + $classicMimeType = $finfo->file($filepath); |
| 107 | + } elseif (function_exists('mime_content_type') && !empty($filepath)) { |
| 108 | + $classicMimeType = mime_content_type($filepath); |
| 109 | + } else { |
| 110 | + $classicMimeType = $this->fetchFallbackMimeType($baseFileName); |
| 111 | + } |
| 112 | + if (!empty($classicMimeType)) { |
| 113 | + return explode("/", $classicMimeType)[1] ?? ''; |
| 114 | + } else { |
| 115 | + return ''; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Fall back fetching of mimetype by original base file name |
| 121 | + * |
| 122 | + * @param string $baseFileName |
| 123 | + * @return string |
| 124 | + * @throws \Zend_Pdf_Exception |
| 125 | + */ |
| 126 | + protected function fetchFallbackMimeType(string $baseFileName): string |
| 127 | + { |
| 128 | + $extension = pathinfo($baseFileName, PATHINFO_EXTENSION); |
| 129 | + switch (strtolower($extension)) { |
| 130 | + case 'jpg': |
| 131 | + //Fall through to next case; |
| 132 | + case 'jpe': |
| 133 | + //Fall through to next case; |
| 134 | + case 'jpeg': |
| 135 | + $classicMimeType = 'image/jpeg'; |
| 136 | + break; |
| 137 | + case 'png': |
| 138 | + $classicMimeType = 'image/png'; |
| 139 | + break; |
| 140 | + case 'tif': |
| 141 | + //Fall through to next case; |
| 142 | + case 'tiff': |
| 143 | + $classicMimeType = 'image/tiff'; |
| 144 | + break; |
| 145 | + default: |
| 146 | + #require_once 'Zend/Pdf/Exception.php'; |
| 147 | + throw new Zend_Pdf_Exception( |
| 148 | + "Cannot create image resource. File extension not known or unsupported type." |
| 149 | + ); |
| 150 | + } |
| 151 | + return $classicMimeType; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Creates instance of Zend_Pdf_Resource_Image |
| 156 | + * |
| 157 | + * @param string $typeOfImage |
| 158 | + * @param string $tempResourceFilePath |
| 159 | + * @return \Zend_Pdf_Resource_Image_Jpeg|\Zend_Pdf_Resource_Image_Png|\Zend_Pdf_Resource_Image_Tiff|object |
| 160 | + */ |
| 161 | + protected function getZendPdfImage(string $typeOfImage, string $tempResourceFilePath) |
| 162 | + { |
| 163 | + $classToUseAsPdfImage = sprintf('Zend_Pdf_Resource_Image_%s', ucfirst($typeOfImage)); |
| 164 | + return new $classToUseAsPdfImage($tempResourceFilePath); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Removes the temporary file from disk |
| 169 | + * |
| 170 | + * @param resource $tempFilenameFromBucketOrDisk |
| 171 | + * @return void |
| 172 | + * @SuppressWarnings(PHPMD.LongVariable) |
| 173 | + */ |
| 174 | + protected function removeTemoraryFile($tempFilenameFromBucketOrDisk): void |
| 175 | + { |
| 176 | + fclose($tempFilenameFromBucketOrDisk); |
| 177 | + } |
| 178 | +} |
0 commit comments