Skip to content

Try to upgrade to intervention/image v3 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: league/flysystem-v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
],
"require": {
"php": ">=8.1",
"intervention/image": "^2.5",
"spatie/image-optimizer": "^1.2",
"intervention/image": "^3.2.0",
"spatie/image-optimizer": "^1.2.0",
"ext-json": "*"
},
"require-dev": {
Expand All @@ -43,7 +43,8 @@
},
"autoload-dev": {
"psr-4": {
"PhpCollective\\Test\\": "tests/"
"PhpCollective\\Test\\": "tests/",
"TestApp\\": "tests/test_app/src/"
}
},
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ parameters:
paths:
- src/
checkGenericClassInNonGenericObjectType: false
checkMissingIterableValueType: false
10 changes: 6 additions & 4 deletions src/ImageProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace PhpCollective\Infrastructure\Storage\Processor\Image;

use GuzzleHttp\Psr7\StreamWrapper;
use Intervention\Image\Interfaces\ImageInterface;
use InvalidArgumentException;
use Intervention\Image\Image;
use Intervention\Image\ImageManager;
Expand Down Expand Up @@ -74,9 +75,9 @@ class ImageProcessor implements ProcessorInterface
protected ImageManager $imageManager;

/**
* @var \Intervention\Image\Image
* @var \Intervention\Image\Interfaces\ImageInterface
*/
protected Image $image;
protected ImageInterface $image;

/**
* Quality setting for writing images
Expand Down Expand Up @@ -239,7 +240,7 @@ public function process(FileInterface $file): FileInterface
continue;
}

$this->image = $this->imageManager->make($tempFile);
$this->image = $this->imageManager->read($tempFile);
$operations = new Operations($this->image);

// Apply the operations
Expand Down Expand Up @@ -285,11 +286,12 @@ protected function optimizeAndStore(FileInterface $file, string $path): void

// We need more tmp files because the optimizer likes to write
// and read the files from disk, not from a stream. :(
//FIXME Use memory/stream instead?
$optimizerTempFile = TemporaryFile::create();
$optimizerOutput = TemporaryFile::create();

// Save the image to the tmp file
$this->image->save($optimizerTempFile, 90, $file->extension());
$this->image->save($optimizerTempFile, 90);
// Optimize it and write it to another file
$this->optimizer()->optimize($optimizerTempFile, $optimizerOutput);
// Open a new stream for the storage system
Expand Down
7 changes: 3 additions & 4 deletions src/ImageVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,16 @@ public function callback(callable $callback): self
}

/**
* @link http://image.intervention.io/api/fit
* @param int $width Width
* @param int|null $height Height
* @param null $height Height
* @param callable|null $callback Callback
* @param bool $preventUpscale Prevent Upscaling
* @param string $position Position
* @return $this
*/
public function fit(
public function cover(
int $width,
?int $height = null,
int $height,
?callable $callback = null,
bool $preventUpscale = false,
string $position = 'center'
Expand Down
150 changes: 79 additions & 71 deletions src/Operations.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,36 @@

namespace PhpCollective\Infrastructure\Storage\Processor\Image;

use Intervention\Image\Image;
use Intervention\Image\Interfaces\ImageInterface;
use InvalidArgumentException;
use PhpCollective\Infrastructure\Storage\Processor\Image\Exception\UnsupportedOperationException;

/**
* Operations
*
* @link https://image.intervention.io/v3
*/
class Operations
{
public const POSITION_CENTER = 'center';
public const POSITION_TOP_CENTER = 'top-center';
public const POSITION_BOTTOM_CENTER = 'bottom-center';
public const POSITION_LEFT_TOP = 'left-top';
public const POSITION_RIGHT_TOP = 'right-top';
public const POSITION_LEFT_CENTER = 'left-center';
public const POSITION_RIGHT_CENTER = 'right-center';
public const POSITION_LEFT_BOTTOM = 'left-bottom';
public const POSITION_RIGHT_BOTTOM = 'right-bottom';

/**
* @var \Intervention\Image\Image
* @var \Intervention\Image\Interfaces\ImageInterface
*/
protected Image $image;
protected ImageInterface $image;

/**
* @param \Intervention\Image\Image $image Image
* @param \Intervention\Image\Interfaces\ImageInterface $image Image
*/
public function __construct(Image $image)
public function __construct(ImageInterface $image)
{
$this->image = $image;
}
Expand All @@ -51,34 +63,37 @@ public function __call(string $name, array $arguments): mixed
/**
* Crops the image
*
* @link http://image.intervention.io/api/fit
* @param array<string, mixed> $arguments Arguments
* @return void
*/
public function fit(array $arguments): void
public function cover(array $arguments): void
{
if (!isset($arguments['width'])) {
throw new InvalidArgumentException('Missing width');
if (!isset($arguments['height'], $arguments['width'])) {
throw new InvalidArgumentException('Missing width or height');
}

$arguments += ['position' => static::POSITION_CENTER];
$preventUpscale = $arguments['preventUpscale'] ?? false;
$height = $arguments['height'] ?? null;
if ($preventUpscale) {
$this->image->coverDown(
(int)$arguments['width'],
(int)$arguments['height'],
$arguments['position']
);

$this->image->fit(
return;
}

$this->image->cover(
(int)$arguments['width'],
(int)$height,
static function ($constraint) use ($preventUpscale) {
if ($preventUpscale) {
$constraint->upsize();
}
}
(int)$arguments['height'],
$arguments['position']
);
}

/**
* Crops the image
*
* @link http://image.intervention.io/api/crop
* @param array<string, mixed> $arguments Arguments
* @return void
*/
Expand All @@ -88,19 +103,18 @@ public function crop(array $arguments): void
throw new InvalidArgumentException('Missing width or height');
}

$arguments = array_merge(['x' => null, 'y' => null], $arguments);
$arguments += ['x' => null, 'y' => null, 'position' => static::POSITION_CENTER];
$height = $arguments['height'] ? (int)$arguments['height'] : null;
$width = $arguments['width'] ? (int)$arguments['width'] : null;
$x = $arguments['x'] ? (int)$arguments['x'] : null;
$y = $arguments['y'] ? (int)$arguments['y'] : null;
$x = $arguments['x'] ? (int)$arguments['x'] : 0;
$y = $arguments['y'] ? (int)$arguments['y'] : 0;

$this->image->crop($width, $height, $x, $y);
$this->image->crop($width, $height, $x, $y, $arguments['position']);
}

/**
* Flips the image horizontal
*
* @link http://image.intervention.io/api/flip
* @return void
*/
public function flipHorizontal(): void
Expand All @@ -111,7 +125,6 @@ public function flipHorizontal(): void
/**
* Flips the image vertical
*
* @link http://image.intervention.io/api/flip
* @return void
*/
public function flipVertical(): void
Expand All @@ -122,7 +135,6 @@ public function flipVertical(): void
/**
* Flips the image
*
* @link http://image.intervention.io/api/flip
* @param array<string, mixed> $arguments Arguments
* @return void
*/
Expand All @@ -138,87 +150,85 @@ public function flip(array $arguments): void
);
}

$this->image->flip($arguments['direction']);
if ($arguments['direction'] === 'h') {
$this->image->flip();

return;
}

$this->image->flop();
}

/**
* Resizes the image
* @param array<string, mixed> $arguments
*
* @link http://image.intervention.io/api/resize
* @param array<string, mixed> $arguments Arguments
* @return void
*/
public function resize(array $arguments): void
public function scale(array $arguments): void
{
if (!isset($arguments['height'], $arguments['width'])) {
throw new InvalidArgumentException(
'Missing height or width'
);
}

$aspectRatio = $arguments['aspectRatio'] ?? true;
$preventUpscale = $arguments['preventUpscale'] ?? false;

$this->image->resize(
if ($preventUpscale) {
$this->image->scaleDown(
$arguments['width'],
$arguments['height']
);

return;
}

$this->image->scale(
$arguments['width'],
$arguments['height'],
static function ($constraint) use ($aspectRatio, $preventUpscale) {
if ($aspectRatio) {
$constraint->aspectRatio();
}
if ($preventUpscale) {
$constraint->upsize();
}
}
);
}

/**
* @link http://image.intervention.io/api/widen
* Resizes the image
*
* @param array<string, mixed> $arguments Arguments
* @return void
*/
public function widen(array $arguments): void
public function resize(array $arguments): void
{
if (!isset($arguments['width'])) {
if (!isset($arguments['height'], $arguments['width'])) {
throw new InvalidArgumentException(
'Missing width'
'Missing height or width'
);
}

$preventUpscale = $arguments['preventUpscale'] ?? false;

$this->image->widen((int)$arguments['width'], function ($constraint) use ($preventUpscale) {
if ($preventUpscale) {
$constraint->upsize();
}
});
}
// Deprecated: Coming from old API
$aspectRatio = $arguments['aspectRatio'] ?? null;
if ($aspectRatio !== null) {
$this->scale($arguments);

/**
* @link http://image.intervention.io/api/heighten
* @param array<string, mixed> $arguments Arguments
* @return void
*/
public function heighten(array $arguments): void
{
if (!isset($arguments['height'])) {
throw new InvalidArgumentException(
'Missing height'
);
return;
}

$preventUpscale = $arguments['preventUpscale'] ?? false;

$this->image->heighten((int)$arguments['height'], function ($constraint) use ($preventUpscale) {
if ($preventUpscale) {
$constraint->upsize();
}
});
if ($preventUpscale) {
$this->image->resizeDown(
$arguments['width'],
$arguments['height']
);

return;
}

$this->image->resize(
$arguments['width'],
$arguments['height'],
);
}

/**
* @link http://image.intervention.io/api/rotate
* @param array<string, mixed> $arguments Arguments
* @return void
*/
Expand All @@ -234,8 +244,6 @@ public function rotate(array $arguments): void
}

/**
* @link http://image.intervention.io/api/rotate
* @param array<string, mixed> $arguments Arguments
* @return void
*/
public function sharpen(array $arguments): void
Expand Down
13 changes: 3 additions & 10 deletions tests/TestCase/Processor/Image/ImageProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace PhpCollective\Test\TestCase\Processor\Image;

use Intervention\Image\Image;
use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\ImageManager;
use PhpCollective\Infrastructure\Storage\File;
use PhpCollective\Infrastructure\Storage\FileFactory;
Expand All @@ -25,6 +25,7 @@
use PhpCollective\Infrastructure\Storage\Processor\Image\ImageVariantCollection;
use PhpCollective\Infrastructure\Storage\Processor\Image\ImageProcessor;
use PhpCollective\Test\TestCase\TestCase;
use TestApp\Image;

/**
* ImageProcessorTest
Expand All @@ -41,15 +42,7 @@ public function testProcessor(): void

$pathBuilder = new PathBuilder();

$imageManager = $this->getMockBuilder(ImageManager::class)
->getMock();

$image = $this->getMockBuilder(Image::class)
->getMock();

$imageManager->expects($this->any())
->method('make')
->willReturn($image);
$imageManager = new ImageManager(new Driver());

$processor = new ImageProcessor(
$fileStorage,
Expand Down
Loading