Skip to content

Commit

Permalink
Merge pull request #368 from plank/heic
Browse files Browse the repository at this point in the history
add support for HEIC
  • Loading branch information
frasmage authored Dec 5, 2024
2 parents 0adf971 + 9425109 commit 6fb3d0a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 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
4 changes: 3 additions & 1 deletion docs/source/variants.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,16 @@ The ImageManipulation class also offers a fluent interface for defining how the
$manipulation->outputTiffFormat();
$manipulation->outputBmpFormat();
$manipulation->outputWebpFormat();
$manipulation->outputHeicFormat();
$manipulation->setOutputFormat($format);

If outputting to JPEG format, it is also possible to set the desired level of lossy compression, from 0 (low quality, smaller file size) to 100 (high quality, larger file size). Defaults to 90. This value is ignored by other formats.
If outputting to JPEG or HEIC format, it is also possible to set the desired level of lossy compression, from 0 (low quality, smaller file size) to 100 (high quality, larger file size). Defaults to 90. This value is ignored by other formats.

::

<?php
$manipulation->outputJpegFormat()->setOutputQuality(50);
$manipulation->outputHeicFormat()->setOutputQuality(50);


.. note::
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 6fb3d0a

Please sign in to comment.