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?
Laravel performs asynchronous tasks via the queue system.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With