diff --git a/app/Http/Controllers/Admin/BasicSettings/MachineSettingController.php b/app/Http/Controllers/Admin/BasicSettings/MachineSettingController.php index 53c9540..7b73c4b 100644 --- a/app/Http/Controllers/Admin/BasicSettings/MachineSettingController.php +++ b/app/Http/Controllers/Admin/BasicSettings/MachineSettingController.php @@ -148,20 +148,30 @@ class MachineSettingController extends AdminController 'updater_id' => auth()->id(), ])); - // 處理圖片更新 (若有上傳新圖片,則替換或附加,這裡採簡單邏輯:若有傳 images 則全換) + // 處理圖片更新 (支援 3 個獨立槽位) if ($request->hasFile('images')) { - // 刪除舊圖 - if (!empty($machine->images)) { - foreach ($machine->images as $oldPath) { - Storage::disk('public')->delete($oldPath); + $currentImages = $machine->images ?? []; + $newImages = $request->file('images'); + $updated = false; + + foreach ($newImages as $index => $file) { + // 限制 3 個槽位 (0, 1, 2) + if ($index < 0 || $index > 2) continue; + + // 刪除該槽位的舊圖 + if (isset($currentImages[$index]) && !empty($currentImages[$index])) { + \Illuminate\Support\Facades\Storage::disk('public')->delete($currentImages[$index]); } + + // 處理並儲存新圖 + $currentImages[$index] = $this->processAndStoreImage($file); + $updated = true; } - $imagePaths = []; - foreach (array_slice($request->file('images'), 0, 3) as $image) { - $imagePaths[] = $this->processAndStoreImage($image); + if ($updated) { + ksort($currentImages); + $machine->update(['images' => array_values($currentImages)]); } - $machine->update(['images' => $imagePaths]); } return redirect()->route('admin.basic-settings.machines.index') @@ -169,46 +179,47 @@ class MachineSettingController extends AdminController } /** - * 處理圖片並轉換為 WebP + * 處理並儲存圖片 (轉換為 WebP 並調整大小) */ - private function processAndStoreImage($file): string + protected function processAndStoreImage($file) { - $filename = Str::random(40) . '.webp'; - $path = 'machines/' . $filename; - - // 建立圖資源 - $image = null; - $extension = strtolower($file->getClientOriginalExtension()); + $path = 'machines/' . \Illuminate\Support\Str::random(40) . '.webp'; - switch ($extension) { - case 'jpeg': - case 'jpg': + // 載入原圖 + $imageInfo = getimagesize($file->getRealPath()); + $mime = $imageInfo['mime']; + + switch ($mime) { + case 'image/jpeg': $image = imagecreatefromjpeg($file->getRealPath()); break; - case 'png': + case 'image/png': $image = imagecreatefrompng($file->getRealPath()); break; - case 'gif': + case 'image/gif': $image = imagecreatefromgif($file->getRealPath()); break; - case 'webp': - $image = imagecreatefromwebp($file->getRealPath()); - break; + default: + return $file->store('machines', 'public'); } if ($image) { - // 確保目錄存在 - Storage::disk('public')->makeDirectory('machines'); - $fullPath = Storage::disk('public')->path($path); + // [修正] imagewebp(): Palette image not supported by webp + // 若為 Palette 圖片 (例如 GIF),轉換為 Truecolor + if (!imageistruecolor($image)) { + imagepalettetotruecolor($image); + } + + \Illuminate\Support\Facades\Storage::disk('public')->makeDirectory('machines'); + $fullPath = \Illuminate\Support\Facades\Storage::disk('public')->path($path); - // 轉換並儲存 - imagewebp($image, $fullPath, 80); // 品質 80 + // 轉換並儲存 (品質 80) + imagewebp($image, $fullPath, 80); imagedestroy($image); return $path; } - // Fallback to standard store if GD fails return $file->store('machines', 'public'); } } diff --git a/resources/views/admin/basic-settings/machines/edit.blade.php b/resources/views/admin/basic-settings/machines/edit.blade.php index bb76cda..7b70268 100644 --- a/resources/views/admin/basic-settings/machines/edit.blade.php +++ b/resources/views/admin/basic-settings/machines/edit.blade.php @@ -1,7 +1,14 @@ @extends('layouts.admin') @section('content') -
+
@@ -227,45 +234,43 @@
- @if(!empty($machine->image_urls)) -
- @foreach($machine->image_urls as $url) -
- -
- @endforeach -
- @else -
-

{{ __('No images uploaded') }}

-
- @endif +
+ @for($i = 0; $i < 3; $i++) +
+
+

+ * {{ __('You can upload images one by one. Supporting up to 3 slots.') }}
+ * {{ __('Click any slot to select or replace a photo.') }} +