BrandStyles.php
3 years ago
NewsletterTemplatesRepository.php
2 months ago
TemplateImageLoader.php
2 years ago
ThumbnailSaver.php
2 months ago
index.php
3 years ago
ThumbnailSaver.php
147 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\NewsletterTemplates; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Config\Env; |
| 9 | use MailPoet\Entities\NewsletterTemplateEntity; |
| 10 | use MailPoet\Util\Security; |
| 11 | use MailPoet\WP\Functions as WPFunctions; |
| 12 | |
| 13 | class ThumbnailSaver { |
| 14 | const THUMBNAIL_DIRECTORY = 'newsletter_thumbnails'; |
| 15 | const IMAGE_QUALITY = 80; |
| 16 | |
| 17 | /** @var NewsletterTemplatesRepository */ |
| 18 | private $repository; |
| 19 | |
| 20 | /** @var WPFunctions */ |
| 21 | private $wp; |
| 22 | |
| 23 | /** @var string */ |
| 24 | private $baseDirectory; |
| 25 | |
| 26 | /** @var string */ |
| 27 | private $baseUrl; |
| 28 | |
| 29 | public function __construct( |
| 30 | NewsletterTemplatesRepository $repository, |
| 31 | WPFunctions $wp |
| 32 | ) { |
| 33 | $this->repository = $repository; |
| 34 | $this->wp = $wp; |
| 35 | $this->baseDirectory = Env::$tempPath; |
| 36 | $this->baseUrl = Env::$tempUrl; |
| 37 | } |
| 38 | |
| 39 | public function ensureTemplateThumbnailsForAll() { |
| 40 | $templateIds = $this->repository->getIdsOfEditableTemplates(); |
| 41 | foreach ($templateIds as $templateId) { |
| 42 | $template = $this->repository->findOneById((int)$templateId); |
| 43 | if (!$template) continue; |
| 44 | $this->ensureTemplateThumbnailFile($template); |
| 45 | // Remove template entity from memory after it was processed |
| 46 | $this->repository->detach($template); |
| 47 | unset($template); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | public function ensureTemplateThumbnailFile(NewsletterTemplateEntity $template): NewsletterTemplateEntity { |
| 52 | if ($template->getReadonly()) { |
| 53 | return $template; |
| 54 | } |
| 55 | $thumbnailUrl = $template->getThumbnail(); |
| 56 | $savedFilename = null; |
| 57 | $savedBaseUrl = null; |
| 58 | if ($thumbnailUrl && strpos($thumbnailUrl, self::THUMBNAIL_DIRECTORY) !== false) { |
| 59 | [$savedBaseUrl, $savedFilename] = explode('/' . self::THUMBNAIL_DIRECTORY . '/', $thumbnailUrl); |
| 60 | } |
| 61 | $file = $this->baseDirectory . '/' . self::THUMBNAIL_DIRECTORY . '/' . $savedFilename; |
| 62 | if (!$savedFilename || !file_exists($file)) { |
| 63 | $this->saveTemplateImage($template); |
| 64 | } |
| 65 | |
| 66 | // File might exist but domain was changed |
| 67 | $thumbnailUrl = $template->getThumbnail(); |
| 68 | if ($savedBaseUrl && $savedBaseUrl !== $this->baseUrl && $thumbnailUrl) { |
| 69 | $template->setThumbnail(str_replace($savedBaseUrl, $this->baseUrl, $thumbnailUrl)); |
| 70 | } |
| 71 | return $template; |
| 72 | } |
| 73 | |
| 74 | private function saveTemplateImage(NewsletterTemplateEntity $template): void { |
| 75 | $data = $template->getThumbnailData(); |
| 76 | if (!$data) { |
| 77 | return; |
| 78 | } |
| 79 | // Check that data contains Base 64 encoded jpeg |
| 80 | if (strpos($data, 'data:image/jpeg;base64') !== 0) { |
| 81 | return; |
| 82 | } |
| 83 | $thumbNailsDirectory = $this->baseDirectory . '/' . self::THUMBNAIL_DIRECTORY; |
| 84 | if (!file_exists($thumbNailsDirectory)) { |
| 85 | $this->wp->wpMkdirP($thumbNailsDirectory); |
| 86 | } |
| 87 | $file = $thumbNailsDirectory . '/' . Security::generateHash(16) . '_template_' . $template->getId() . '.jpg'; |
| 88 | |
| 89 | // Save the original quality image to a file and update DB record |
| 90 | if (!$this->saveBase64AsImageFile($file, $data)) { |
| 91 | return; |
| 92 | } |
| 93 | $url = str_replace($this->baseDirectory, $this->baseUrl, $file); |
| 94 | $template->setThumbnail($url); |
| 95 | $this->repository->flush(); |
| 96 | |
| 97 | // It is important that compression happens after the url was saved to DB. |
| 98 | // For some large files there is a risk that compression (if done using GD library) may fail due hitting memory limit. |
| 99 | // This way if the error occures the url is already saved and next time (e.g. next cron run) the image will be skipped |
| 100 | // and the previously saved original quality image used |
| 101 | $this->compressImage($file); |
| 102 | } |
| 103 | |
| 104 | private function compressImage(string $file): bool { |
| 105 | $editor = $this->wp->wpGetImageEditor($file); |
| 106 | if ($editor instanceof \WP_Error) { |
| 107 | return false; |
| 108 | } |
| 109 | $result = $editor->set_quality(self::IMAGE_QUALITY); |
| 110 | if ($result instanceof \WP_Error) { |
| 111 | return false; |
| 112 | } |
| 113 | $result = $editor->save($file); |
| 114 | if ($result instanceof \WP_Error) { |
| 115 | return false; |
| 116 | } |
| 117 | unset($editor); |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Saves base64 to a file without any compression. We decode the base64 data, because encoded data |
| 123 | * is considered URL, and file_put_contents() would fail when allow_url_fopen is disabled in PHP. |
| 124 | * |
| 125 | * @param string $file The path to the file to save the base64 data to. |
| 126 | * @param string $data The base64 data to save to the file. |
| 127 | * |
| 128 | * @return bool |
| 129 | */ |
| 130 | private function saveBase64AsImageFile(string $file, string $data): bool { |
| 131 | // Extract the base64 data from the data URI |
| 132 | if (preg_match('/^data:.*;base64,(.+)$/', $data, $matches)) { |
| 133 | $base64Data = $matches[1]; |
| 134 | } else { |
| 135 | // If it's not a proper data URI, assume it's already base64 encoded |
| 136 | $base64Data = $data; |
| 137 | } |
| 138 | |
| 139 | $decodedData = base64_decode($base64Data, true); |
| 140 | if ($decodedData === false) { |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | return file_put_contents($file, $decodedData) !== false; |
| 145 | } |
| 146 | } |
| 147 |