├── .github ├── FUNDING.yml ├── dependabot.yml └── workflows │ ├── dependabot-auto-merge.yml │ ├── php-cs-fixer.yml │ ├── run-tests.yml │ └── update-changelog.yml ├── .php_cs.dist.php ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json ├── config └── image-optimizer.php ├── phpunit.xml.dist.bak └── src ├── Exceptions └── InvalidConfiguration.php ├── Facades └── ImageOptimizer.php ├── ImageOptimizerServiceProvider.php ├── Middlewares └── OptimizeImages.php └── OptimizerChainFactory.php /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | custom: https://spatie.be/open-source/support-us 2 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | 3 | updates: 4 | 5 | - package-ecosystem: "github-actions" 6 | directory: "/" 7 | schedule: 8 | interval: "weekly" 9 | -------------------------------------------------------------------------------- /.github/workflows/dependabot-auto-merge.yml: -------------------------------------------------------------------------------- 1 | name: dependabot-auto-merge 2 | on: pull_request_target 3 | 4 | permissions: 5 | pull-requests: write 6 | contents: write 7 | 8 | jobs: 9 | dependabot: 10 | runs-on: ubuntu-latest 11 | if: ${{ github.actor == 'dependabot[bot]' }} 12 | steps: 13 | 14 | - name: Dependabot metadata 15 | id: metadata 16 | uses: dependabot/fetch-metadata@v2.4.0 17 | with: 18 | github-token: "${{ secrets.GITHUB_TOKEN }}" 19 | compat-lookup: true 20 | 21 | - name: Auto-merge Dependabot PRs for semver-minor updates 22 | if: ${{steps.metadata.outputs.update-type == 'version-update:semver-minor'}} 23 | run: gh pr merge --auto --merge "$PR_URL" 24 | env: 25 | PR_URL: ${{github.event.pull_request.html_url}} 26 | GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} 27 | 28 | - name: Auto-merge Dependabot PRs for semver-patch updates 29 | if: ${{steps.metadata.outputs.update-type == 'version-update:semver-patch'}} 30 | run: gh pr merge --auto --merge "$PR_URL" 31 | env: 32 | PR_URL: ${{github.event.pull_request.html_url}} 33 | GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} 34 | 35 | - name: Auto-merge Dependabot PRs for Action major versions when compatibility is higher than 90% 36 | if: ${{steps.metadata.outputs.package-ecosystem == 'github_actions' && steps.metadata.outputs.update-type == 'version-update:semver-major' && steps.metadata.outputs.compatibility-score >= 90}} 37 | run: gh pr merge --auto --merge "$PR_URL" 38 | env: 39 | PR_URL: ${{github.event.pull_request.html_url}} 40 | GH_TOKEN: ${{secrets.GITHUB_TOKEN}} 41 | -------------------------------------------------------------------------------- /.github/workflows/php-cs-fixer.yml: -------------------------------------------------------------------------------- 1 | name: Check & fix styling 2 | 3 | on: [push] 4 | 5 | jobs: 6 | php-cs-fixer: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - name: Checkout code 11 | uses: actions/checkout@v4 12 | with: 13 | ref: ${{ github.head_ref }} 14 | 15 | - name: Run PHP CS Fixer 16 | uses: docker://oskarstark/php-cs-fixer-ga 17 | with: 18 | args: --config=.php_cs.dist.php --allow-risky=yes 19 | 20 | - name: Commit changes 21 | uses: stefanzweifel/git-auto-commit-action@v4 22 | with: 23 | commit_message: Fix styling 24 | -------------------------------------------------------------------------------- /.github/workflows/run-tests.yml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | 3 | on: 4 | - push 5 | - pull_request 6 | 7 | jobs: 8 | test: 9 | runs-on: ${{ matrix.os }} 10 | 11 | strategy: 12 | fail-fast: true 13 | matrix: 14 | os: [ubuntu-latest] 15 | php: [8.4, 8.3, 8.2, 8.1, 8.0] 16 | laravel: ['8.*', '9.*', '10.*', '11.*', '12.*'] 17 | dependency-version: [prefer-lowest, prefer-stable] 18 | include: 19 | - laravel: 10.* 20 | testbench: 8.* 21 | - laravel: 9.* 22 | testbench: 7.* 23 | - laravel: 8.* 24 | testbench: ^6.23 25 | - laravel: 11.* 26 | testbench: 9.* 27 | - laravel: 12.* 28 | testbench: 10.* 29 | exclude: 30 | - laravel: 10.* 31 | php: 8.0 32 | - laravel: 11.* 33 | php: 8.1 34 | - laravel: 11.* 35 | php: 8.0 36 | - laravel: 12.* 37 | php: 8.1 38 | - laravel: 12.* 39 | php: 8.0 40 | 41 | name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} 42 | 43 | steps: 44 | - uses: actions/setup-node@v4 45 | with: 46 | node-version: 10 47 | 48 | - name: Install tools 49 | run: sudo apt-get install jpegoptim && sudo apt-get install pngquant && sudo apt-get install gifsicle && sudo apt-get install optipng && sudo apt-get install libjpeg-progs && npm install -g svgo 50 | 51 | - name: Checkout code 52 | uses: actions/checkout@v4 53 | 54 | - name: Setup PHP 55 | uses: shivammathur/setup-php@v2 56 | with: 57 | php-version: ${{ matrix.php }} 58 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick 59 | coverage: none 60 | 61 | - name: Install dependencies 62 | run: | 63 | composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update 64 | composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest 65 | 66 | - name: Execute tests 67 | run: vendor/bin/phpunit 68 | -------------------------------------------------------------------------------- /.github/workflows/update-changelog.yml: -------------------------------------------------------------------------------- 1 | name: "Update Changelog" 2 | 3 | on: 4 | release: 5 | types: [released] 6 | 7 | jobs: 8 | update: 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - name: Checkout code 13 | uses: actions/checkout@v4 14 | with: 15 | ref: main 16 | 17 | - name: Update Changelog 18 | uses: stefanzweifel/changelog-updater-action@v1 19 | with: 20 | latest-version: ${{ github.event.release.name }} 21 | release-notes: ${{ github.event.release.body }} 22 | 23 | - name: Commit updated CHANGELOG 24 | uses: stefanzweifel/git-auto-commit-action@v4 25 | with: 26 | branch: main 27 | commit_message: Update CHANGELOG 28 | file_pattern: CHANGELOG.md 29 | -------------------------------------------------------------------------------- /.php_cs.dist.php: -------------------------------------------------------------------------------- 1 | in([ 5 | __DIR__ . '/src', 6 | __DIR__ . '/tests', 7 | ]) 8 | ->name('*.php') 9 | ->notName('*.blade.php') 10 | ->ignoreDotFiles(true) 11 | ->ignoreVCS(true); 12 | 13 | return (new PhpCsFixer\Config()) 14 | ->setRules([ 15 | '@PSR12' => true, 16 | 'array_syntax' => ['syntax' => 'short'], 17 | 'ordered_imports' => ['sort_algorithm' => 'alpha'], 18 | 'no_unused_imports' => true, 19 | 'not_operator_with_successor_space' => true, 20 | 'trailing_comma_in_multiline' => true, 21 | 'phpdoc_scalar' => true, 22 | 'unary_operator_spaces' => true, 23 | 'binary_operator_spaces' => true, 24 | 'blank_line_before_statement' => [ 25 | 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], 26 | ], 27 | 'phpdoc_single_line_var_spacing' => true, 28 | 'phpdoc_var_without_name' => true, 29 | 'class_attributes_separation' => [ 30 | 'elements' => [ 31 | 'method' => 'one', 32 | ], 33 | ], 34 | 'method_argument_space' => [ 35 | 'on_multiline' => 'ensure_fully_multiline', 36 | 'keep_multiple_spaces_after_comma' => true, 37 | ], 38 | 'single_trait_insert_per_statement' => true, 39 | ]) 40 | ->setFinder($finder); 41 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-image-optimizer` will be documented in this file 4 | 5 | ## 1.8.2 - 2025-02-21 6 | 7 | ### What's Changed 8 | 9 | * Laravel 12.x Compatibility by @laravel-shift in https://github.com/spatie/laravel-image-optimizer/pull/131 10 | 11 | **Full Changelog**: https://github.com/spatie/laravel-image-optimizer/compare/1.8.1...1.8.2 12 | 13 | ## 1.8.1 - 2025-02-21 14 | 15 | ### What's Changed 16 | 17 | * Update README.md by @JadoJodo in https://github.com/spatie/laravel-image-optimizer/pull/127 18 | * Bump dependabot/fetch-metadata from 1.6.0 to 2.1.0 by @dependabot in https://github.com/spatie/laravel-image-optimizer/pull/126 19 | * Bump dependabot/fetch-metadata from 2.1.0 to 2.2.0 by @dependabot in https://github.com/spatie/laravel-image-optimizer/pull/129 20 | * Bump dependabot/fetch-metadata from 2.2.0 to 2.3.0 by @dependabot in https://github.com/spatie/laravel-image-optimizer/pull/130 21 | 22 | ### New Contributors 23 | 24 | * @JadoJodo made their first contribution in https://github.com/spatie/laravel-image-optimizer/pull/127 25 | 26 | **Full Changelog**: https://github.com/spatie/laravel-image-optimizer/compare/1.8.0...1.8.1 27 | 28 | ## 1.8.0 - 2024-02-29 29 | 30 | ### What's Changed 31 | 32 | * Bump dependabot/fetch-metadata from 1.3.5 to 1.3.6 by @dependabot in https://github.com/spatie/laravel-image-optimizer/pull/113 33 | * Fix `Tests` badge status in README.md by @gomzyakov in https://github.com/spatie/laravel-image-optimizer/pull/115 34 | * Bump dependabot/fetch-metadata from 1.3.6 to 1.4.0 by @dependabot in https://github.com/spatie/laravel-image-optimizer/pull/116 35 | * Change $routeMiddleware to $middlewareAliases by @HassanZahirnia in https://github.com/spatie/laravel-image-optimizer/pull/117 36 | * Bump dependabot/fetch-metadata from 1.4.0 to 1.5.1 by @dependabot in https://github.com/spatie/laravel-image-optimizer/pull/118 37 | * Bump dependabot/fetch-metadata from 1.5.1 to 1.6.0 by @dependabot in https://github.com/spatie/laravel-image-optimizer/pull/119 38 | * Bump actions/checkout from 3 to 4 by @dependabot in https://github.com/spatie/laravel-image-optimizer/pull/120 39 | * Bump actions/setup-node from 3 to 4 by @dependabot in https://github.com/spatie/laravel-image-optimizer/pull/122 40 | * Laravel 11.x Compatibility by @laravel-shift in https://github.com/spatie/laravel-image-optimizer/pull/124 41 | 42 | ### New Contributors 43 | 44 | * @gomzyakov made their first contribution in https://github.com/spatie/laravel-image-optimizer/pull/115 45 | * @HassanZahirnia made their first contribution in https://github.com/spatie/laravel-image-optimizer/pull/117 46 | 47 | **Full Changelog**: https://github.com/spatie/laravel-image-optimizer/compare/1.7.1...1.8.0 48 | 49 | ## 1.7.1 - 2023-01-24 50 | 51 | ### What's Changed 52 | 53 | - Add Dependabot Automation by @patinthehat in https://github.com/spatie/laravel-image-optimizer/pull/108 54 | - Bump actions/setup-node from 1 to 3 by @dependabot in https://github.com/spatie/laravel-image-optimizer/pull/109 55 | - Add PHP 8.2 Support by @patinthehat in https://github.com/spatie/laravel-image-optimizer/pull/110 56 | - Bump actions/checkout from 2 to 3 by @dependabot in https://github.com/spatie/laravel-image-optimizer/pull/111 57 | - Laravel 10.x Compatibility by @laravel-shift in https://github.com/spatie/laravel-image-optimizer/pull/112 58 | 59 | ### New Contributors 60 | 61 | - @patinthehat made their first contribution in https://github.com/spatie/laravel-image-optimizer/pull/108 62 | - @dependabot made their first contribution in https://github.com/spatie/laravel-image-optimizer/pull/109 63 | - @laravel-shift made their first contribution in https://github.com/spatie/laravel-image-optimizer/pull/112 64 | 65 | **Full Changelog**: https://github.com/spatie/laravel-image-optimizer/compare/1.7.0...1.7.1 66 | 67 | ## 1.7.0 - 2022-01-14 68 | 69 | - support Laravel 9 70 | 71 | ## 1.6.4 - 2020-11-27 72 | 73 | - add support for PHP 8 74 | 75 | ## 1.6.3 - 2020-09-08 76 | 77 | - add support for Laravel 8 78 | 79 | ## 1.6.2 - 2020-07-14 80 | 81 | - bugfix for custom logger (#87) 82 | 83 | ## 1.6.1 - 2020-03-10 84 | 85 | - fix deps 86 | 87 | ## 1.6.0 - 2020-03-02 88 | 89 | - add support for L7 90 | 91 | ## 1.5.1 - 2019-11-02 92 | 93 | - only optimize files that were uploaded without errors (#75) 94 | 95 | ## 1.5.0 - 2019-09-04 96 | 97 | - make compatible with Laravel 6 98 | 99 | ## 1.4.0 - 2019-02-27 100 | 101 | - drop support for PHP 7.1 102 | 103 | ## 1.3.3 - 2019-02-27 104 | 105 | - drop support for PHP 7.0 106 | - add support for Laravel 5.8 107 | 108 | ## 1.3.2 - 2018-08-27 109 | 110 | - add support for Laravel 5.7 111 | 112 | ## 1.3.1 - 2018-06-05 113 | 114 | - make sure a compatible version of spatie/image-optimizer is being pulled in 115 | 116 | ## 1.3.0 - 2018-06-05 117 | 118 | - add compatibility with Lumen 119 | 120 | ## 1.2.0 - 2018-05-16 121 | 122 | - add `binary_path` to config file 123 | 124 | ## 1.1.3 - 2018-02-08 125 | 126 | - add support for L5.6 127 | - drop support for L5.4 128 | 129 | ## 1.1.2 - 2017-12-11 130 | 131 | - fix issue with middleware and images in multidimensional input arrays 132 | - fix issue where jpegs would not be optimized correctly 133 | 134 | ## 1.1.1 135 | 136 | - fix for multipe file uploads 137 | 138 | ## 1.1.0 139 | 140 | - add support for Laravel 5.5 141 | 142 | ## 1.0.1 - 2017-07-22 143 | 144 | - fix initial package autodiscovery support 145 | 146 | ## 1.0.0 - 2017-07-13 147 | 148 | - initial release 149 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) Spatie bvba 4 | 5 | > Permission is hereby granted, free of charge, to any person obtaining a copy 6 | > of this software and associated documentation files (the "Software"), to deal 7 | > in the Software without restriction, including without limitation the rights 8 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | > copies of the Software, and to permit persons to whom the Software is 10 | > furnished to do so, subject to the following conditions: 11 | > 12 | > The above copyright notice and this permission notice shall be included in 13 | > all copies or substantial portions of the Software. 14 | > 15 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Optimize images in your Laravel app 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-image-optimizer.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-image-optimizer) 4 | ![Tests](https://github.com/spatie/laravel-image-optimizer/actions/workflows/run-tests.yml/badge.svg) 5 | [![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-image-optimizer.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-image-optimizer) 6 | 7 | This package is the Laravel 6.0 and up specific integration of [spatie/image-optimizer](https://github.com/spatie/image-optimizer). It can optimize PNGs, JPGs, SVGs and GIFs by running them through a chain of various [image optimization tools](https://github.com/spatie/image-optimizer#optimization-tools). The package will automatically detect which optimization binaries are installed on your system and use them. 8 | 9 | Here's how you can use it: 10 | 11 | ```php 12 | use ImageOptimizer; 13 | 14 | // the image will be replaced with an optimized version which should be smaller 15 | ImageOptimizer::optimize($pathToImage); 16 | 17 | // if you use a second parameter the package will not modify the original 18 | ImageOptimizer::optimize($pathToImage, $pathToOptimizedImage); 19 | ``` 20 | 21 | You don't like facades you say? No problem! Just resolve a configured instance of `Spatie\ImageOptimizer\OptimizerChain` out of the container: 22 | 23 | ```php 24 | app(Spatie\ImageOptimizer\OptimizerChain::class)->optimize($pathToImage); 25 | ``` 26 | 27 | The package also contains [a middleware](https://github.com/spatie/laravel-image-optimizer/blob/master/README.md#using-the-middleware) to automatically optimize all images in an request. 28 | 29 | Don't use Laravel you say? No problem! Just use the underlying [spatie/image-optimizer](https://github.com/spatie/image-optimizer) directly. 30 | 31 | ## Support us 32 | 33 | [](https://spatie.be/github-ad-click/laravel-image-optimizer) 34 | 35 | We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). 36 | 37 | We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). 38 | 39 | ## Installation 40 | 41 | You can install the package via composer: 42 | 43 | ```bash 44 | composer require spatie/laravel-image-optimizer 45 | ``` 46 | 47 | The package will automatically register itself. 48 | 49 | The package uses a bunch of binaries to optimize images. To learn which ones on how to install them, head over to the [optimization tools section](https://github.com/spatie/image-optimizer#optimization-tools) in the readme of the underlying image-optimizer package. That readme also contains info on [what these tools will do to your images](https://github.com/spatie/image-optimizer#which-tools-will-do-what). 50 | 51 | The package comes with some sane defaults to optimize images. You can modify that configuration by publishing the config file. 52 | 53 | ```bash 54 | php artisan vendor:publish --provider="Spatie\LaravelImageOptimizer\ImageOptimizerServiceProvider" 55 | ``` 56 | 57 | This is the contents of the `config/image-optimizer` file that will be published: 58 | 59 | ```php 60 | use Spatie\ImageOptimizer\Optimizers\Svgo; 61 | use Spatie\ImageOptimizer\Optimizers\Optipng; 62 | use Spatie\ImageOptimizer\Optimizers\Gifsicle; 63 | use Spatie\ImageOptimizer\Optimizers\Pngquant; 64 | use Spatie\ImageOptimizer\Optimizers\Jpegoptim; 65 | use Spatie\ImageOptimizer\Optimizers\Cwebp; 66 | 67 | return [ 68 | /** 69 | * When calling `optimize` the package will automatically determine which optimizers 70 | * should run for the given image. 71 | */ 72 | 'optimizers' => [ 73 | 74 | Jpegoptim::class => [ 75 | '-m85', // set maximum quality to 85% 76 | '--strip-all', // this strips out all text information such as comments and EXIF data 77 | '--all-progressive' // this will make sure the resulting image is a progressive one 78 | ], 79 | 80 | Pngquant::class => [ 81 | '--force' // required parameter for this package 82 | ], 83 | 84 | Optipng::class => [ 85 | '-i0', // this will result in a non-interlaced, progressive scanned image 86 | '-o2', // this set the optimization level to two (multiple IDAT compression trials) 87 | '-quiet' // required parameter for this package 88 | ], 89 | 90 | Svgo::class => [ 91 | '--disable=cleanupIDs' // disabling because it is known to cause trouble 92 | ], 93 | 94 | Gifsicle::class => [ 95 | '-b', // required parameter for this package 96 | '-O3' // this produces the slowest but best results 97 | ], 98 | 99 | Cwebp::class => [ 100 | '-m 6', // for the slowest compression method in order to get the best compression. 101 | '-pass 10', // for maximizing the amount of analysis pass. 102 | '-mt', // multithreading for some speed improvements. 103 | '-q 90', //quality factor that brings the least noticeable changes. 104 | ], 105 | ], 106 | 107 | /** 108 | * The maximum time in seconds each optimizer is allowed to run separately. 109 | */ 110 | 'timeout' => 60, 111 | 112 | /** 113 | * If set to `true` all output of the optimizer binaries will be appended to the default log. 114 | * You can also set this to a class that implements `Psr\Log\LoggerInterface`. 115 | */ 116 | 'log_optimizer_activity' => false, 117 | ]; 118 | ``` 119 | 120 | If you want to automatically optimize images that get uploaded to your application add the `\Spatie\LaravelImageOptimizer\Middlewares\OptimizeImages::class` in the http kernel. 121 | 122 | ```php 123 | // app/Http/Kernel.php 124 | protected $middlewareAliases = [ 125 | ... 126 | 'optimizeImages' => \Spatie\LaravelImageOptimizer\Middlewares\OptimizeImages::class, 127 | ]; 128 | ``` 129 | 130 | ## Usage 131 | 132 | You can resolve a configured instance of `Spatie\ImageOptimizer\OptimizerChain` out of the container: 133 | 134 | ```php 135 | // the image will be replaced with an optimized version which should be smaller 136 | app(Spatie\ImageOptimizer\OptimizerChain::class)->optimize($pathToImage); 137 | 138 | // if you use a second parameter the package will not modify the original 139 | app(Spatie\ImageOptimizer\OptimizerChain::class)->optimize($pathToImage, $pathToOptimizedImage); 140 | ``` 141 | 142 | ### Using the facade 143 | 144 | ```php 145 | use ImageOptimizer; 146 | 147 | // the image will be replaced with an optimized version which should be smaller 148 | ImageOptimizer::optimize($pathToImage); 149 | 150 | // if you use a second parameter the package will not modify the original 151 | ImageOptimizer::optimize($pathToImage, $pathToOptimizedImage); 152 | ``` 153 | 154 | You don't like facades you say? No problem! Just resolve a configured instance of `Spatie\ImageOptimizer\OptimizerChain` out of the container: 155 | 156 | ```php 157 | app(Spatie\ImageOptimizer\OptimizerChain::class)->optimize($pathToImage); 158 | ``` 159 | 160 | ### Using the middleware 161 | 162 | All images that in requests to routes that use the `optimizeImages`-middleware will be optimized automatically. 163 | 164 | ```php 165 | Route::middleware('optimizeImages')->group(function () { 166 | // all images will be optimized automatically 167 | Route::post('upload-images', 'UploadController@index'); 168 | }); 169 | ``` 170 | 171 | ### Adding your own optimizers 172 | 173 | To learn how to create your own optimizer read the ["Writing custom optimizers" section](https://github.com/spatie/image-optimizer#writing-a-custom-optimizers) in the readme of the underlying [spatie/image-optimizer](https://github.com/spatie/image-optimizer#writing-a-custom-optimizers) package. 174 | 175 | You can add the fully qualified classname of your optimizer as a key in the `optimizers` array in the config file. 176 | 177 | ## Example conversions 178 | 179 | Here are some [example conversions](https://github.com/spatie/image-optimizer#example-conversions) that were made by the optimizer. 180 | 181 | ## Changelog 182 | 183 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 184 | 185 | ## Testing 186 | 187 | ``` bash 188 | composer test 189 | ``` 190 | 191 | ## Contributing 192 | 193 | Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details. 194 | 195 | ## Security 196 | 197 | If you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker. 198 | 199 | ## Postcardware 200 | 201 | You're free to use this package (it's [MIT-licensed](LICENSE.md)), but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. 202 | 203 | Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium. 204 | 205 | We publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards). 206 | 207 | ## Credits 208 | 209 | - [Freek Van der Herten](https://github.com/freekmurze) 210 | - [All Contributors](../../contributors) 211 | 212 | The idea of a middleware that optimizes all files in a request is taken from [approached/laravel-image-optimizer](https://github.com/approached/laravel-image-optimizer). 213 | 214 | ## License 215 | 216 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 217 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "spatie/laravel-image-optimizer", 3 | "description": "Optimize images in your Laravel app", 4 | "keywords": [ 5 | "spatie", 6 | "laravel-image-optimizer" 7 | ], 8 | "homepage": "https://github.com/spatie/laravel-image-optimizer", 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Freek Van der Herten", 13 | "email": "freek@spatie.be", 14 | "homepage": "https://spatie.be", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.0", 20 | "laravel/framework": "^8.0|^9.0|^10.0|^11.0|^12.0", 21 | "spatie/image-optimizer": "^1.2.0" 22 | }, 23 | "require-dev": { 24 | "orchestra/testbench": "^6.23|^7.0|^8.0|^9.0|^10.0", 25 | "phpunit/phpunit": "^9.4|^10.5|^11.5.3" 26 | }, 27 | "autoload": { 28 | "psr-4": { 29 | "Spatie\\LaravelImageOptimizer\\": "src" 30 | } 31 | }, 32 | "autoload-dev": { 33 | "psr-4": { 34 | "Spatie\\LaravelImageOptimizer\\Test\\": "tests" 35 | } 36 | }, 37 | "scripts": { 38 | "test": "vendor/bin/phpunit", 39 | "format": "vendor/bin/php-cs-fixer fix --allow-risky=yes" 40 | }, 41 | "config": { 42 | "sort-packages": true 43 | }, 44 | "extra": { 45 | "laravel": { 46 | "providers": [ 47 | "Spatie\\LaravelImageOptimizer\\ImageOptimizerServiceProvider" 48 | ], 49 | "aliases": { 50 | "ImageOptimizer": "Spatie\\LaravelImageOptimizer\\Facades\\ImageOptimizer" 51 | } 52 | } 53 | }, 54 | "minimum-stability": "dev", 55 | "prefer-stable": true 56 | } 57 | -------------------------------------------------------------------------------- /config/image-optimizer.php: -------------------------------------------------------------------------------- 1 | [ 16 | 17 | Jpegoptim::class => [ 18 | '-m85', // set maximum quality to 85% 19 | '--strip-all', // this strips out all text information such as comments and EXIF data 20 | '--all-progressive', // this will make sure the resulting image is a progressive one 21 | ], 22 | 23 | Pngquant::class => [ 24 | '--force', // required parameter for this package 25 | ], 26 | 27 | Optipng::class => [ 28 | '-i0', // this will result in a non-interlaced, progressive scanned image 29 | '-o2', // this set the optimization level to two (multiple IDAT compression trials) 30 | '-quiet', // required parameter for this package 31 | ], 32 | 33 | Svgo::class => [ 34 | '--disable=cleanupIDs', // disabling because it is know to cause troubles 35 | ], 36 | 37 | Gifsicle::class => [ 38 | '-b', // required parameter for this package 39 | '-O3', // this produces the slowest but best results 40 | ], 41 | 42 | Cwebp::class => [ 43 | '-m 6', // for the slowest compression method in order to get the best compression. 44 | '-pass 10', // for maximizing the amount of analysis pass. 45 | '-mt', // multithreading for some speed improvements. 46 | '-q 90', // quality factor that brings the least noticeable changes. 47 | ], 48 | ], 49 | 50 | /* 51 | * The directory where your binaries are stored. 52 | * Only use this when you binaries are not accessible in the global environment. 53 | */ 54 | 'binary_path' => '', 55 | 56 | /* 57 | * The maximum time in seconds each optimizer is allowed to run separately. 58 | */ 59 | 'timeout' => 60, 60 | 61 | /* 62 | * If set to `true` all output of the optimizer binaries will be appended to the default log. 63 | * You can also set this to a class that implements `Psr\Log\LoggerInterface`. 64 | */ 65 | 'log_optimizer_activity' => false, 66 | ]; 67 | -------------------------------------------------------------------------------- /phpunit.xml.dist.bak: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | tests 15 | 16 | 17 | 18 | 19 | src/ 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/Exceptions/InvalidConfiguration.php: -------------------------------------------------------------------------------- 1 | mergeConfigFrom(__DIR__.'/../config/image-optimizer.php', 'image-optimizer'); 19 | 20 | if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) { 21 | $this->publishes([ 22 | __DIR__.'/../config/image-optimizer.php' => config_path('image-optimizer.php'), 23 | ], 'config'); 24 | } 25 | 26 | if ($this->app instanceof LumenApplication) { 27 | $this->app->configure('image-optimizer'); 28 | } 29 | 30 | $this->app->bind(OptimizerChain::class, function () { 31 | return OptimizerChainFactory::create(config('image-optimizer')); 32 | }); 33 | 34 | $this->app->singleton('image-optimizer', OptimizerChain::class); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Middlewares/OptimizeImages.php: -------------------------------------------------------------------------------- 1 | allFiles()) 16 | ->flatten() 17 | ->filter(function (UploadedFile $file) { 18 | if (app()->environment('testing')) { 19 | return true; 20 | } 21 | 22 | return $file->isValid(); 23 | }) 24 | ->each(function (UploadedFile $file) use ($optimizerChain) { 25 | $optimizerChain->optimize($file->getPathname()); 26 | }); 27 | 28 | return $next($request); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/OptimizerChainFactory.php: -------------------------------------------------------------------------------- 1 | useLogger(static::getLogger($config)) 17 | ->setTimeout($config['timeout']) 18 | ->setOptimizers(static::getOptimizers($config)); 19 | } 20 | 21 | protected static function getLogger($config): LoggerInterface 22 | { 23 | $configuredLogger = $config['log_optimizer_activity']; 24 | 25 | if ($configuredLogger === true) { 26 | return app('log'); 27 | } 28 | 29 | if ($configuredLogger === false) { 30 | return new DummyLogger(); 31 | } 32 | 33 | if (! is_a($configuredLogger, LoggerInterface::class, true)) { 34 | throw InvalidConfiguration::notAnLogger($configuredLogger); 35 | } 36 | 37 | return new $configuredLogger(); 38 | } 39 | 40 | protected static function getOptimizers(array $config) 41 | { 42 | return collect($config['optimizers']) 43 | ->mapWithKeys(function (array $options, string $optimizerClass) use ($config) { 44 | if (! is_a($optimizerClass, Optimizer::class, true)) { 45 | throw InvalidConfiguration::notAnOptimizer($optimizerClass); 46 | } 47 | 48 | // Initialize optimizer class 49 | $newOptimizerClass = new $optimizerClass(); 50 | 51 | if (static::getBinaryPath($config)) { 52 | $newOptimizerClass->setBinaryPath(self::getBinaryPath($config)); 53 | } 54 | 55 | $newOptimizerClass->setOptions($options); 56 | 57 | return [$optimizerClass => $newOptimizerClass]; 58 | }) 59 | ->toArray(); 60 | } 61 | 62 | public static function getBinaryPath(array $config): string 63 | { 64 | return $config['binary_path'] ?? ''; 65 | } 66 | } 67 | --------------------------------------------------------------------------------