Skip to content

Commit

Permalink
add support for HEIC
Browse files Browse the repository at this point in the history
  • Loading branch information
frasmage committed Dec 5, 2024
1 parent 0adf971 commit b094b69
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
2 changes: 2 additions & 0 deletions config/mediable.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,14 @@
'image/jpeg',
'image/png',
'image/gif',
'image/heic',
],
'extensions' => [
'jpg',
'jpeg',
'png',
'gif',
'heic',
]
],
Plank\Mediable\Media::TYPE_IMAGE_VECTOR => [
Expand Down
22 changes: 16 additions & 6 deletions src/ImageManipulation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,30 @@

class ImageManipulation
{
public const FORMAT_BMP = 'bmp';
public const FORMAT_GIF = 'gif';
public const FORMAT_HEIC = 'heic';
public const FORMAT_JPG = 'jpg';
public const FORMAT_PNG = 'png';
public const FORMAT_GIF = 'gif';
public const FORMAT_TIFF = 'tif';
public const FORMAT_BMP = 'bmp';
public const FORMAT_WEBP = 'webp';

public const VALID_IMAGE_FORMATS = [
self::FORMAT_BMP,
self::FORMAT_GIF,
self::FORMAT_HEIC,
self::FORMAT_JPG,
self::FORMAT_PNG,
self::FORMAT_GIF,
self::FORMAT_TIFF,
self::FORMAT_BMP
];

public const MIME_TYPE_MAP = [
self::FORMAT_BMP => 'image/bmp',
self::FORMAT_GIF => 'image/gif',
self::FORMAT_HEIC => 'image/heic',
self::FORMAT_JPG => 'image/jpeg',
self::FORMAT_PNG => 'image/png',
self::FORMAT_GIF => 'image/gif',
self::FORMAT_TIFF => 'image/tiff',
self::FORMAT_BMP => 'image/bmp',
self::FORMAT_WEBP => 'image/webp'
];

Expand Down Expand Up @@ -182,6 +185,13 @@ public function outputWebpFormat(): self
return $this;
}

public function outputHeicFormat(): self
{
$this->setOutputFormat(self::FORMAT_HEIC);

return $this;
}

/**
* @return callable
*/
Expand Down
1 change: 1 addition & 0 deletions src/ImageManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ private function imageToStream(
ImageManipulation::FORMAT_GIF => $image->toGif(),
ImageManipulation::FORMAT_WEBP => $image->toBitmap(),
ImageManipulation::FORMAT_TIFF => $image->toTiff($outputQuality),
ImageManipulation::FORMAT_HEIC => $image->toHeic($outputQuality),
default => throw ImageManipulationException::unknownOutputFormat(),
};
return Utils::streamFor($formatted->toFilePointer());
Expand Down
2 changes: 2 additions & 0 deletions tests/Integration/ImageManipulationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public function test_can_get_set_output_format(): void
$this->assertEquals('webp', $manipulation->getOutputFormat());
$manipulation->outputJpegFormat();
$this->assertEquals('jpg', $manipulation->getOutputFormat());
$manipulation->outputHeicFormat();
$this->assertEquals('heic', $manipulation->getOutputFormat());
}

public function test_can_get_set_before_save_callback(): void
Expand Down
15 changes: 12 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Faker\Factory;
use GuzzleHttp\Psr7\Utils;
use Illuminate\Filesystem\Filesystem;
use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\ImageManager;
use Orchestra\Testbench\TestCase as BaseTestCase;
use PHPUnit\Framework\MockObject\MockObject;
Expand Down Expand Up @@ -105,8 +104,18 @@ protected function getEnvironmentSetUp($app)

$app['config']->set('mediable.image_optimization.enabled', false);

if (class_exists(Driver::class)) {
$app->instance(ImageManager::class, new ImageManager(new Driver()));
if (class_exists(\Intervention\Image\Drivers\Imagick\Driver::class)
&& class_exists('Imagick')
) {
$app->instance(
ImageManager::class,
new ImageManager(new \Intervention\Image\Drivers\Imagick\Driver())
);
} elseif (class_exists(\Intervention\Image\Drivers\Gd\Driver::class)) {
$app->instance(
ImageManager::class,
new ImageManager(new \Intervention\Image\Drivers\Gd\Driver())
);
}
}

Expand Down

0 comments on commit b094b69

Please sign in to comment.