├── .editorconfig ├── .gitignore ├── .styleci.yml ├── LICENSE.md ├── README.md ├── composer.json ├── config └── laravel-webp.php └── src ├── Cwebp.php ├── Exceptions ├── CwebpShellExecutionFailed.php ├── DriverIsNotSupportedException.php └── ImageMimeNotSupportedException.php ├── Facades └── Webp.php ├── Interfaces └── WebpInterface.php ├── PhpGD.php ├── Traits └── WebpTrait.php ├── Webp.php └── WebpServiceProvider.php /.editorconfig: -------------------------------------------------------------------------------- 1 | ; This file is for unifying the coding style for different editors and IDEs. 2 | ; More information at http://editorconfig.org 3 | 4 | root = true 5 | 6 | [*] 7 | charset = utf-8 8 | indent_size = 4 9 | indent_style = space 10 | end_of_line = lf 11 | insert_final_newline = true 12 | trim_trailing_whitespace = true 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | PULL_REQUEST_TEMPLATE.md 2 | prefill.php 3 | .idea 4 | vendor 5 | composer.lock 6 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: psr2 2 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) 2017 :author_name <:author_email> 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 | [](https://supportukrainenow.org) 2 | 3 | ## WebP (.webp) comes to Laravel 4 | 5 | [![Latest Version on Packagist][ico-version]][link-packagist] 6 | [![Software License][ico-license]](LICENSE.md) 7 | [![Total Downloads][ico-downloads]][link-downloads] 8 | 9 | ## About 10 | 11 | WebP is a modern image format that provides superior lossless and lossy compression for images on the web. Using WebP, 12 | webmasters and web developers can create smaller, richer images that make the web faster. 13 | 14 | WebP lossless images are 26% smaller in size compared to PNGs. WebP lossy images are 25-34% smaller than comparable JPEG 15 | images at equivalent SSIM quality index. 16 | 17 | Lossless WebP supports transparency (also known as alpha channel) at a cost of just 22% additional bytes. For cases when 18 | lossy RGB compression is acceptable, lossy WebP also supports transparency, typically providing 3× smaller file sizes 19 | compared to PNG. 20 | 21 | `cwebp` compresses an image using the WebP format. Input format can be either `PNG`, `JPEG`, `TIFF`, `WebP` or 22 | raw `Y'CbCr` samples. 23 | 24 | ## Before Installation 25 | 26 | ### New Driver Is Available: `php-gd` 27 | 28 | Currently, supports 2 drivers: 29 | * `php-gd` - Only needs `gd` - PHP extension to be installed 30 | * `cwebp` - uses Google native `cwebp` cli command 31 | 32 | Note: If you choose to use cwebp driver you will need to install WebP before installing this package.\ 33 | For more information you can visit this [page](https://developers.google.com/speed/webp/) 34 | 35 | ## Install 36 | 37 | Via Composer 38 | 39 | ```bash 40 | $ composer require buglinjo/laravel-webp 41 | ``` 42 | 43 | #### For Laravel <= 5.4 44 | 45 | After updating composer, add the ServiceProvider to the providers array in config/app.php 46 | 47 | ```php 48 | Buglinjo\LaravelWebp\WebpServiceProvider::class, 49 | ``` 50 | 51 | You can use the facade for shorter code. Add this to your aliases: 52 | 53 | ```php 54 | 'Webp' => Buglinjo\LaravelWebp\Facades\Webp::class, 55 | ``` 56 | 57 | #### Publish config file 58 | 59 | You will need to publish config file to add `cwebp` global path. 60 | 61 | ``` 62 | php artisan vendor:publish --provider="Buglinjo\LaravelWebp\WebpServiceProvider" --tag=config 63 | ``` 64 | 65 | In `config/laravel-webp.php` config file you should set `cwebp` global path. 66 | 67 | ```php 68 | return [ 69 | /* 70 | |-------------------------------------------------------------------------- 71 | | Default Quality 72 | |-------------------------------------------------------------------------- 73 | | 74 | | This is a default quality unless you provide while generation of the WebP 75 | | 76 | */ 77 | 'default_quality' => 70, 78 | 79 | /* 80 | |-------------------------------------------------------------------------- 81 | | Default Driver 82 | |-------------------------------------------------------------------------- 83 | | 84 | | This is a default image processing driver. Available: ['cwebp', 'php-gd'] 85 | | 86 | */ 87 | 'default_driver' => 'php-gd', 88 | 89 | /* 90 | |-------------------------------------------------------------------------- 91 | | Drivers 92 | |-------------------------------------------------------------------------- 93 | | 94 | | Available drivers which can be selected 95 | | 96 | */ 97 | 'drivers' => [ 98 | 99 | /* 100 | |-------------------------------------------------------------------------- 101 | | Cwebp Driver 102 | |-------------------------------------------------------------------------- 103 | | 104 | | If you choose cwebp driver it is required to specify the path to the executable. 105 | | 106 | */ 107 | 'cwebp' => [ 108 | 'path' => '/usr/local/bin/cwebp', 109 | ], 110 | 111 | /* 112 | |-------------------------------------------------------------------------- 113 | | Cwebp Driver 114 | |-------------------------------------------------------------------------- 115 | | 116 | | If you choose PHP GD driver no configuration is necessary. 117 | | 118 | */ 119 | 'php-gd' => [ 120 | // 121 | ], 122 | ], 123 | ]; 124 | ``` 125 | 126 | ## Usage 127 | 128 | ```php 129 | Webp::make()->save(, ); 130 | ``` 131 | 132 | Note: `UploadedFile` class instance is created when file is retrieved using laravel request. 133 | 134 | Example: 135 | 136 | ```php 137 | $webp = Webp::make($request->file('image')); 138 | 139 | if ($webp->save(public_path('output.webp'))) { 140 | // File is saved successfully 141 | } 142 | ``` 143 | 144 | where `` is 0 - 100 integer. 0 - lowest quality, 100 - highest quality. 145 | 146 | Default `quality` is 70 147 | 148 | Also you can set `quality` by chaining `->quality()` between `WebP::make()` 149 | and `->save();` 150 | 151 | ## License 152 | 153 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 154 | 155 | [ico-version]: https://img.shields.io/packagist/v/buglinjo/laravel-webp.svg?style=flat-square 156 | 157 | [ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square 158 | 159 | [ico-downloads]: https://img.shields.io/packagist/dt/buglinjo/laravel-webp.svg?style=flat-square 160 | 161 | [link-packagist]: https://packagist.org/packages/buglinjo/laravel-webp 162 | 163 | [link-downloads]: https://packagist.org/packages/buglinjo/laravel-webp 164 | 165 | [link-author]: https://github.com/buglinjo 166 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "buglinjo/laravel-webp", 3 | "type": "library", 4 | "description": "Laravel package for WebP image formatting.", 5 | "keywords": [ 6 | "Buglinjo", 7 | "LaravelWebp", 8 | "Webp", 9 | "Laravel" 10 | ], 11 | "homepage": "https://github.com/buglinjo/laravel-webp", 12 | "license": "MIT", 13 | "authors": [ 14 | { 15 | "name": "Irakli Tchitadze", 16 | "email": "irakli.tchitadze@gmail.com", 17 | "homepage": "https://irakli.dev", 18 | "role": "Developer" 19 | } 20 | ], 21 | "require": { 22 | "laravel/framework": "^10.0|^11.0|^12.0", 23 | "php": "^8.0|^8.1", 24 | "ext-gd": "*" 25 | }, 26 | "require-dev": { 27 | "phpunit/phpunit": ">=5.4.3", 28 | "squizlabs/php_codesniffer": "^2.3|^3.6" 29 | }, 30 | "autoload": { 31 | "psr-4": { 32 | "Buglinjo\\LaravelWebp\\": "src" 33 | } 34 | }, 35 | "autoload-dev": { 36 | "psr-4": { 37 | "Buglinjo\\LaravelWebp\\": "tests" 38 | } 39 | }, 40 | "scripts": { 41 | "test": "phpunit", 42 | "check-style": "phpcs -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests", 43 | "fix-style": "phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests" 44 | }, 45 | "extra": { 46 | "laravel": { 47 | "providers": [ 48 | "Buglinjo\\LaravelWebp\\WebpServiceProvider" 49 | ], 50 | "aliases": { 51 | "Webp": "Buglinjo\\LaravelWebp\\Webp" 52 | } 53 | } 54 | }, 55 | "config": { 56 | "sort-packages": true 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /config/laravel-webp.php: -------------------------------------------------------------------------------- 1 | 70, 13 | 14 | /* 15 | |-------------------------------------------------------------------------- 16 | | Default Driver 17 | |-------------------------------------------------------------------------- 18 | | 19 | | This is a default image processing driver. Available: ['cwebp', 'php-gd'] 20 | | 21 | */ 22 | 'default_driver' => 'php-gd', 23 | 24 | /* 25 | |-------------------------------------------------------------------------- 26 | | Drivers 27 | |-------------------------------------------------------------------------- 28 | | 29 | | Available drivers which can be selected 30 | | 31 | */ 32 | 'drivers' => [ 33 | 34 | /* 35 | |-------------------------------------------------------------------------- 36 | | Cwebp Driver 37 | |-------------------------------------------------------------------------- 38 | | 39 | | If you choose cwebp driver it is required to specify the path to the executable. 40 | | 41 | */ 42 | 'cwebp' => [ 43 | 'path' => '/usr/local/bin/cwebp', 44 | ], 45 | 46 | /* 47 | |-------------------------------------------------------------------------- 48 | | PHP GD Driver 49 | |-------------------------------------------------------------------------- 50 | | 51 | | If you choose PHP GD driver no configuration is necessary. 52 | | 53 | */ 54 | 'php-gd' => [ 55 | // 56 | ], 57 | ], 58 | ]; 59 | -------------------------------------------------------------------------------- /src/Cwebp.php: -------------------------------------------------------------------------------- 1 | cwebpPath = Config::get('laravel-webp.drivers.cwebp.path'); 27 | } 28 | 29 | /** 30 | * @param string $outputPath 31 | * @param int|null $quality 32 | * @return bool 33 | * @throws CwebpShellExecutionFailed 34 | */ 35 | public function save(string $outputPath, ?int $quality = null): bool 36 | { 37 | $quality = $quality ?? $this->quality; 38 | $cmd = [$this->cwebpPath, '-q', $quality, $this->image->getPathname(), '-o', $outputPath]; 39 | 40 | $process = Process::command($cmd); 41 | $result = $process->run(); 42 | 43 | if ($result->failed()) { 44 | throw new CwebpShellExecutionFailed($result->command(), $result->output(), $result->exitCode()); 45 | } 46 | 47 | return File::exists($outputPath); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/Exceptions/CwebpShellExecutionFailed.php: -------------------------------------------------------------------------------- 1 | makeMessage($cmd, $code, $output), $code, $previous); 13 | } 14 | 15 | /** 16 | * @param string $cmd 17 | * @param int|null $exitCode 18 | * @param string $output 19 | * @return string 20 | */ 21 | protected function makeMessage(string $cmd, ?int $exitCode, string $output): string 22 | { 23 | return 'Image conversion to WebP using cwebp failed with error code ' . $exitCode . ".\n" 24 | . "This command was used to execute cwebp: \n" 25 | . " " . $cmd . "\n" 26 | . ( 27 | trim($output) 28 | ? "The following output was sent to stdout: \n$output" 29 | : "No output was sent to stdout" 30 | ); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Exceptions/DriverIsNotSupportedException.php: -------------------------------------------------------------------------------- 1 | quality; 23 | $path = $this->image->path(); 24 | $info = getimagesize($path); 25 | $isAlpha = false; 26 | 27 | if ($info === false) { 28 | throw new ImageMimeNotSupportedException('Image mime type SVG is not supported.'); 29 | } 30 | 31 | switch ($info['mime']) { 32 | case 'image/jpeg': 33 | $image = imagecreatefromjpeg($path); 34 | break; 35 | case 'image/gif': 36 | $isAlpha = true; 37 | $image = imagecreatefromgif($path); 38 | break; 39 | case 'image/png': 40 | $isAlpha = true; 41 | $image = @imagecreatefrompng($path); 42 | break; 43 | default: 44 | throw new ImageMimeNotSupportedException('Image mime type' . $info['mime'] . 'is not supported.'); 45 | } 46 | 47 | if ($isAlpha) { 48 | imagepalettetotruecolor($image); 49 | imagealphablending($image, true); 50 | imagesavealpha($image, true); 51 | } 52 | 53 | imagewebp($image, $outputPath, $quality); 54 | 55 | return File::exists($outputPath); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/Traits/WebpTrait.php: -------------------------------------------------------------------------------- 1 | quality = Config::get('laravel-webp.default_quality'); 27 | $this->image = $image; 28 | 29 | return $this; 30 | } 31 | 32 | /** 33 | * @param int|null $quality 34 | * @return $this 35 | */ 36 | public function quality(?int $quality): self 37 | { 38 | $this->quality = $quality; 39 | 40 | return $this; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Webp.php: -------------------------------------------------------------------------------- 1 | make($image); 28 | case 'cwebp': 29 | return (new Cwebp())->make($image); 30 | } 31 | 32 | return null; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/WebpServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 17 | __DIR__ . '/../config/laravel-webp.php' => config_path('laravel-webp.php'), 18 | ], 'config'); 19 | } 20 | 21 | /** 22 | * Register any package services. 23 | * 24 | * @return void 25 | */ 26 | public function register() 27 | { 28 | $this->app->bind('webp', function () { 29 | return new Webp(); 30 | }); 31 | } 32 | } 33 | --------------------------------------------------------------------------------