Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

GeorgLeb's avatar

Spatie Media Library conversion not working

Hello everyone, I have a problem with Spatie Media Library conversions. I want to convert the uploaded image into two different sizes using the queue worker. If I only use "thumb" as addMediaConversion() everything works fine, but as soon as i add any second addMediaConversion() the queued job fails. I set everything up the way it is described in the documentary from Spatie Media Library.

Here is my code:

Post.php

<?php

namespace App\Models;

use Spatie\Image\Enums\BorderType;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

class Post extends Model implements HasMedia
{
    use InteractsWithMedia;

    public function registerMediaCollections(): void
    {
        $this->addMediaCollection('posts');
    }

    public function registerMediaConversions(?Media $media = null): void
    {
        $this->addMediaConversion('thumb')
            ->width(368)
            ->height(232)
            ->sharpen(10);

        $this->addMediaConversion('preview')
            ->width(736)
            ->height(464)
            ->sharpen(10);
        # if only the "thumb" conversion is defined, everything works fine and the conversion is created
    }
}

PostController.php


namespace App\Http\Controllers;

use App\Http\Requests\PostRequest;
use App\Http\Resources\PostResource;

class PostController extends Controller
{
    public function store(PostRequest $request)
    {
        $validated = $request->validated();
        $post = Post::create($validated);

        if ($request->hasFile('file_upload')) {
            $post
                ->addMediaFromRequest('file_upload')
                ->toMediaCollection('posts');
        }

        return new PostResource($post);
    }
}

.env

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=database
SESSION_DRIVER=cookie
SESSION_LIFETIME=120

php artisan queue:work log

// if only thumb conversion is defined
  2024-08-13 07:07:07 Spatie\MediaLibrary\Conversions\Jobs\PerformConversionsJob ............................................................................................. RUNNING
  2024-08-13 07:07:08 Spatie\MediaLibrary\Conversions\Jobs\PerformConversionsJob ....................................................................................... 744.84ms DONE


// if two conversions are defined
  2024-08-13 07:07:26 Spatie\MediaLibrary\Conversions\Jobs\PerformConversionsJob ............................................................................................. RUNNING
  2024-08-13 07:07:26 Spatie\MediaLibrary\Conversions\Jobs\PerformConversionsJob ........................................................................................ 24.85ms FAIL

The "failed_job" table does not tell me anything why exactly the job failed. There is only the info Spatie\MediaLibrary\MediaCollections\Exceptions\InvalidConversion: There is no conversion named `preview` in ...path\vendor\spatie\laravel-medialibrary\src\MediaCollections\Exceptions\InvalidConversion.php:11. The folder with the original image is created in every case, the "conversion"-folder is only created if only one conversion is defined.

What am I missing? Is there a special setting for the queue worker to handle multiple conversions at the same time? Thank you a lot in advance!

Edit

If i use the nonQueued() argument, everything works fine without any errors and all conversions are generated. So I guess it may be related to the artisan queue:work job.

And additionally, if i use the command

return $post->getRegisteredMediaCollections();

I get the following output

"posts": {
    "diskName": "",
    "conversionsDiskName": "",
    "mediaConversionRegistrations": {},
    "generateResponsiveImages": false,
    "acceptsFile": {},
    "acceptsMimeTypes": [
        "image/jpeg",
        "image/jpg",
        "image/peng",
        "image/png"
    ],
    "collectionSizeLimit": false,
    "singleFile": false,
    "fallbackUrls": [],
    "fallbackPaths": [],
    "name": "posts"
}

As you can see the mediaConversionRegistrations attribute is empty. I do not know if it should be filled, but it would make sense to me, if the conversions were listed there?

Edit 2

I finally found the problem. It was as simple as it could be. One of the laravel packages was not up to date. I just ran composer update and now everything works like a charm.

Hopefully this helps anyone

0 likes
7 replies
Tray2's avatar

It looks like the code is correct, so I would suggest taking a look in the laravel log to see what went wrong.

GeorgLeb's avatar

@Tray2 thank you for your really quick response!

I checked, but the error confuses me still more:

[2024-08-13 08:00:49] local.ERROR: There is no conversion named `preview` {"exception":"[object] (Spatie\\MediaLibrary\\MediaCollections\\Exceptions\\InvalidConversion(code: 0): There is no conversion named `preview` at [...path...]

do I have to define the conversion name anywhere else than in the model?

Tray2's avatar

@GeorgLeb Like I said the code looks solid, and should work, try renaming the conversion to preview-image just for a fools test.

GeorgLeb's avatar

@Tray2 hm, still same error. And i have/had to correct my initial post, the conversion ONLY works with the "thumb" conversion. Even a single conversion named "thumb2" or "thumbs" fails. Looks like spatie does not accept any other names than "thumb" for my project, which really confuses me

GeorgLeb's avatar
GeorgLeb
OP
Best Answer
Level 1

I finally found the solution to the problem. As mentioned in Edit 2, it was just a laravel package that was not up to date. I just ran composer update and now everything works like a charm.

agencep's avatar

Hello,

I have the same problems with conversion, in my

model PdvContent

 public function registerMediaConversions(Media $media = null): void
    {
        $this->addMediaConversion('thumb')
            ->width(368)
            ->height(232)
            ->sharpen(10)
            ->withResponsiveImages();
    }

Controller PdvContentController

public function store(Request $request)
{
if ($request->has('img_path')) {
            $pthToImg = $request->file('img_path');
            $media = $pdvContent->addMedia($pthToImg)
                ->withResponsiveImages()
                ->toMediaCollection('about');
        }
}

In /storage/app/public I can found the file just uploaded in sub-directory with ID but I can't found the conversion directory !!

Can you help me please ? Laravel 11 ans Spatie Laravel-medialibrary

Thanks

GeorgLeb's avatar

@agencep Sorry for the late response.

  1. Have you updated all packages (composer update)?

  2. Have you registered the mediaCollections()?

use InteractsWithMedia;

public function registerMediaCollections(): void
{
    $this->addMediaCollection('posts');
}

Please or to participate in this conversation.