Merge branch 'master' into 'production'

add small pic to User avatar

See merge request Romulus21/portal!52
This commit is contained in:
Romain Delanoë
2020-05-17 18:24:39 +00:00
5 changed files with 41 additions and 20 deletions

View File

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

View File

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

View File

@@ -88,4 +88,14 @@ class User extends Authenticatable
$userImage->path = 'images/default-cover.jpg'; $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';
});
}
} }

View File

@@ -1,7 +1,7 @@
<template> <template>
<div class="top-bar flex justify-between bg-gray-600"> <div class="top-bar flex justify-between bg-gray-600">
<router-link v-if="authUser" :to="'/profile'" class="flex items-center m-2"> <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> <span class="ml-2 text-gray-100 hover:text-white">{{ authUser.data.attributes.name }}</span>
</router-link> </router-link>
<form v-if="authUser && search" class="m-2"> <form v-if="authUser && search" class="m-2">

View File

@@ -2,7 +2,7 @@
<div v-if="authUser"> <div v-if="authUser">
<div class="flex m-4"> <div class="flex m-4">
<div class="avatar mr-2"> <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>
<div class="flex flex-col justify-center ml-2"> <div class="flex flex-col justify-center ml-2">
<div><strong>{{ authUser.data.attributes.name }}</strong></div> <div><strong>{{ authUser.data.attributes.name }}</strong></div>