DEV Community

Cover image for How to Add a Blur Effect to an Image in Laravel 13 with Intervention Image 4
Muhammad Yasir Amin
Muhammad Yasir Amin

Posted on Originally published at yasiramin.dev

How to Add a Blur Effect to an Image in Laravel 13 with Intervention Image 4

To blur an image in Laravel 13, decode it with Intervention Image 4 and call $image->blur($level), where the level is a number from 0 to 100. A low level such as 3 to 10 gives a soft blur, a high level hides the content. This tutorial shows the method, how to save or return the blurred image, and why you should limit the level when you use the GD driver.

Targets: Laravel 13 (PHP 8.3 to 8.5), intervention/image 4 and the GD or Imagick extension.

Step 1: Install Intervention Image

composer require intervention/image
Enter fullscreen mode Exit fullscreen mode

Step 2: Blur an image

use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\ImageManager;

$manager = ImageManager::usingDriver(Driver::class);

$image = $manager->decode(storage_path('app/public/photos/original.jpg'));

$image->blur(10);

$image->save(storage_path('app/public/photos/blurred.jpg'));
Enter fullscreen mode Exit fullscreen mode

The signature is blur(int $level = 5). It applies a Gaussian blur. Calling it without an argument uses level 5.

Which blur level should I use?

  • 1 to 5: a very light softening, useful to smooth small noise.
  • 5 to 20: a clearly visible blur that still shows shapes. Good for backgrounds and previews.
  • Above 30: details disappear. Use it for hiding sensitive content such as a locked preview.

The values above are practical guidelines, not official limits. The documented range is 0 to 100, so test the result on your own images.

Why is blur slow with GD?

The Intervention documentation warns that with the GD driver the blur method is performance intensive for larger blur levels. A big image with a high level can use a lot of CPU time. Three ways to stay safe:

  • Reduce the image first with scaleDown(width: 800) before blurring, so there are fewer pixels to process.
  • Limit the level you accept from users.
  • Use the Imagick driver if it is installed, because it is faster for large images.
$image = $manager->decode($path)
    ->scaleDown(width: 800)
    ->blur(15);
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a controller with a safe level

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\Format;
use Intervention\Image\ImageManager;

class BlurController extends Controller
{
    public function __invoke(Request $request): Response
    {
        $data = $request->validate([
            'image' => ['required', 'image', 'mimes:jpg,jpeg,png,webp', 'max:5120'],
            'level' => ['nullable', 'integer', 'min:1', 'max:30'],
        ]);

        $image = ImageManager::usingDriver(Driver::class)
            ->decode($request->file('image')->getRealPath())
            ->scaleDown(width: 1200)
            ->blur((int) ($data['level'] ?? 8));

        $encoded = $image->encodeUsingFormat(Format::JPEG, quality: 85);

        return response((string) $encoded, 200, ['Content-Type' => 'image/jpeg']);
    }
}
Enter fullscreen mode Exit fullscreen mode
Route::post('/blur', BlurController::class)->middleware('throttle:20,1');
Enter fullscreen mode Exit fullscreen mode

The validation caps the file at 5 MB and the level at 30. The throttle protects the server from many heavy requests in a short time.

How do I create a blurred placeholder?

A common use is a tiny blurred preview shown while the full image loads:

$placeholder = $manager->decode($path)
    ->scaleDown(width: 40)
    ->blur(5)
    ->encodeUsingFormat(Format::JPEG, quality: 40);

$dataUri = $placeholder->toDataUri();
Enter fullscreen mode Exit fullscreen mode

The tiny image is only a few hundred bytes, so you can store the data URI in the database and print it in an img tag as the initial source.

How do I check that it works?

  1. Post an image to /blur with level=5 and again with level=25.
  2. Compare the two results. The second should be clearly softer.
  3. Send level=80 and confirm validation rejects it with a 422 response.

Common mistakes

  • Blurring the full-size photo with GD: scale it down first.
  • Accepting any level from users: validate the range.
  • Using the old read() method: Intervention Image 4 uses decode().
  • Expecting blur to be secure hiding: a light blur can be partly reversed. For private content, do not serve the original at all.
  • Overwriting the original file: save to a new path so you can redo the effect.

FAQ

Is blur() a method of the image object?

Yes. In Intervention Image 4 it is Image::blur(int $level = 5) and it returns the image so you can chain more methods.

Can I blur only part of an image?

Blur affects the whole image. To blur a part, crop the region, blur it and insert it back with insert().

Which driver should I use in production?

Imagick if you can install it. GD works but is slower for large images and strong blur levels.

References


Originally published on yasiramin.dev.

Top comments (0)