From 0d083c8f46713c0d34b6050a5cb48cf5fa1b8c6c Mon Sep 17 00:00:00 2001 From: Jason Mayo Date: Mon, 14 Nov 2022 17:22:42 +0000 Subject: [PATCH] Added: Amazon S3 and Servd compatability --- CHANGELOG.md | 6 ++ README.md | 21 +++--- composer.json | 2 +- src/services/PdfTransformService.php | 93 ++++++++++++-------------- src/variables/PdfTransformVariable.php | 15 ++++- 5 files changed, 74 insertions(+), 63 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19e1506..242d166 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # PDF Transform Changelog +## 1.0.8 - 2022-11-14 +### Added +- Amazon S3 Compatability +- Servd Compatability (Thanks @mattgrayisok / @servd) +- New `.render()` method that outputs the transformed image as a Craft asset (No longer just the URL 🎉) + ## 1.0.7 - 2022-06-17 ### Changed - Icon to work with darkmode themes diff --git a/README.md b/README.md index 2bdcbbf..7a9e00a 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ A use case for this is to show the preview of a PDF before a user downloads that - Transform PDF's to images via Twig (The file needs to be an existing Asset element) - PDF's are transformed to an image when PDF's are uploaded via Assets or Asset fields. - Transformed PDF images are indexed and available in Assets like all other asset elements. +- Works with local asset volumes, Amazon S3 and Servd. ## Install @@ -63,13 +64,15 @@ You can also install the plugin via the Plugin Store in the Craft Admin CP by se ## Templating -To transform a PDF to an image, and then output the URL use the following Twig tag: +To transform a PDF to an image use the following Twig tag: ``` -{% set asset = entry.pdfAsset.one() %} -{{ craft.pdfTransform.url(asset) }} +{% set pdfToTransform = entry.pdfAsset.one() %} +{{ craft.pdfTransform.render(pdfToTransform) }} ``` +The transformed PDF (Now an image stored in Assets) can then be output using `{{ pdfToTransform.one().url }}`. Or get any Asset property e.g. Title, ID, Filename etc. + If the transformed image doesn't exist then the PDF will be transformed via the template. This may cause the template/page to become slow whilst the PDF is transformed. Be aware that this also may output a large image, so we'd recommend running this through an image transform. See Dimensions. @@ -89,20 +92,14 @@ PDF Transform does the basic job of converting your PDF to a single image. It wi I'd recommend running the PDF image through one of the following options/plugins and setting the dimensions that way (Some of these also handle caching the image as well) - Image Transforms by Craft -- Imager by aelvan -- Image Optimize by nystudio107 - -### Local - -Currently the plugin has only been tested with local assets, not assets through Amazon S3 etc. It may, or may not work with remote assets. +- Imager X by aelvan +- Image Optimize by nystudio107 ## Support -If you have any issues (Surely not!) then I'll aim to reply to these as soon as possible. If it's a site-breaking-oh-no-what-has-happened moment, then hit me up on the Craft CMS Slack - @bymayo +If you have any issues (Surely not!) then I'll aim to reply to these as soon as possible. If it's a site-breaking-oh-no-what-has-happened moment, then hit me up on the Craft CMS Discord - @bymayo ## Roadmap -- Output asset element, not just the URL so that all Asset methods are available. - Optional variables (E.g. page, resolution etc) -- Test and support remote assets. - When PDF assets are updated, ensure old transformed image is removed. diff --git a/composer.json b/composer.json index 49940ae..a573930 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "bymayo/pdf-transform", "description": "Transform a PDF page to an image (JPEG, PNG)", "type": "craft-plugin", - "version": "1.0.7", + "version": "1.0.8", "keywords": [ "craft", "cms", diff --git a/src/services/PdfTransformService.php b/src/services/PdfTransformService.php index 9c962ef..f9512b0 100644 --- a/src/services/PdfTransformService.php +++ b/src/services/PdfTransformService.php @@ -10,12 +10,14 @@ use bymayo\pdftransform\PdfTransform; +use Imagick; use Craft; use craft\base\Component; use Yii; use craft\services\Volumes; use Spatie\PdfToImage\Pdf; use craft\elements\Asset; +use craft\helpers\Path; /** * @author ByMayo @@ -60,83 +62,76 @@ public function getImageVolume() return $volume; } - public function getImagePath($asset, $aliasType) - { - - return Yii::getAlias($this->getImageVolume()->$aliasType) . '/' . $this->getFileName($asset); - - } - public function getFileName($asset) { // e.g. filename-12345.jpg return $asset->filename . '-' . $asset->id . '.' . $this->settings->imageFormat; } - public function url($asset) + public function render($asset) { - // @TODO Check to see if asset exists using actual Asset volume path from volume settings - if (!file_exists($this->getImagePath($asset, 'path'))) - { + $volume = $this->getImageVolume(); + $fileName = $this->getFileName($asset); + + if ($volume->fileExists($fileName)) { + + $transformedAsset = Asset::find() + ->volumeId($volume->id) + ->filename($fileName) + ->one(); - $this->pdfToImage( - $asset - ); + return $transformedAsset; } - // Get Asset Path - // @TODO Check to see if asset exists using actual Asset volume path from volume settings - return $this->getImagePath($asset, 'url'); + return $this->pdfToImage( + $asset + ); } - public function getPdfAssetPath($asset) + public function pdfToImage($asset) { - $volumePath = Yii::getAlias($asset->getVolume()->path); - $folderPath = $asset->getPath(); - - $assetPath = $volumePath . '/' . $folderPath; + $filename = $this->getFileName($asset); + $volume = $this->getImageVolume(); - return $assetPath; + $pathService = Craft::$app->getPath(); + $tempPath = $pathService->getTempPath(true) . '/' . mt_rand(0, 9999999) . '.png'; + file_put_contents($tempPath, file_get_contents($asset->url)); - } + $tempPathTransform = $pathService->getTempPath(true) . '/' . $filename; - public function pdfToImage($asset) - { + $folder = Craft::$app->getAssets()->getRootFolderByVolumeId($volume->id); - // @TODO Check to see if file exists - $pdf = new Pdf($this->getPdfAssetPath($asset)); + $pdf = new Pdf($tempPath); $pdf ->setPage($this->settings->page) ->setResolution($this->settings->imageResolution) ->setCompressionQuality($this->settings->imageQuality) - ->saveImage($this->getImagePath($asset, 'path')); - - $this->indexAsset($asset); + ->saveImage($tempPathTransform); + + $assetTransformed = new Asset(); + $assetTransformed->tempFilePath = $tempPathTransform; + $assetTransformed->filename = $filename; + $assetTransformed->folderId = $folder->id; + $assetTransformed->newFolderId = $folder->id; + $assetTransformed->kind = 'Image'; + $assetTransformed->title = $asset->title; + $assetTransformed->avoidFilenameConflicts = true; + $assetTransformed->setVolumeId($volume->id); + $assetTransformed->setScenario(Asset::SCENARIO_CREATE); + + $assetTransformed->validate(); + + if (Craft::$app->getElements()->saveElement($assetTransformed, false)) + { + return $assetTransformed; + } - return true; } - public function indexAsset($asset) - { - - $volume = $this->getImageVolume(); - $fileName = $this->getFileName($asset); - - if ($volume->fileExists($fileName)) { - - Craft::$app->getAssetIndexer()->indexFile( - $volume, - $fileName - ); - - } - - } - } diff --git a/src/variables/PdfTransformVariable.php b/src/variables/PdfTransformVariable.php index 8d61fa3..a487776 100644 --- a/src/variables/PdfTransformVariable.php +++ b/src/variables/PdfTransformVariable.php @@ -26,8 +26,21 @@ class PdfTransformVariable * @param null $optional * @return string */ + + public function render($asset) + { + return PdfTransform::$plugin->pdfTransformService->render($asset); + } + public function url($asset) { - return PdfTransform::$plugin->pdfTransformService->url($asset); + + $render = PdfTransform::$plugin->pdfTransformService->render($asset); + + if ($render) + { + return $render->getUrl(); + } + } }