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:
@@ -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) {
|
return match ($extension) {
|
||||||
'webp' => new WebpEncoder(),
|
'webp' => new WebpEncoder(),
|
||||||
'avif' => new AvifEncoder(),
|
'avif' => new AvifEncoder(),
|
||||||
|
33
src/Encoders/FilePathEncoder.php
Normal file
33
src/Encoders/FilePathEncoder.php
Normal 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)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@@ -15,6 +15,7 @@ use Intervention\Image\Encoders\AutoEncoder;
|
|||||||
use Intervention\Image\Encoders\AvifEncoder;
|
use Intervention\Image\Encoders\AvifEncoder;
|
||||||
use Intervention\Image\Encoders\BmpEncoder;
|
use Intervention\Image\Encoders\BmpEncoder;
|
||||||
use Intervention\Image\Encoders\FileExtensionEncoder;
|
use Intervention\Image\Encoders\FileExtensionEncoder;
|
||||||
|
use Intervention\Image\Encoders\FilePathEncoder;
|
||||||
use Intervention\Image\Encoders\GifEncoder;
|
use Intervention\Image\Encoders\GifEncoder;
|
||||||
use Intervention\Image\Encoders\JpegEncoder;
|
use Intervention\Image\Encoders\JpegEncoder;
|
||||||
use Intervention\Image\Encoders\MediaTypeEncoder;
|
use Intervention\Image\Encoders\MediaTypeEncoder;
|
||||||
@@ -250,6 +251,20 @@ final class Image implements ImageInterface, Countable
|
|||||||
return $this->driver->resolve($encoder)->encode($this);
|
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}
|
* {@inheritdoc}
|
||||||
*
|
*
|
||||||
@@ -766,6 +781,16 @@ final class Image implements ImageInterface, Countable
|
|||||||
return $this->encode(new FileExtensionEncoder($extension));
|
return $this->encode(new FileExtensionEncoder($extension));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*
|
||||||
|
* @see ImageInterface::encodeByPath()
|
||||||
|
*/
|
||||||
|
public function encodeByPath(?string $path = null): EncodedImageInterface
|
||||||
|
{
|
||||||
|
return $this->encode(new FilePathEncoder($path));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*
|
*
|
||||||
|
@@ -67,6 +67,15 @@ interface ImageInterface extends IteratorAggregate, Countable
|
|||||||
*/
|
*/
|
||||||
public function encode(EncoderInterface $encoder): EncodedImage;
|
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
|
* Apply given modifier to current image
|
||||||
*
|
*
|
||||||
@@ -576,6 +585,16 @@ interface ImageInterface extends IteratorAggregate, Countable
|
|||||||
*/
|
*/
|
||||||
public function encodeByExtension(?string $extension = null): EncodedImageInterface;
|
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
|
* Encode image to JPEG format
|
||||||
*
|
*
|
||||||
|
@@ -35,6 +35,16 @@ class Origin
|
|||||||
return $this->mediaType();
|
return $this->mediaType();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return file path of origin
|
||||||
|
*
|
||||||
|
* @return null|string
|
||||||
|
*/
|
||||||
|
public function filePath(): ?string
|
||||||
|
{
|
||||||
|
return $this->filePath;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set file path for origin
|
* Set file path for origin
|
||||||
*
|
*
|
||||||
|
@@ -144,6 +144,27 @@ class ImageTest extends TestCase
|
|||||||
$this->assertMediaType('image/png', (string) $result);
|
$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
|
public function testWidthHeightSize(): void
|
||||||
{
|
{
|
||||||
$this->assertEquals(3, $this->image->width());
|
$this->assertEquals(3, $this->image->width());
|
||||||
|
@@ -143,6 +143,27 @@ class ImageTest extends TestCase
|
|||||||
$this->assertMediaType('image/png', (string) $result);
|
$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
|
public function testWidthHeightSize(): void
|
||||||
{
|
{
|
||||||
$this->assertEquals(20, $this->image->width());
|
$this->assertEquals(20, $this->image->width());
|
||||||
|
Reference in New Issue
Block a user