pixelfed/app/Jobs/AvatarPipeline/AvatarOptimize.php

78 lines
1.9 KiB
PHP
Raw Normal View History

2018-08-10 09:10:05 +00:00
<?php
namespace App\Jobs\AvatarPipeline;
2018-08-28 03:07:36 +00:00
use App\Avatar;
use App\Profile;
use Carbon\Carbon;
2018-08-10 09:10:05 +00:00
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
2018-08-28 03:07:36 +00:00
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Image as Intervention;
2018-08-10 09:10:05 +00:00
class AvatarOptimize implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $profile;
protected $current;
2019-01-12 22:24:20 +00:00
/**
* Delete the job if its models no longer exist.
*
* @var bool
*/
public $deleteWhenMissingModels = true;
2018-08-10 09:10:05 +00:00
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Profile $profile, $current)
{
$this->profile = $profile;
$this->current = $current;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$avatar = $this->profile->avatar;
$file = storage_path("app/$avatar->media_path");
try {
$img = Intervention::make($file)->orientate();
$img->fit(200, 200, function ($constraint) {
$constraint->upsize();
});
$quality = config('pixelfed.image_quality');
$img->save($file, $quality);
$avatar = Avatar::whereProfileId($this->profile->id)->firstOrFail();
$avatar->thumb_path = $avatar->media_path;
$avatar->change_count = ++$avatar->change_count;
$avatar->last_processed_at = Carbon::now();
$avatar->save();
$this->deleteOldAvatar($avatar->media_path, $this->current);
} catch (Exception $e) {
}
}
protected function deleteOldAvatar($new, $current)
{
2018-08-28 03:07:36 +00:00
if (storage_path('app/'.$new) == $current) {
2018-08-10 09:10:05 +00:00
return;
}
2018-08-28 03:07:36 +00:00
if (is_file($current)) {
2018-08-10 09:10:05 +00:00
@unlink($current);
}
}
}