add thumbnail cover on memo

This commit is contained in:
2020-05-17 19:23:50 +02:00
parent 0034fdbbc4
commit aae3dba36e
4 changed files with 40 additions and 1 deletions

View File

@@ -6,6 +6,8 @@ use App\Http\Resources\Image as ImageResource;
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
@@ -26,6 +28,16 @@ class ImageController extends Controller
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();
$newImage = $memo->images()->create([
@@ -35,19 +47,35 @@ class ImageController extends Controller
'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);
}
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()));