PHPackages                             ranjith/laravel-webp-converter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Image &amp; Media](/categories/media)
4. /
5. ranjith/laravel-webp-converter

ActiveLibrary[Image &amp; Media](/categories/media)

ranjith/laravel-webp-converter
==============================

Automatically convert uploaded images to WebP format in Laravel

v1.0.2(6mo ago)27MITPHPPHP ^8.1|^8.2|^8.3

Since Nov 3Pushed 6mo agoCompare

[ Source](https://github.com/ranjith67/laravel-webp-converter)[ Packagist](https://packagist.org/packages/ranjith/laravel-webp-converter)[ RSS](/packages/ranjith-laravel-webp-converter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (4)Used By (0)

Laravel WebP Converter
======================

[](#laravel-webp-converter)

Automatically convert uploaded images to WebP format in Laravel with support for multiple sizes and easy integration.

[![Latest Version on Packagist](https://camo.githubusercontent.com/404e06385b20ed33255d7ca947bcbdf3d553c1178239dba3a3b52e85845fb9f1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f72616e6a6974682f6c61726176656c2d776562702d636f6e7665727465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ranjith/laravel-webp-converter)[![Total Downloads](https://camo.githubusercontent.com/abc8bec187a7c65ec95fa1e8a6d233cf53832be0c6f46aca74632908d2fd2880/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f72616e6a6974682f6c61726176656c2d776562702d636f6e7665727465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ranjith/laravel-webp-converter)

Features
--------

[](#features)

- 🚀 **Automatic Conversion** - Simply assign an uploaded file to a model attribute
- 🖼️ **Multiple Sizes** - Generate thumbnails, medium, and large versions automatically
- 🔒 **Secure** - Uses Laravel's built-in secure file handling
- ⚙️ **Configurable** - Customize quality, sizes, and storage options
- 📦 **Laravel Integration** - Works seamlessly with Eloquent models
- 🎨 **Filament Compatible** - Works with Filament v3 &amp; v4 admin panels via dedicated helper
- 💾 **Keep Original** - Option to keep original images for fallback support

Requirements
------------

[](#requirements)

- PHP 8.1 or higher
- Laravel 11.0 or higher
- GD extension enabled

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require ranjith/laravel-webp-converter
```

The package will automatically register itself.

### Publish Configuration (Optional)

[](#publish-configuration-optional)

Publish the config file to customize settings:

```
php artisan vendor:publish --tag=webp-config
```

This will create a `config/webp.php` file where you can customize:

- Image quality
- Generated sizes (thumbnail, medium, large)
- Storage disk
- Whether to keep original images
- Allowed file extensions

Usage
-----

[](#usage)

### Basic Usage (Traditional Laravel Forms)

[](#basic-usage-traditional-laravel-forms)

**1. Add the trait to your model:**

```
use Illuminate\Database\Eloquent\Model;
use Ranjith\LaravelWebpConverter\Traits\HasWebPImages;

class Product extends Model
{
    use HasWebPImages;

    protected $fillable = ['name', 'image'];

    // Define which attributes should be converted to WebP
    protected $webpImages = ['image'];
}
```

**2. Upload images in your controller:**

```
public function store(Request $request)
{
    $product = new Product();
    $product->name = $request->name;
    $product->image = $request->file('image'); // Automatically converts to WebP!
    $product->save();

    return redirect()->back();
}
```

That's it! The image is automatically converted to WebP format.

---

### With Multiple Sizes

[](#with-multiple-sizes)

**1. Add size columns to your migration:**

```
Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->string('image')->nullable();
    $table->string('image_thumbnail')->nullable();
    $table->string('image_medium')->nullable();
    $table->string('image_large')->nullable();
    $table->timestamps();
});
```

**2. Configure your model:**

```
use Illuminate\Database\Eloquent\Model;
use Ranjith\LaravelWebpConverter\Traits\HasWebPImages;

class Product extends Model
{
    use HasWebPImages;

    protected $fillable = ['name', 'image', 'image_thumbnail', 'image_medium', 'image_large'];

    // Define which attributes should be converted to WebP
    protected $webpImages = ['image'];

    // Map size names to database columns
    protected $webpSizeColumns = [
        'image' => [
            'thumbnail' => 'image_thumbnail',
            'medium' => 'image_medium',
            'large' => 'image_large',
        ],
    ];
}
```

**3. Display images in your views:**

```

```

---

### Custom Directory

[](#custom-directory)

You can specify custom directories for different image attributes:

```
class Product extends Model
{
    use HasWebPImages;

    protected $webpImages = ['image', 'gallery'];

    // Define custom directories for storage
    protected $webpDirectories = [
        'image' => 'products/featured',
        'gallery' => 'products/gallery',
    ];
}
```

---

### Disable Size Generation

[](#disable-size-generation)

If you only want the main WebP image without additional sizes, set `sizes` to an empty array in `config/webp.php`:

```
'sizes' => [],
```

Then use a simpler migration:

```
Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->string('image')->nullable();
    $table->timestamps();
});
```

And simpler model:

```
class Product extends Model
{
    use HasWebPImages;

    protected $fillable = ['name', 'image'];
    protected $webpImages = ['image'];
}
```

---

Configuration
-------------

[](#configuration)

After publishing the config file, you can customize these settings in `config/webp.php`:

```
return [
    // WebP image quality (0-100)
    'quality' => 80,

    // Keep original image file
    'keep_original' => true,

    // Storage disk (must be defined in config/filesystems.php)
    'disk' => 'public',

    // Image sizes to generate
    'sizes' => [
        'thumbnail' => 150,
        'medium' => 500,
        'large' => 1200,
    ],

    // Allowed file extensions
    'allowed_extensions' => ['jpg', 'jpeg', 'png'],
];
```

### Configuration Options

[](#configuration-options)

OptionDefaultDescription`quality``80`WebP compression quality (0-100). Higher = better quality, larger file size.`keep_original``true`Keep the original uploaded image (JPG/PNG) for fallback support.`disk``'public'`Laravel storage disk to use (must be defined in `config/filesystems.php`).`sizes``[...]`Array of image sizes to generate. Key = size name, Value = width in pixels.`allowed_extensions``[...]`Array of allowed image extensions that can be converted to WebP.---

Filament Integration
--------------------

[](#filament-integration)

This package provides a dedicated helper class for seamless integration with [Filament v3 and v4](https://filamentphp.com/) admin panels.

### Installation

[](#installation-1)

No additional setup needed - the helper is included in the package.

### Basic Usage

[](#basic-usage)

**Model (no trait needed for Filament):**

```
