mirror of
https://github.com/Intervention/image.git
synced 2025-08-16 19:04:00 +02:00
Merge branch 'feature/heic' into next
This commit is contained in:
28
src/Drivers/Imagick/Encoders/HeicEncoder.php
Normal file
28
src/Drivers/Imagick/Encoders/HeicEncoder.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Drivers\Imagick\Encoders;
|
||||||
|
|
||||||
|
use Intervention\Image\Drivers\DriverSpecializedEncoder;
|
||||||
|
use Intervention\Image\EncodedImage;
|
||||||
|
use Intervention\Image\Interfaces\ImageInterface;
|
||||||
|
use Intervention\Image\Interfaces\EncodedImageInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property int $quality
|
||||||
|
*/
|
||||||
|
class HeicEncoder extends DriverSpecializedEncoder
|
||||||
|
{
|
||||||
|
public function encode(ImageInterface $image): EncodedImageInterface
|
||||||
|
{
|
||||||
|
$format = 'HEIC';
|
||||||
|
|
||||||
|
$imagick = $image->core()->native();
|
||||||
|
|
||||||
|
$imagick->setFormat($format);
|
||||||
|
$imagick->setImageFormat($format);
|
||||||
|
$imagick->setCompressionQuality($this->quality);
|
||||||
|
$imagick->setImageCompressionQuality($this->quality);
|
||||||
|
|
||||||
|
return new EncodedImage($imagick->getImagesBlob(), 'image/heic');
|
||||||
|
}
|
||||||
|
}
|
@@ -57,6 +57,7 @@ class FileExtensionEncoder extends AutoEncoder
|
|||||||
'png' => new PngEncoder(),
|
'png' => new PngEncoder(),
|
||||||
'tiff', 'tif' => new TiffEncoder(quality: $this->quality),
|
'tiff', 'tif' => new TiffEncoder(quality: $this->quality),
|
||||||
'jp2', 'j2k', 'jpf', 'jpm', 'jpg2', 'j2c', 'jpc', 'jpx' => new Jpeg2000Encoder(quality: $this->quality),
|
'jp2', 'j2k', 'jpf', 'jpm', 'jpg2', 'j2c', 'jpc', 'jpx' => new Jpeg2000Encoder(quality: $this->quality),
|
||||||
|
'heic', 'heif' => new HeicEncoder(quality: $this->quality),
|
||||||
default => throw new EncoderException('No encoder found for file extension (' . $extension . ').'),
|
default => throw new EncoderException('No encoder found for file extension (' . $extension . ').'),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
10
src/Encoders/HeicEncoder.php
Normal file
10
src/Encoders/HeicEncoder.php
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Encoders;
|
||||||
|
|
||||||
|
class HeicEncoder extends AbstractEncoder
|
||||||
|
{
|
||||||
|
public function __construct(public int $quality = 75)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
@@ -46,6 +46,7 @@ class MediaTypeEncoder extends AbstractEncoder implements EncoderInterface
|
|||||||
'image/png' => new PngEncoder(),
|
'image/png' => new PngEncoder(),
|
||||||
'image/tiff' => new TiffEncoder(quality: $this->quality),
|
'image/tiff' => new TiffEncoder(quality: $this->quality),
|
||||||
'image/jp2', 'image/jpx', 'image/jpm' => new Jpeg2000Encoder(quality: $this->quality),
|
'image/jp2', 'image/jpx', 'image/jpm' => new Jpeg2000Encoder(quality: $this->quality),
|
||||||
|
'image/heic', 'image/heif', => new HeicEncoder(quality: $this->quality),
|
||||||
default => throw new EncoderException('No encoder found for media type (' . $type . ').'),
|
default => throw new EncoderException('No encoder found for media type (' . $type . ').'),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@@ -16,6 +16,7 @@ use Intervention\Image\Encoders\BmpEncoder;
|
|||||||
use Intervention\Image\Encoders\FileExtensionEncoder;
|
use Intervention\Image\Encoders\FileExtensionEncoder;
|
||||||
use Intervention\Image\Encoders\FilePathEncoder;
|
use Intervention\Image\Encoders\FilePathEncoder;
|
||||||
use Intervention\Image\Encoders\GifEncoder;
|
use Intervention\Image\Encoders\GifEncoder;
|
||||||
|
use Intervention\Image\Encoders\HeicEncoder;
|
||||||
use Intervention\Image\Encoders\Jpeg2000Encoder;
|
use Intervention\Image\Encoders\Jpeg2000Encoder;
|
||||||
use Intervention\Image\Encoders\JpegEncoder;
|
use Intervention\Image\Encoders\JpegEncoder;
|
||||||
use Intervention\Image\Encoders\MediaTypeEncoder;
|
use Intervention\Image\Encoders\MediaTypeEncoder;
|
||||||
@@ -927,6 +928,16 @@ final class Image implements ImageInterface
|
|||||||
return $this->toTiff(...$options);
|
return $this->toTiff(...$options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*
|
||||||
|
* @see ImageInterface::toHeic()
|
||||||
|
*/
|
||||||
|
public function toHeic(int $quality = 75): EncodedImageInterface
|
||||||
|
{
|
||||||
|
return $this->encode(new HeicEncoder($quality));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clone image
|
* Clone image
|
||||||
*
|
*
|
||||||
|
@@ -659,4 +659,12 @@ interface ImageInterface extends IteratorAggregate, Countable
|
|||||||
* @return EncodedImageInterface
|
* @return EncodedImageInterface
|
||||||
*/
|
*/
|
||||||
public function toTiff(mixed ...$options): EncodedImageInterface;
|
public function toTiff(mixed ...$options): EncodedImageInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encode image to HEIC format
|
||||||
|
*
|
||||||
|
* @param mixed $options
|
||||||
|
* @return EncodedImageInterface
|
||||||
|
*/
|
||||||
|
public function toHeic(mixed ...$options): EncodedImageInterface;
|
||||||
}
|
}
|
||||||
|
41
tests/Drivers/Imagick/Encoders/HeicEncoderTest.php
Normal file
41
tests/Drivers/Imagick/Encoders/HeicEncoderTest.php
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Intervention\Image\Tests\Drivers\Imagick\Encoders;
|
||||||
|
|
||||||
|
use Imagick;
|
||||||
|
use ImagickPixel;
|
||||||
|
use Intervention\Image\Drivers\Imagick\Core;
|
||||||
|
use Intervention\Image\Drivers\Imagick\Driver;
|
||||||
|
use Intervention\Image\Encoders\HeicEncoder;
|
||||||
|
use Intervention\Image\Image;
|
||||||
|
use Intervention\Image\Tests\TestCase;
|
||||||
|
use Intervention\Image\Tests\Traits\CanCreateImagickTestImage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires extension imagick
|
||||||
|
*/
|
||||||
|
final class HeicEncoderTest extends TestCase
|
||||||
|
{
|
||||||
|
use CanCreateImagickTestImage;
|
||||||
|
|
||||||
|
protected function getTestImage(): Image
|
||||||
|
{
|
||||||
|
$imagick = new Imagick();
|
||||||
|
$imagick->newImage(3, 2, new ImagickPixel('red'), 'jpg');
|
||||||
|
|
||||||
|
return new Image(
|
||||||
|
new Driver(),
|
||||||
|
new Core($imagick)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testEncode(): void
|
||||||
|
{
|
||||||
|
$image = $this->getTestImage();
|
||||||
|
$encoder = new HeicEncoder(75);
|
||||||
|
$result = $encoder->encode($image);
|
||||||
|
$this->assertMediaType('image/heic', (string) $result);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user