Skip to content

Commit

Permalink
DEP Upgrade to intervention/image 3
Browse files Browse the repository at this point in the history
Also add strict typing to Image_Backend and InterventionBackend.
  • Loading branch information
GuySartorelli committed Jul 10, 2024
1 parent 7078c36 commit 26d7a95
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 407 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"silverstripe/framework": "^6",
"silverstripe/vendor-plugin": "^2",
"symfony/filesystem": "^6.1",
"intervention/image": "^2.7.2",
"intervention/image": "^3.7",
"league/flysystem": "^3.9.0"
},
"require-dev": {
Expand Down
103 changes: 3 additions & 100 deletions src/Conversion/InterventionImageFileConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace SilverStripe\Assets\Conversion;

use Imagick;
use Intervention\Image\Exception\ImageException;
use SilverStripe\Assets\File;
use SilverStripe\Assets\Image_Backend;
Expand Down Expand Up @@ -31,7 +30,9 @@ public function supportsConversion(string $fromExtension, string $toExtension, a
if (!is_a($backend, InterventionBackend::class)) {
return false;
}
return $this->supportedByIntervention($fromExtension, $backend) && $this->supportedByIntervention($toExtension, $backend);
/** @var InterventionBackend $backend */
$driver = $backend->getImageManager()->driver();
return $driver->supports($fromExtension) && $driver->supports($toExtension);
}

public function convert(DBFile|File $from, string $toExtension, array $options = []): DBFile
Expand Down Expand Up @@ -90,102 +91,4 @@ private function validateOptions(array $options): array
}
return $problems;
}

private function supportedByIntervention(string $format, InterventionBackend $backend): bool
{
$driver = $backend->getImageManager()->config['driver'] ?? null;

// Return early for empty values - we obviously can't support that
if ($format === '') {
return false;
}

$format = strtolower($format);

// If the driver is somehow not GD or Imagick, we have no way to know what it might support
if ($driver !== 'gd' && $driver !== 'imagick') {
$supported = false;
$this->extend('updateSupportedByIntervention', $supported, $format, $driver);
return $supported;
}

// GD and Imagick support different things.
// This follows the logic in intervention's AbstractEncoder::process() method
// and the various methods in the Encoder classes for GD and Imagick,
// excluding checking for strings that were obviously mimetypes
switch ($format) {
case 'gif':
// always supported
return true;
case 'png':
// always supported
return true;
case 'jpg':
case 'jpeg':
case 'jfif':
// always supported
return true;
case 'tif':
case 'tiff':
if ($driver === 'gd') {
false;
}
// always supported by imagick
return true;
case 'bmp':
case 'ms-bmp':
case 'x-bitmap':
case 'x-bmp':
case 'x-ms-bmp':
case 'x-win-bitmap':
case 'x-windows-bmp':
case 'x-xbitmap':
if ($driver === 'gd' && !function_exists('imagebmp')) {
return false;
}
// always supported by imagick
return true;
case 'ico':
if ($driver === 'gd') {
return false;
}
// always supported by imagick
return true;
case 'psd':
if ($driver === 'gd') {
return false;
}
// always supported by imagick
return true;
case 'webp':
if ($driver === 'gd' && !function_exists('imagewebp')) {
return false;
}
if ($driver === 'imagick' && !Imagick::queryFormats('WEBP')) {
return false;
}
return true;
case 'avif':
if ($driver === 'gd' && !function_exists('imageavif')) {
return false;
}
if ($driver === 'imagick' && !Imagick::queryFormats('AVIF')) {
return false;
}
return true;
case 'heic':
if ($driver === 'gd') {
return false;
}
if ($driver === 'imagick' && !Imagick::queryFormats('HEIC')) {
return false;
}
return true;
default:
// Anything else is not supported
return false;
}
// This should never be reached, but return false if it is
return false;
}
}
84 changes: 26 additions & 58 deletions src/Image_Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,42 +36,36 @@ interface Image_Backend
public function __construct(AssetContainer $assetContainer = null);

/**
* @return int The width of the image
* Get the width of the image
*/
public function getWidth();
public function getWidth(): int;

/**
* @return int The height of the image
* Get the height of the image
*/
public function getHeight();
public function getHeight(): int;

/**
* Populate the backend with a given object
*
* @param AssetContainer $assetContainer Object to load from
*/
public function loadFromContainer(AssetContainer $assetContainer);
public function loadFromContainer(AssetContainer $assetContainer): static;

/**
* Populate the backend from a local path
*
* @param string $path
*/
public function loadFrom($path);
public function loadFrom(string $path): static;

/**
* Get the currently assigned image resource
*
* @return mixed
*/
public function getImageResource();
public function getImageResource(): mixed;

/**
* Set the currently assigned image resource
*
* @param mixed $resource
*/
public function setImageResource($resource);
public function setImageResource($resource): static;

/**
* Write to the given asset store
Expand All @@ -84,87 +78,61 @@ public function setImageResource($resource);
* @return array Tuple associative array (Filename, Hash, Variant) Unless storing a variant, the hash
* will be calculated from the given data.
*/
public function writeToStore(AssetStore $assetStore, $filename, $hash = null, $variant = null, $config = []);
public function writeToStore(AssetStore $assetStore, string $filename, ?string $hash = null, ?string $variant = null, array $config = []): array;

/**
* Write the backend to a local path
*
* @param string $path
* @return bool if the write was successful
*/
public function writeTo($path);
public function writeTo(string $path): bool;

/**
* Set the quality to a value between 0 and 100
*
* @param int $quality
*/
public function setQuality($quality);
public function setQuality(int $quality): static;

/**
* Resize an image, skewing it as necessary.
*
* @param int $width
* @param int $height
* @return static
*/
public function resize($width, $height);
public function resize(int $width, int $height): static;

/**
* Resize the image by preserving aspect ratio. By default, it will keep the image inside the maxWidth
* and maxHeight. Passing useAsMinimum will make the smaller dimension equal to the maximum corresponding dimension
*
* @param int $width
* @param int $height
* @param bool $useAsMinimum If true, image will be sized outside of these dimensions.
* If false (default) image will be sized inside these dimensions.
* @return static
* Resize the image by preserving aspect ratio. By default, the image cannot be resized to be larger
* than its current size.
* Passing true to useAsMinimum will allow the image to be scaled up.
*/
public function resizeRatio($width, $height, $useAsMinimum = false);
public function resizeRatio(int $width, int $height, bool $useAsMinimum = false): static;

/**
* Resize an image by width. Preserves aspect ratio.
*
* @param int $width
* @return static
*/
public function resizeByWidth($width);
public function resizeByWidth(int $width): static;

/**
* Resize an image by height. Preserves aspect ratio.
*
* @param int $height
* @return static
*/
public function resizeByHeight($height);
public function resizeByHeight(int $height): static;

/**
* Return a clone of this image resized, with space filled in with the given colour
*
* @param int $width
* @param int $height
* @param string $backgroundColor
* @param int $transparencyPercent
* @return static
* Return a clone of this image resized, with space filled in with the given colour.
*/
public function paddedResize($width, $height, $backgroundColor = "FFFFFF", $transparencyPercent = 0);
public function paddedResize(string $width, string $height, string $backgroundColour = 'FFFFFF', int $transparencyPercent = 0): static;

/**
* Resize an image to cover the given width/height completely, and crop off any overhanging edges.
*
* @param int $width
* @param int $height
* @return static
*/
public function croppedResize($width, $height);
public function croppedResize(int $width, int $height, string $position): static;

/**
* Crop's part of image.
* @param int $top y position of left upper corner of crop rectangle
* @param int $left x position of left upper corner of crop rectangle
* @param int $top Amount of pixels the cutout will be moved on the y (vertical) axis
* @param int $left Amount of pixels the cutout will be moved on the x (horizontal) axis
* @param int $width rectangle width
* @param int $height rectangle height
* @return Image_Backend
* @param string $position Postion at which the cutout will be aligned
* @param string $backgroundColour Colour to fill any newly created areas
*/
public function crop($top, $left, $width, $height);
public function crop(int $top, int $left, int $width, int $height, string $position, string $backgroundColour = 'FFFFFF'): static;
}
Loading

0 comments on commit 26d7a95

Please sign in to comment.