add small pic to User avatar

This commit is contained in:
2020-05-17 20:23:41 +02:00
parent aae3dba36e
commit d5be73c622
5 changed files with 41 additions and 20 deletions

View File

@@ -7,13 +7,19 @@ use App\Models\Memo;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
class ImageController extends Controller
{
public function users(User $user)
{
foreach (auth()->user()->images as $image) {
if(File::exists(storage_path('app/public/'.$image->path))) {
File::delete(storage_path('app/public/'.$image->path));
auth()->user()->images()->where('id', $image->id)->delete();
}
}
$data = $this->storeImage();
$newImage = auth()->user()->images()->create([
@@ -23,21 +29,22 @@ class ImageController extends Controller
'location' => $data['location'],
]);
$this->haveThumbnailImage($data, auth()->user());
auth()->user()->update(['updated_at' => now()]);
return new ImageResource($newImage);
}
public function memos(Memo $memo)
{
foreach ($memo->images as $image) {
$exist = File::exists(storage_path('app/public/'.$image->path));
if($exist) {
if(File::exists(storage_path('app/public/'.$image->path))) {
File::delete(storage_path('app/public/'.$image->path));
$memo->images()->where('id', $image->id)->delete();
}
}
\request()['thumbnail'] = true;
$data = $this->storeImage();
$newImage = $memo->images()->create([
@@ -47,35 +54,38 @@ class ImageController extends Controller
'location' => $data['location'],
]);
// Thumbnail
$memo->images()->create([
$this->haveThumbnailImage($data, $memo);
$memo->update(['updated_at' => now()]);
return new ImageResource($newImage);
}
private function haveThumbnailImage($data, $object)
{
Image::make($data['image'])
->fit(round($data['width'] / 3, 0), round($data['height'] / 3, 0))
->save(storage_path('app/public/thumbnail/images/'.$data['image']->hashName()));
$object->images()->create([
'path' => "thumbnail/".$data['path'],
'width' => round($data['width'] / 3, 0),
'height' => round($data['height'] / 3, 0),
'location' => $data['location']."-small",
]);
return new ImageResource($newImage);
}
private function storeImage() {
private function storeImage()
{
$data = request()->validate([
'image' => 'required',
'width' => 'required',
'height' => 'required',
'location' => 'required',
'thumbnail' => '',
]);
$data['path'] = $data['image']->store('images', 'public');
if($data['thumbnail']) {
Image::make($data['image'])
->fit(round($data['width'] / 3, 0), round($data['height'] / 3, 0))
->save(storage_path('app/public/thumbnail/images/'.$data['image']->hashName()));
}
Image::make($data['image'])
->fit($data['width'], $data['height'])
->save(storage_path('app/public/images/'.$data['image']->hashName()));

View File

@@ -24,6 +24,7 @@ class User extends JsonResource
'email' => $this->email,
'profile_image' => new ImageResource($this->profileImage),
'cover_image' => new ImageResource($this->coverImage),
'thumbnail_cover_image' => new ImageResource($this->thumbnailImage),
'last_login' => optional($this->login_at)->diffForHumans(),
'is_admin' => $this->isAdmin(),
],

View File

@@ -88,4 +88,14 @@ class User extends Authenticatable
$userImage->path = 'images/default-cover.jpg';
});
}
public function thumbnailImage(): MorphOne
{
return $this->morphOne(Image::class, 'imageable')
->orderBy('id', 'desc')
->where('location', 'profile-small')
->withDefault(function ($userImage) {
$userImage->path = 'images/default-cover.jpg';
});
}
}