Skip to content

Commit

Permalink
Added: Amazon S3 and Servd compatability
Browse files Browse the repository at this point in the history
  • Loading branch information
bymayo committed Nov 14, 2022
1 parent 3593f19 commit 0d083c8
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 63 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
21 changes: 9 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 <a href="#dimensions">Dimensions</a>.
Expand All @@ -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)

- <a href="https://docs.craftcms.com/v2/image-transforms.html" target="_blank">Image Transforms by Craft</a>
- <a href="https://github.com/aelvan/Imager-Craft" target="_blank">Imager by aelvan</a>
- <a href="https://github.com/nystudio107/craft-imageoptimize" target="_blank">Image Optimize by nystudio107</a>

### 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.
- <a href="https://plugins.craftcms.com/imager-x" target="_blank">Imager X by aelvan</a>
- <a href="https://plugins.craftcms.com/image-optimize" target="_blank">Image Optimize by nystudio107</a>

## 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.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
93 changes: 44 additions & 49 deletions src/services/PdfTransformService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
);

}

}

}
15 changes: 14 additions & 1 deletion src/variables/PdfTransformVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
}

0 comments on commit 0d083c8

Please sign in to comment.