@dennis1502
To achieve so that every image is processed in it's own job i would structure it like this:
Using: https://laravel.com/docs/5.8/scheduling#scheduling-queued-jobs
i would schedule a queued job in App\Console\Kernel class, like so:
<?php
namespace App\Console;
use Jobs\CheckForImagesToConvert;
use Illuminate\Support\Facades\DB;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->job(new CheckForImagesToConvert)->everyFiveMinutes();
}
}
Then create a Job class in which i would write my logic in the handle() method for the following:
- Check if there are any images in the folder the client has to upload to
- Check if image is older than 5 minutes (to be sure there is no image still in "upload" process
- Check if image is greater than 0kb (yes, it happens that the client uploads 0kb images)
Then if all of the 3 steps are passing i would dispatch another job for every image that matches the 3 things above while referencing the image somehow.
An dispatch for each validated image (if any), would look something like this:
<?php
namespace App\Jobs;
use Exception;
use Jobs\ProcessImage;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class CheckForImagesToConvert implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// 1. Check if there are any images in the folder the client has to upload to
foreach($images as $image) {
if (//Check if image is older than 5 minutes AND more then 0KB) {
ProcessImage::dispatch($image);
}
}
}
}
Then i would create the job ProccessImage in which i would handle the remaining steps for the image itself:
- Reduce image with "intervention/image"
- Copy image to local website
- Move image to "uploaded" folder as backup
Hope this helps in clarifying the general structure of things, or at least my point of view on it.
Happy coding :).