1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-19 12:11:26 +02:00

Implement Image::encodeByPath()

This commit is contained in:
Oliver Vogel
2023-12-21 16:23:40 +01:00
parent 38cdd24c8f
commit 097bfed504
7 changed files with 141 additions and 1 deletions

View File

@@ -33,8 +33,19 @@ class FileExtensionEncoder extends AutoEncoder
);
}
protected function encoderByFileExtension(string $extension): EncoderInterface
/**
* Create matching encoder for given file extension
*
* @param string $extension
* @return EncoderInterface
* @throws EncoderException
*/
protected function encoderByFileExtension(?string $extension): EncoderInterface
{
if (empty($extension)) {
throw new EncoderException('No encoder found for empty file extension.');
}
return match ($extension) {
'webp' => new WebpEncoder(),
'avif' => new AvifEncoder(),

View File

@@ -0,0 +1,33 @@
<?php
namespace Intervention\Image\Encoders;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\EncodedImageInterface;
class FilePathEncoder extends FileExtensionEncoder
{
/**
* Create new encoder instance to encode to format of file extension in given path
*
* @param null|string $path
* @return void
*/
public function __construct(protected ?string $path = null)
{
}
/**
* {@inheritdoc}
*
* @see EncoderInterface::encode()
*/
public function encode(ImageInterface $image): EncodedImageInterface
{
return $image->encode(
$this->encoderByFileExtension(
is_null($this->path) ? $image->origin()->fileExtension() : pathinfo($this->path, PATHINFO_EXTENSION)
)
);
}
}

View File

@@ -15,6 +15,7 @@ use Intervention\Image\Encoders\AutoEncoder;
use Intervention\Image\Encoders\AvifEncoder;
use Intervention\Image\Encoders\BmpEncoder;
use Intervention\Image\Encoders\FileExtensionEncoder;
use Intervention\Image\Encoders\FilePathEncoder;
use Intervention\Image\Encoders\GifEncoder;
use Intervention\Image\Encoders\JpegEncoder;
use Intervention\Image\Encoders\MediaTypeEncoder;
@@ -250,6 +251,20 @@ final class Image implements ImageInterface, Countable
return $this->driver->resolve($encoder)->encode($this);
}
/**
* {@inheritdoc}
*
* @see ImageInterface::save()
*/
public function save(?string $path = null): ImageInterface
{
$path = is_null($path) ? $this->origin()->filePath() : $path;
$this->encodeByPath($path)->save($path);
return $this;
}
/**
* {@inheritdoc}
*
@@ -766,6 +781,16 @@ final class Image implements ImageInterface, Countable
return $this->encode(new FileExtensionEncoder($extension));
}
/**
* {@inheritdoc}
*
* @see ImageInterface::encodeByPath()
*/
public function encodeByPath(?string $path = null): EncodedImageInterface
{
return $this->encode(new FilePathEncoder($path));
}
/**
* {@inheritdoc}
*

View File

@@ -67,6 +67,15 @@ interface ImageInterface extends IteratorAggregate, Countable
*/
public function encode(EncoderInterface $encoder): EncodedImage;
/**
* Save the image to the specified path in the file system. If no path is
* given, the image will be saved at its original location.
*
* @param null|string $path
* @return ImageInterface
*/
public function save(?string $path = null): ImageInterface;
/**
* Apply given modifier to current image
*
@@ -576,6 +585,16 @@ interface ImageInterface extends IteratorAggregate, Countable
*/
public function encodeByExtension(?string $extension = null): EncodedImageInterface;
/**
* Encode the image into the format represented by the given extension of
* the given file path extension is given the image will be encoded to
* the format of the originally read image.
*
* @param null|string $path
* @return EncodedImageInterface
*/
public function encodeByPath(?string $path = null): EncodedImageInterface;
/**
* Encode image to JPEG format
*

View File

@@ -35,6 +35,16 @@ class Origin
return $this->mediaType();
}
/**
* Return file path of origin
*
* @return null|string
*/
public function filePath(): ?string
{
return $this->filePath;
}
/**
* Set file path for origin
*

View File

@@ -144,6 +144,27 @@ class ImageTest extends TestCase
$this->assertMediaType('image/png', (string) $result);
}
public function testEncodeByPath(): void
{
$result = $this->readTestImage('blue.gif')->encodeByPath();
$this->assertInstanceOf(EncodedImage::class, $result);
$this->assertMediaType('image/gif', (string) $result);
$result = $this->readTestImage('blue.gif')->encodeByPath('foo/bar.png');
$this->assertInstanceOf(EncodedImage::class, $result);
$this->assertMediaType('image/png', (string) $result);
}
public function testSaveAsFormat(): void
{
$path = __DIR__ . '/tmp.png';
$result = $this->readTestImage('blue.gif')->save($path);
$this->assertInstanceOf(Image::class, $result);
$this->assertFileExists($path);
$this->assertMediaType('image/png', file_get_contents($path));
unlink($path);
}
public function testWidthHeightSize(): void
{
$this->assertEquals(3, $this->image->width());

View File

@@ -143,6 +143,27 @@ class ImageTest extends TestCase
$this->assertMediaType('image/png', (string) $result);
}
public function testEncodeByPath(): void
{
$result = $this->readTestImage('blue.gif')->encodeByPath();
$this->assertInstanceOf(EncodedImage::class, $result);
$this->assertMediaType('image/gif', (string) $result);
$result = $this->readTestImage('blue.gif')->encodeByPath('foo/bar.png');
$this->assertInstanceOf(EncodedImage::class, $result);
$this->assertMediaType('image/png', (string) $result);
}
public function testSaveAsFormat(): void
{
$path = __DIR__ . '/tmp.png';
$result = $this->readTestImage('blue.gif')->save($path);
$this->assertInstanceOf(Image::class, $result);
$this->assertFileExists($path);
$this->assertMediaType('image/png', file_get_contents($path));
unlink($path);
}
public function testWidthHeightSize(): void
{
$this->assertEquals(20, $this->image->width());