Skip to content
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

spatie\image support #9

Merged
merged 2 commits into from
Dec 18, 2023
Merged
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
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: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
>
<php>
<ini name="error_reporting" value="-1" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0&amp;max[direct]=0"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0&amp;max[direct]=0&amp;quiet[]=indirect&amp;quiet[]=other"/>
</php>

<testsuites>
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'])
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@

namespace Zenstruck\Image\Tests\Transformer;

use Zenstruck\Image\Tests\TransformerTest;
use Zenstruck\Image\Tests\TransformerTestCase;
use Zenstruck\ImageFileInfo;
use Zenstruck\TempFile;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
abstract class FilterObjectTransformerTest extends TransformerTest
abstract class FilterObjectTransformerTestCase extends TransformerTestCase
{
/**
* @test
Expand Down
4 changes: 2 additions & 2 deletions tests/Transformer/GdImageTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

namespace Zenstruck\Image\Tests\Transformer;

use Zenstruck\Image\Tests\TransformerTest;
use Zenstruck\Image\Tests\TransformerTestCase;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
final class GdImageTransformerTest extends TransformerTest
final class GdImageTransformerTest extends TransformerTestCase
{
protected function filterInvokable(): object
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Transformer/ImagickTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

namespace Zenstruck\Image\Tests\Transformer;

use Zenstruck\Image\Tests\TransformerTest;
use Zenstruck\Image\Tests\TransformerTestCase;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
final class ImagickTransformerTest extends TransformerTest
final class ImagickTransformerTest extends TransformerTestCase
{
protected function setUp(): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Transformer/ImagineTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
final class ImagineTransformerTest extends FilterObjectTransformerTest
final class ImagineTransformerTest extends FilterObjectTransformerTestCase
{
protected function filterCallback(): callable
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Transformer/InterventionTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
final class InterventionTransformerTest extends FilterObjectTransformerTest
final class InterventionTransformerTest extends FilterObjectTransformerTestCase
{
protected function filterCallback(): callable
{
Expand Down
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);
}
}
10 changes: 5 additions & 5 deletions tests/TransformerTest.php → tests/TransformerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
abstract class TransformerTest extends TestCase
abstract class TransformerTestCase extends TestCase
{
/**
* @test
Expand Down 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