here is my setup for uploading images with jobs. When I updating channel user choose image from computer and dispatch UploadImage job
public function update(ChannelUpdateRequest $request, Channel $channel)
{
$this->authorize('update', $channel);
$channel->update([
'name' => $request->name,
'slug' => $request->slug,
'description' => $request->description,
]);
if ($request->file('image')) {
$request->file('image')->move(
storage_path() . '/uploads',
$fileId = uniqid(true)
);
$this->dispatch(new UploadImage($channel, $fileId));
}
return redirect('/channel/' . $request->slug . '/edit');
}
here is my job . in handle method I'm storing image to storage folder via Image Intervention made size 50x50. and send it to s3 bucket if it is successful I delete this image from storage folder and save that filename to my db column . also encode all formats to .png
public $channel;
public $fileId;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Channel $channel, $fileId)
{
$this->channel = $channel;
$this->fileId = $fileId;
}
public function handle()
{
$path = storage_path() . '/uploads/' . $this->fileId;
$fileName = $this->fileId . '.png';
Image::make($path)->encode('png')->fit(50, 50, function ($c) {
$c->upsize();
})->save();
if (Storage::disk('s3images')->put('profile/' . $fileName, fopen($path, 'r+'))) {
\File::delete($path);
}
$this->channel->image_filename = $fileName;
$this->channel->save();
}