Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"laravel/prompts": "0.*",
"league/commonmark": "^2.4",
"masterminds/html5": "^2.8",
"spatie/image-optimizer": "^1.6",
"spatie/laravel-image-optimizer": "^1.8",
"symfony/html-sanitizer": "^7.3",
"tightenco/ziggy": "^2.0"
},
Expand Down
2 changes: 1 addition & 1 deletion demo/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"laravel/tinker": "^3.0",
"masterminds/html5": "^2.9",
"pragmarx/google2fa": "^8.0",
"spatie/image-optimizer": "^1.7",
"spatie/laravel-image-optimizer": "^1.8",
"spatie/laravel-passkeys": "^1.6",
"spatie/laravel-translatable": "^6.13",
"symfony/html-sanitizer": "^7.3",
Expand Down
94 changes: 81 additions & 13 deletions demo/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions demo/config/image-optimizer.php
Original file line number Diff line number Diff line change
@@ -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' => env('IMAGE_OPTIMIZER_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' => true,
];
14 changes: 6 additions & 8 deletions src/Http/Jobs/HandleUploadedFileJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Storage;
use Spatie\ImageOptimizer\OptimizerChainFactory;

class HandleUploadedFileJob implements ShouldQueue
{
Expand Down Expand Up @@ -36,26 +35,25 @@ public function handle(): void
);

if ($this->shouldOptimizeImage) {
// We do not need to check for exception nor file format because
// the package will not throw any errors and just operate silently.
app(OptimizerChainFactory::class)
->create()
->optimize(Storage::disk($tmpDisk)->path($tmpFilePath));
OptimizeImageJob::dispatchSync(
disk: $tmpDisk,
filePath: $tmpFilePath,
);
}

if ($this->transformFilters) {
// There are transformation and field was configured to handle transformation on the source image
HandleTransformedFileJob::dispatchSync(
disk: $tmpDisk,
filePath: $tmpFilePath,
transformFilters: $this->transformFilters
transformFilters: $this->transformFilters,
);
}

if ($this->shouldSanitizeSvg && Storage::disk($tmpDisk)->mimeType($tmpFilePath) === 'image/svg+xml') {
SanitizeSvgJob::dispatchSync(
disk: $tmpDisk,
filePath: $tmpFilePath
filePath: $tmpFilePath,
);
}

Expand Down
56 changes: 56 additions & 0 deletions src/Http/Jobs/OptimizeImageJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Code16\Sharp\Http\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Storage;
use Spatie\ImageOptimizer\OptimizerChain;
use Spatie\ImageOptimizer\Optimizers\Avifenc;
use Spatie\ImageOptimizer\Optimizers\Jpegoptim;
use Spatie\ImageOptimizer\Optimizers\Pngquant;

class OptimizeImageJob
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;

public function __construct(
public string $disk,
public string $filePath,
) {}

public function handle(): void
{
// We do not need to check for exception nor file format because
// the package will not throw any errors and just operate silently.
$chain = app(OptimizerChain::class);

if ($jpegOptim = collect($chain->getOptimizers())->whereInstanceOf(Jpegoptim::class)->first()) {
$jpegOptim->options[] = '--keep-exif';
}

if ($pngquant = collect($chain->getOptimizers())->whereInstanceOf(Pngquant::class)->first()) {
if (! collect($pngquant->options)->some(fn ($option) => str_starts_with($option, '--quality'))) {
$pngquant->options[] = '--quality=85';
}
}

if (! collect($chain->getOptimizers())->whereInstanceOf(Avifenc::class)->first()) {
$chain->addOptimizer(new Avifenc([
'-a cq-level=23',
'-j all',
'--min 0',
'--max 63',
'--minalpha 0',
'--maxalpha 63',
'-a end-usage=q',
'-a tune=ssim',
]));
}

$chain->optimize(Storage::disk($this->disk)->path($this->filePath));
}
}
Loading
Loading