Skip to content

Commit

Permalink
feat: spatie/image support
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Dec 18, 2023
1 parent c8d3308 commit 57d4199
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:

sca:
uses: zenstruck/.github/.github/workflows/php-stan.yml@main
with:
php: 8.2

fixcs:
name: Run php-cs-fixer
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ The following transformers are available:
- [Imagick](https://www.php.net/manual/en/book.imagick.php)
- [intervention\image](https://github.com/Intervention/image)
- [imagine\imagine](https://github.com/php-imagine/Imagine)
- [spatie\image](https://github.com/spatie/image)

To use the desired transformer, type-hint the first parameter of the callable
passed to `Zenstruck\ImageFileInfo::transform()` with the desired transformer's
Expand All @@ -64,6 +65,7 @@ _image object_:
- **Imagick**: `\Imagick`
- **intervention\image**: `Intervention\Image\Image`
- **imagine\imagine**: `Imagine\Image\ImageInterface`
- **spatie\image**: `Spatie\Image\Image`

> **Note**: The return value of the callable must be the same as the passed parameter.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"phpstan/phpstan": "^1.4",
"phpunit/phpunit": "^9.5.0",
"psr/container": "^1.0|^2.0",
"spatie/image": "^2.0|^3.2",
"symfony/phpunit-bridge": "^6.1",
"symfony/var-dumper": "^5.4|^6.0"
},
Expand Down
2 changes: 2 additions & 0 deletions src/Image/Transformer/MultiTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Intervention\Image\Filters\FilterInterface as InterventionFilter;
use Intervention\Image\Image as InterventionImage;
use Psr\Container\ContainerInterface;
use Spatie\Image\Image as SpatieImage;
use Zenstruck\Image\Transformer;

/**
Expand Down Expand Up @@ -104,6 +105,7 @@ private static function defaultTransformer(string $class): Transformer
\Imagick::class => new ImagickTransformer(),
ImagineImage::class, GdImagineImage::class, ImagickImagineImage::class, GmagickImagineImage::class => ImagineTransformer::createFor($class),
InterventionImage::class => new InterventionTransformer(),
SpatieImage::class => new SpatieImageTransformer(),
default => throw new \InvalidArgumentException(\sprintf('No transformer available for "%s".', $class)),
};
}
Expand Down
42 changes: 42 additions & 0 deletions src/Image/Transformer/SpatieImageTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the zenstruck/image package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Image\Transformer;

use Spatie\Image\Image;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*
* @internal
*
* @extends FileTransformer<Image>
*/
final class SpatieImageTransformer extends FileTransformer
{
protected function object(\SplFileInfo $image): object
{
return Image::load($image);
}

protected static function expectedClass(): string
{
return Image::class;
}

protected function save(object $object, array $options): void
{
$object
->format($options['format'])
->save($options['output'])
;
}
}
41 changes: 41 additions & 0 deletions tests/Transformer/SpatieImageTransformerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the zenstruck/image package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Image\Tests\Transformer;

use Spatie\Image\Image;
use Zenstruck\Image\Tests\TransformerTestCase;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
final class SpatieImageTransformerTest extends TransformerTestCase
{
protected function invalidFilterCallback(): callable
{
return fn(Image $i) => null;
}

protected function filterInvokable(): object
{
return new class() {
public function __invoke(Image $image): Image
{
return $image->width(100);
}
};
}

protected function filterCallback(): callable
{
return fn(Image $i) => $i->width(100);
}
}
8 changes: 4 additions & 4 deletions tests/TransformerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function can_transform_into_temp_image(): void
*/
public function can_transform_to_specific_file(): void
{
$output = TempFile::new();
$output = TempFile::withExtension('jpg');
$image = new ImageFileInfo(__DIR__.'/Fixture/files/symfony.jpg');

$resized = $image->transform($this->filterCallback(), ['output' => $output]);
Expand All @@ -63,7 +63,7 @@ public function can_transform_to_specific_file(): void
*/
public function can_transform_in_place(): void
{
$image = ImageFileInfo::from(new \SplFileInfo(__DIR__.'/Fixture/files/symfony.jpg'));
$image = new ImageFileInfo(TempFile::for(new \SplFileInfo(__DIR__.'/Fixture/files/symfony.jpg'), extension: 'jpg'));

$this->assertSame(678, $image->dimensions()->height());
$this->assertSame(563, $image->dimensions()->width());
Expand Down Expand Up @@ -105,7 +105,7 @@ public function can_transform_into_temp_image_with_invokable_object(): void
*/
public function can_transform_to_specific_file_with_invokable_object(): void
{
$output = TempFile::new();
$output = TempFile::withExtension('jpg');
$image = new ImageFileInfo(__DIR__.'/Fixture/files/symfony.jpg');

$resized = $image->transform($this->filterInvokable(), ['output' => $output]);
Expand All @@ -121,7 +121,7 @@ public function can_transform_to_specific_file_with_invokable_object(): void
*/
public function can_transform_in_place_with_invokable_object(): void
{
$image = ImageFileInfo::from(new \SplFileInfo(__DIR__.'/Fixture/files/symfony.jpg'));
$image = new ImageFileInfo(TempFile::for(new \SplFileInfo(__DIR__.'/Fixture/files/symfony.jpg'), extension: 'jpg'));

$this->assertSame(678, $image->dimensions()->height());
$this->assertSame(563, $image->dimensions()->width());
Expand Down

0 comments on commit 57d4199

Please sign in to comment.