add small pic to User avatar
This commit is contained in:
@@ -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()));
|
||||
|
||||
@@ -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(),
|
||||
],
|
||||
|
||||
@@ -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';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<template>
|
||||
<div class="top-bar flex justify-between bg-gray-600">
|
||||
<router-link v-if="authUser" :to="'/profile'" class="flex items-center m-2">
|
||||
<Avatar :avatar="authUser.data.attributes.profile_image.data.attributes.path" size="small" :alt="authUser.data.attributes.name" class="w-10 h-10"/>
|
||||
<Avatar :avatar="authUser.data.attributes.thumbnail_cover_image.data.attributes.path" size="small" :alt="authUser.data.attributes.name" class="w-10 h-10"/>
|
||||
<span class="ml-2 text-gray-100 hover:text-white">{{ authUser.data.attributes.name }}</span>
|
||||
</router-link>
|
||||
<form v-if="authUser && search" class="m-2">
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<div v-if="authUser">
|
||||
<div class="flex m-4">
|
||||
<div class="avatar mr-2">
|
||||
<Avatar :avatar="authUser.data.attributes.profile_image.data.attributes.path" size="6xl" :alt="authUser.data.attributes.name" class="w-24 h-24" />
|
||||
<Avatar :avatar="authUser.data.attributes.thumbnail_cover_image.data.attributes.path" size="6xl" :alt="authUser.data.attributes.name" class="w-24 h-24" />
|
||||
</div>
|
||||
<div class="flex flex-col justify-center ml-2">
|
||||
<div><strong>{{ authUser.data.attributes.name }}</strong></div>
|
||||
|
||||
Reference in New Issue
Block a user