Skip to content

Commit

Permalink
Merge branch 'release/2.6.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
torralbodavid committed Oct 22, 2024
2 parents 074b231 + ed3aba9 commit a9faa6d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `image-cacher` will be documented in this file

## 2.6.0

- Improve JPEG and WebP optimization during image cropping [43b964bf](https://github.com/gnahotelsolutions/image-cacher/commit/43b964bf48a245bdf2a17a4b77835312ccbbbcd8)

## 2.5.0

- Fix Laravel singleton declaration [ac7f1e74](https://github.com/gnahotelsolutions/image-cacher/commit/ac7f1e744340280a58482f7d0d608b1490f718b3)
Expand Down
14 changes: 7 additions & 7 deletions src/Adapters/Laravel/ImageCacherServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ class ImageCacherServiceProvider extends ServiceProvider
{
public function register()
{
$this->mergeConfigFrom(__DIR__.'/config/image-cacher.php', 'image-cacher');
$this->mergeConfigFrom(__DIR__ . '/config/image-cacher.php', 'image-cacher');

$this->app->singleton(Cacher::class, function (Application $app, array $params) {
return new Cacher(
$params['cache_path'] ?? config('image-cacher.cache_path'),
$params['cache_root_path'] ?? config('image-cacher.cache_root_path'),
$params['images_root_path'] ?? config('image-cacher.images_root_path'),
$params['output_format'] ?? config('image-cacher.output_format'),
$params['quality'] ?? config('image-cacher.quality', 80)
$params['cache_path'] ?? config('image-cacher.cache_path'),
$params['cache_root_path'] ?? config('image-cacher.cache_root_path'),
$params['images_root_path'] ?? config('image-cacher.images_root_path'),
$params['quality'] ?? config('image-cacher.quality', 80),
$params['output_format'] ?? null,
);
});
}

public function boot()
{
$this->publishes([__DIR__.'/config/image-cacher.php' => config_path('image-cacher.php')]);
$this->publishes([__DIR__ . '/config/image-cacher.php' => config_path('image-cacher.php')]);
}
}
6 changes: 3 additions & 3 deletions src/Cacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public function __construct(
string $cachePath = 'cache/images',
string $cacheRootPath = '',
string $imagesRootPath = '',
?string $outputFormat = null,
int $quality = 80
int $quality = 80,
?string $outputFormat = null
) {
$this->cachePath = $cachePath;
$this->cacheRootPath = rtrim($cacheRootPath, '/');
$this->imagesRootPath = rtrim($imagesRootPath, '/');
$this->outputFormat = $outputFormat;
$this->quality = $quality;
$this->outputFormat = $outputFormat;
}

public function setOutputFormat(string $format): self
Expand Down
29 changes: 24 additions & 5 deletions src/Manipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace GNAHotelSolutions\ImageCacher;

use Exception;

class Manipulator
{
public static function create(string $format, string $path)
Expand All @@ -22,13 +24,21 @@ public static function create(string $format, string $path)
return imagecreatefromwebp($path);
}

throw new \Exception("Image type [{$format}] not supported.");
throw new Exception("Image type [{$format}] not supported.");
}

public static function save(string $format, $layout, string $name, int $quality = 80): string
{
if ($format === Format::JPEG) {
return imagejpeg($layout, $name);
if (self::isJpeg($format, $name)) {
$image = imagejpeg($layout, $name);

exec("jpegoptim --max=$quality --strip-all --all-progressive --force $name", $output, $resultCode);

if ($resultCode !== 0) {
throw new Exception("Error optimizing image.");
}

return $image;
}

if ($format === Format::PNG) {
Expand All @@ -42,11 +52,20 @@ public static function save(string $format, $layout, string $name, int $quality
if ($format === Format::WEBP) {
$image = imagewebp($layout, $name);

exec("cwebp -m 6 -pass 10 -mt -q $quality $name -o $name");
exec("cwebp -m 6 -pass 10 -jpeg_like -mt -q $quality $name -o $name", $output, $resultCode);

if ($resultCode !== 0) {
throw new Exception('Error optimizing image');
}

return $image;
}

throw new \Exception("Image type [$format] not supported.");
throw new Exception('Image type [$format] not supported.');
}

protected static function isJpeg(string $format, string $name): bool
{
return $format === Format::JPEG || in_array(pathinfo($name, PATHINFO_EXTENSION), ['jpg', 'jpeg']);
}
}
18 changes: 8 additions & 10 deletions tests/Adapters/Laravel/ImageCacherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ public function can_generate_image_using_facade()
*/
public function can_transform_images_to_webp_by_default()
{
config(['image-cacher' => [
$cacher = app()->make(Cacher::class, [
'cache_path' => 'cache',
'cache_root_path' => __DIR__.'/../../fixtures',
'images_root_path' => __DIR__.'/../../fixtures/images',
'output_format' => Format::WEBP
]]);
]);

$resized = ImageCacher::crop('office/meetings_room/plant.jpg', 300, 300);
$resized = $cacher->crop('office/meetings_room/plant.jpg', 300, 300);

$this->assertFileExists(__DIR__.'/../../fixtures/cache/office/meetings_room/300x300/plant.webp');

Expand All @@ -77,21 +77,19 @@ public function can_transform_images_to_webp_by_default()
*/
public function can_update_webp_quality_image_to_100()
{
config(['image-cacher' => [
$cacher = app()->make(Cacher::class, [
'cache_path' => 'cache',
'cache_root_path' => __DIR__.'/../../fixtures',
'images_root_path' => __DIR__.'/../../fixtures/images',
'output_format' => Format::WEBP,
'quality' => 100
]]);
'quality' => 100,
'output_format' => Format::WEBP
]);

$resized = ImageCacher::crop('office/meetings_room/plant.jpg', 300, 300);
$resized = $cacher->crop('office/meetings_room/plant.jpg', 300, 300);

$this->assertFileExists(__DIR__.'/../../fixtures/cache/office/meetings_room/300x300/plant.webp');

$this->assertInstanceOf(Image::class, $resized);

$this->assertEquals(100, config('image-cacher.quality'));
}

public function singleton_is_working()
Expand Down

0 comments on commit a9faa6d

Please sign in to comment.