John Bupit
John Bupit

Reputation: 10618

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?

Upvotes: 4

Views: 40171

Answers (1)

ceejayoz
ceejayoz

Reputation: 179994

Laravel performs asynchronous tasks via the queue system.

Upvotes: 12

Related Questions