It looks like the code is correct, so I would suggest taking a look in the laravel log to see what went wrong.
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
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.
Please or to participate in this conversation.