Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5: Performing an asynchronous task

I have the following function where I process an image file of type UploadedFile, give it as a random name, use \Intervention\Image to store it in different sizes, and then return the image name.

The storing part takes lot of time and the request usually times out. It would be great if I could do the processing in a separate thread or process, and return the image name. Here's the code I have:

public function storeSnapshots(UploadedFile $image) {
    // Generate a random image name.
    $imageName = str_random(12) . '.' . $image->getClientOriginalExtension();

    // Process the image. Should be done asynchronously.
    \Intervention\Image\Facades\Image::make($image)
        ->heighten(2000, function($constraint) {
            $constraint->upsize();
        })->save('img/lg/' . $imageName)
        ->heighten(800)->save('img/md/' . $imageName)
        ->heighten(120)->save('img/sm/' . $imageName);

    // Return the image name generated.
    return $imageName;
}

What would be the Laravel way to perform an asynchronous task?

like image 251
John Bupit Avatar asked Dec 14 '22 12:12

John Bupit


1 Answers

Laravel performs asynchronous tasks via the queue system.

like image 88
ceejayoz Avatar answered Dec 17 '22 02:12

ceejayoz