-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Image::encodeByExtension()
- Loading branch information
1 parent
c6d52c3
commit 38cdd24
Showing
11 changed files
with
175 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Intervention\Image\Encoders; | ||
|
||
use Intervention\Gif\Exception\EncoderException; | ||
use Intervention\Image\Interfaces\EncodedImageInterface; | ||
use Intervention\Image\Interfaces\EncoderInterface; | ||
use Intervention\Image\Interfaces\ImageInterface; | ||
|
||
class FileExtensionEncoder extends AutoEncoder | ||
{ | ||
/** | ||
* Create new encoder instance to encode to format of given file extension | ||
* | ||
* @param null|string $extension | ||
* @return void | ||
*/ | ||
public function __construct(protected ?string $extension = null) | ||
{ | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @see EncoderInterface::encode() | ||
*/ | ||
public function encode(ImageInterface $image): EncodedImageInterface | ||
{ | ||
return $image->encode( | ||
$this->encoderByFileExtension( | ||
is_null($this->extension) ? $image->origin()->fileExtension() : $this->extension | ||
) | ||
); | ||
} | ||
|
||
protected function encoderByFileExtension(string $extension): EncoderInterface | ||
{ | ||
return match ($extension) { | ||
'webp' => new WebpEncoder(), | ||
'avif' => new AvifEncoder(), | ||
'jpeg', 'jpg' => new JpegEncoder(), | ||
'bmp' => new BmpEncoder(), | ||
'gif' => new GifEncoder(), | ||
'png' => new PngEncoder(), | ||
'tiff', 'tif' => new TiffEncoder(), | ||
default => throw new EncoderException('No encoder found for file extension (' . $extension . ').'), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters