Merge branch 'master' into 'production'
add thumbnail cover on memo See merge request Romulus21/portal!51
This commit is contained in:
@@ -6,6 +6,8 @@ use App\Http\Resources\Image as ImageResource;
|
|||||||
use App\Models\Memo;
|
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\Storage;
|
||||||
use Intervention\Image\Facades\Image;
|
use Intervention\Image\Facades\Image;
|
||||||
|
|
||||||
class ImageController extends Controller
|
class ImageController extends Controller
|
||||||
@@ -26,6 +28,16 @@ class ImageController extends Controller
|
|||||||
|
|
||||||
public function memos(Memo $memo)
|
public function memos(Memo $memo)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
foreach ($memo->images as $image) {
|
||||||
|
$exist = File::exists(storage_path('app/public/'.$image->path));
|
||||||
|
if($exist) {
|
||||||
|
File::delete(storage_path('app/public/'.$image->path));
|
||||||
|
$memo->images()->where('id', $image->id)->delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
\request()['thumbnail'] = true;
|
||||||
$data = $this->storeImage();
|
$data = $this->storeImage();
|
||||||
|
|
||||||
$newImage = $memo->images()->create([
|
$newImage = $memo->images()->create([
|
||||||
@@ -35,19 +47,35 @@ class ImageController extends Controller
|
|||||||
'location' => $data['location'],
|
'location' => $data['location'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
// Thumbnail
|
||||||
|
$memo->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);
|
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()));
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ class Memo extends JsonResource
|
|||||||
'attributes' => [
|
'attributes' => [
|
||||||
'posted_by' => new UserResource($this->user),
|
'posted_by' => new UserResource($this->user),
|
||||||
'cover_image' => new ImageResource($this->coverImage),
|
'cover_image' => new ImageResource($this->coverImage),
|
||||||
|
'thumbnail_cover_image' => new ImageResource($this->thumbnailImage),
|
||||||
]
|
]
|
||||||
//'tags' => TagResource::collection($this->tags),
|
//'tags' => TagResource::collection($this->tags),
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -41,4 +41,14 @@ class Memo extends Model
|
|||||||
$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', 'cover-small')
|
||||||
|
->withDefault(function ($userImage) {
|
||||||
|
$userImage->path = 'images/default-cover.jpg';
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<router-link v-for="memo in memos" :key="memo.data.memo_id" :to="'/memos/' + memo.data.memo_id" class="w-full sm:w-1/2 md:w-1/3 lg:w-1/4">
|
<router-link v-for="memo in memos" :key="memo.data.memo_id" :to="'/memos/' + memo.data.memo_id" class="w-full sm:w-1/2 md:w-1/3 lg:w-1/4">
|
||||||
<div class="m-2 max-w-lg rounded overflow-hidden shadow-lg bg-white">
|
<div class="m-2 max-w-lg rounded overflow-hidden shadow-lg bg-white">
|
||||||
<img :src="memo.data.attributes.cover_image.data.attributes.path" alt="" class="w-full">
|
<img :src="memo.data.attributes.thumbnail_cover_image.data.attributes.path" alt="" class="w-full">
|
||||||
<div class="px-6 py-4">
|
<div class="px-6 py-4">
|
||||||
<h1 class="text-gray-700">{{ memo.data.name }}</h1>
|
<h1 class="text-gray-700">{{ memo.data.name }}</h1>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user