Skip to content

Commit

Permalink
Tranformer rules
Browse files Browse the repository at this point in the history
  • Loading branch information
timkelty committed Sep 12, 2023
1 parent 74edee9 commit f172405
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
8 changes: 8 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

# Add to path for GUIs (Tower, etc.)
PATH="/usr/local/bin:$PATH"
[[ -s $HOME/.nvm/nvm.sh ]] && . $HOME/.nvm/nvm.sh

npx lint-staged
57 changes: 48 additions & 9 deletions src/ImageTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

use Craft;
use craft\base\Component;
use craft\base\imagetransforms\ImageEditorTransformerInterface;
use craft\base\imagetransforms\ImageTransformerInterface;
use craft\elements\Asset;
use craft\errors\ImageTransformException;
use craft\helpers\UrlHelper;
use craft\models\ImageTransform;
use Illuminate\Support\Collection;
use yii\base\NotSupportedException;

/**
* TODO: ImageEditorTransformerInterface
Expand All @@ -23,10 +24,20 @@ class ImageTransformer extends Component implements ImageTransformerInterface
public function getTransformUrl(Asset $asset, ImageTransform $imageTransform, bool $immediately): string
{
$this->asset = $asset;
$fs = $asset->getVolume()->getTransformFs();
$assetUrl = $this->asset->getUrl();
$mimeType = $asset->getMimeType();

if (!$assetUrl) {
return '';
if (!$fs->hasUrls) {
throw new NotSupportedException('The asset’s volume’s transform filesystem doesn’t have URLs.');
}

if ($mimeType === 'image/gif' && !Craft::$app->getConfig()->getGeneral()->transformGifs) {
throw new NotSupportedException('GIF files shouldn’t be transformed.');
}

if ($mimeType === 'image/svg+xml' && !Craft::$app->getConfig()->getGeneral()->transformSvgs) {
throw new NotSupportedException('SVG files shouldn’t be transformed.');
}

$transformParams = $this->buildTransformParams($imageTransform);
Expand All @@ -51,16 +62,41 @@ public function buildTransformParams(ImageTransform $imageTransform): array
'format' => $this->getFormatValue($imageTransform),
'fit' => $this->getFitValue($imageTransform),
'background' => $this->getBackgroundValue($imageTransform),
'gravity' => $this->getGravityValue(),
'gravity' => $this->getGravityValue($imageTransform),
])->whereNotNull()->all();
}


protected function getGravityValue(): ?string
protected function getGravityValue(ImageTransform $imageTransform): ?array
{
return $this->asset->getHasFocalPoint()
? $this->asset->getFocalPoint()
: null;
if ($this->asset->getHasFocalPoint()) {
return $this->asset->getFocalPoint();
}

if ($imageTransform->position === 'center-center') {
return null;
}

// TODO: maybe just do this in Craft
$parts = explode('-', $imageTransform->position);
$yPosition = $parts[0] ?? null;
$xPosition = $parts[1] ?? null;

try {
$x = match ($xPosition) {
'top' => 0,
'center' => 0.5,
'bottom' => 1,
};
$y = match ($yPosition) {
'top' => 0,
'center' => 0.5,
'bottom' => 1,
};
} catch (\UnhandledMatchError $e) {
throw new ImageTransformException('Invalid `position` value.');
}

return [$x, $y];
}

protected function getBackgroundValue(ImageTransform $imageTransform): ?string
Expand All @@ -72,6 +108,9 @@ protected function getBackgroundValue(ImageTransform $imageTransform): ?string

protected function getFitValue(ImageTransform $imageTransform): string
{
// @see https://developers.cloudflare.com/images/image-resizing/url-format/#fit
// Cloudflare doesn't have an exact match to `stretch`.
// `cover` is close, but will crop instead of stretching.
return match ($imageTransform->mode) {
'fit' => $imageTransform->upscale ? 'contain' : 'scale-down',
'stretch' => 'cover',
Expand Down

0 comments on commit f172405

Please sign in to comment.