Add image optimization on save

This commit is contained in:
DariusIII
2023-10-03 22:16:14 +02:00
parent a1066271c8
commit 7a97d0a0f4
4 changed files with 196 additions and 1 deletions
+5
View File
@@ -8,6 +8,7 @@ use Intervention\Image\Exception\NotFoundException;
use Intervention\Image\Exception\NotReadableException;
use Intervention\Image\Exception\NotWritableException;
use Intervention\Image\Facades\Image;
use Spatie\LaravelImageOptimizer\Facades\ImageOptimizer;
/**
* Resize/save/delete images to disk.
@@ -120,6 +121,8 @@ class ReleaseImage
if ($saveThumb) {
$cover->save($imgSavePath.$imgName.'_thumb.jpg');
//Optimize the thumbnail.
ImageOptimizer::optimize($imgSavePath.$imgName.'_thumb.jpg');
}
}
}
@@ -127,6 +130,8 @@ class ReleaseImage
$coverPath = $imgSavePath.$imgName.'.jpg';
try {
$cover->save($coverPath);
//Optimize the image.
ImageOptimizer::optimize($coverPath);
} catch (NotWritableException $e) {
return 0;
}
+1
View File
@@ -103,6 +103,7 @@
"spatie/laravel-directory-cleanup": "^1.9",
"spatie/laravel-fractal": "^6.0",
"spatie/laravel-ignition": "^2.3",
"spatie/laravel-image-optimizer": "^1.7",
"spatie/laravel-permission": "^5.11",
"sspat/es-query-sanitizer": "^1.0",
"stechstudio/laravel-zipstream": "^4.13",
Generated
+124 -1
View File
@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "2465c8d28001088880be49e75e818a4e",
"content-hash": "48256280350122730fa6375500eae71d",
"packages": [
{
"name": "aharen/omdbapi",
@@ -9590,6 +9590,61 @@
],
"time": "2023-09-19T15:29:52+00:00"
},
{
"name": "spatie/image-optimizer",
"version": "1.7.1",
"source": {
"type": "git",
"url": "https://github.com/spatie/image-optimizer.git",
"reference": "af179994e2d2413e4b3ba2d348d06b4eaddbeb30"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/image-optimizer/zipball/af179994e2d2413e4b3ba2d348d06b4eaddbeb30",
"reference": "af179994e2d2413e4b3ba2d348d06b4eaddbeb30",
"shasum": ""
},
"require": {
"ext-fileinfo": "*",
"php": "^7.3|^8.0",
"psr/log": "^1.0 | ^2.0 | ^3.0",
"symfony/process": "^4.2|^5.0|^6.0"
},
"require-dev": {
"pestphp/pest": "^1.21",
"phpunit/phpunit": "^8.5.21|^9.4.4",
"symfony/var-dumper": "^4.2|^5.0|^6.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Spatie\\ImageOptimizer\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Freek Van der Herten",
"email": "freek@spatie.be",
"homepage": "https://spatie.be",
"role": "Developer"
}
],
"description": "Easily optimize images using PHP",
"homepage": "https://github.com/spatie/image-optimizer",
"keywords": [
"image-optimizer",
"spatie"
],
"support": {
"issues": "https://github.com/spatie/image-optimizer/issues",
"source": "https://github.com/spatie/image-optimizer/tree/1.7.1"
},
"time": "2023-07-27T07:57:32+00:00"
},
{
"name": "spatie/laravel-backup",
"version": "8.3.4",
@@ -9935,6 +9990,74 @@
],
"time": "2023-08-23T06:24:34+00:00"
},
{
"name": "spatie/laravel-image-optimizer",
"version": "1.7.1",
"source": {
"type": "git",
"url": "https://github.com/spatie/laravel-image-optimizer.git",
"reference": "cd8945e47b9fd01bc7b770eecd57c56f46c47422"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/laravel-image-optimizer/zipball/cd8945e47b9fd01bc7b770eecd57c56f46c47422",
"reference": "cd8945e47b9fd01bc7b770eecd57c56f46c47422",
"shasum": ""
},
"require": {
"laravel/framework": "^8.0|^9.0|^10.0",
"php": "^8.0",
"spatie/image-optimizer": "^1.2.0"
},
"require-dev": {
"orchestra/testbench": "^6.23|^7.0|^8.0",
"phpunit/phpunit": "^9.4"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Spatie\\LaravelImageOptimizer\\ImageOptimizerServiceProvider"
],
"aliases": {
"ImageOptimizer": "Spatie\\LaravelImageOptimizer\\Facades\\ImageOptimizer"
}
}
},
"autoload": {
"psr-4": {
"Spatie\\LaravelImageOptimizer\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Freek Van der Herten",
"email": "freek@spatie.be",
"homepage": "https://spatie.be",
"role": "Developer"
}
],
"description": "Optimize images in your Laravel app",
"homepage": "https://github.com/spatie/laravel-image-optimizer",
"keywords": [
"laravel-image-optimizer",
"spatie"
],
"support": {
"source": "https://github.com/spatie/laravel-image-optimizer/tree/1.7.1"
},
"funding": [
{
"url": "https://spatie.be/open-source/support-us",
"type": "custom"
}
],
"time": "2023-01-24T23:44:33+00:00"
},
{
"name": "spatie/laravel-package-tools",
"version": "1.16.1",
+66
View File
@@ -0,0 +1,66 @@
<?php
use Spatie\ImageOptimizer\Optimizers\Cwebp;
use Spatie\ImageOptimizer\Optimizers\Gifsicle;
use Spatie\ImageOptimizer\Optimizers\Jpegoptim;
use Spatie\ImageOptimizer\Optimizers\Optipng;
use Spatie\ImageOptimizer\Optimizers\Pngquant;
use Spatie\ImageOptimizer\Optimizers\Svgo;
return [
/*
* When calling `optimize` the package will automatically determine which optimizers
* should run for the given image.
*/
'optimizers' => [
Jpegoptim::class => [
'-m85', // set maximum quality to 85%
'--strip-all', // this strips out all text information such as comments and EXIF data
'--all-progressive', // this will make sure the resulting image is a progressive one
],
Pngquant::class => [
'--force', // required parameter for this package
],
Optipng::class => [
'-i0', // this will result in a non-interlaced, progressive scanned image
'-o2', // this set the optimization level to two (multiple IDAT compression trials)
'-quiet', // required parameter for this package
],
Svgo::class => [
'--disable=cleanupIDs', // disabling because it is know to cause troubles
],
Gifsicle::class => [
'-b', // required parameter for this package
'-O3', // this produces the slowest but best results
],
Cwebp::class => [
'-m 6', // for the slowest compression method in order to get the best compression.
'-pass 10', // for maximizing the amount of analysis pass.
'-mt', // multithreading for some speed improvements.
'-q 90', // quality factor that brings the least noticeable changes.
],
],
/*
* The directory where your binaries are stored.
* Only use this when you binaries are not accessible in the global environment.
*/
'binary_path' => '',
/*
* The maximum time in seconds each optimizer is allowed to run separately.
*/
'timeout' => 60,
/*
* If set to `true` all output of the optimizer binaries will be appended to the default log.
* You can also set this to a class that implements `Psr\Log\LoggerInterface`.
*/
'log_optimizer_activity' => false,
];