From 0f70183154833b51895a2cdca61f12d45f1ad90a Mon Sep 17 00:00:00 2001 From: A1Gard Date: Thu, 5 Dec 2024 01:56:11 +0330 Subject: [PATCH] added optimize image ckeditor uploader --- .../Controllers/Admin/CkeditorController.php | 48 ++++++++++++++++--- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/Admin/CkeditorController.php b/app/Http/Controllers/Admin/CkeditorController.php index 56e45e9..f3ed264 100644 --- a/app/Http/Controllers/Admin/CkeditorController.php +++ b/app/Http/Controllers/Admin/CkeditorController.php @@ -5,21 +5,57 @@ namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use App\Models\User; use Illuminate\Http\Request; +use Spatie\Image\Enums\AlignPosition; +use Spatie\Image\Enums\Fit; +use Spatie\Image\Enums\Unit; +use Spatie\Image\Image; +use function PHPUnit\Framework\fileExists; class CkeditorController extends Controller { public function upload(Request $request) { if ($request->hasFile('upload')) { - $originName = $request->file('upload')->getClientOriginalName(); - $fileName = pathinfo($originName, PATHINFO_FILENAME); - $extension = $request->file('upload')->getClientOriginalExtension(); - $fileName = $fileName . '_' . time() . '.' . $extension; - $request->file('upload')->move(public_path('upload/images'), $fileName); + + $key = 'upload'; + $format = getSetting('optimize'); + $i = Image::load($request->file($key)->getRealPath()) + ->optimize() +// ->sharpen(10) + // ->quality(90) + // ->nonQueued() + ->format($format); + + $filename = 'optimized-' . $request->file($key)->getClientOriginalName() . '_' . time() . '.webp'; + if (getSetting('watermark')) { + $i->watermark(public_path('upload/images/logo.png'), + AlignPosition::BottomLeft, 5, 5, Unit::Percent, + config('app.media.watermark_size'), Unit::Percent, + config('app.media.watermark_size'), Unit::Percent, Fit::Contain, + config('app.media.watermark_opacity')); + } + $directoryPath = storage_path('app/public/upload'); + + if (!file_exists($directoryPath)) { + if (!mkdir($directoryPath, 0777, true) && !is_dir($directoryPath)) { + // Handle error - directory creation failed + throw new \RuntimeException(sprintf('Directory "%s" was not created', $directoryPath)); + } + } + $i->save(storage_path() . '/app/public/upload/'.$filename); + +// $originName = $request->file('upload')->getClientOriginalName(); +// $fileName = pathinfo($originName, PATHINFO_FILENAME); +// $extension = $request->file('upload')->getClientOriginalExtension(); +// $fileName = $fileName . '_' . time() . '.' . $extension; +// +// $request->file('upload')->move(public_path('upload/images'), $fileName); + $CKEditorFuncNum = $request->input('CKEditorFuncNum'); - $url = asset('/upload/images/' . $fileName); + $url =\Storage::url('upload/' . $filename); + $msg = __('Image uploaded successfully'); $response = "";