Imagenes en productos.
This commit is contained in:
@@ -68,13 +68,39 @@ class ProductosController extends Controller
|
||||
'min_stock_alert' => 'required|integer|min:0',
|
||||
'type' => 'required|in:bike,accessory,clothing,spare,service,children,skate,rollers,other',
|
||||
'serial_number' => 'nullable|string|max:100',
|
||||
'image' => 'nullable|image|mimes:jpeg,png,jpg,webp|max:2048',
|
||||
'image' => 'nullable|image|mimes:jpeg,png,jpg,webp|max:20480',
|
||||
'suppliers_id' => 'required|exists:suppliers,id',
|
||||
'description' => 'nullable|string'
|
||||
], [
|
||||
'image.uploaded' => 'La imagen supera el límite de subida permitido por el servidor.',
|
||||
'image.max' => 'La imagen no debe pesar más de 20 MB.',
|
||||
'image.image' => 'El archivo debe ser una imagen válida.',
|
||||
'image.mimes' => 'La imagen debe tener formato jpeg, png, jpg o webp.',
|
||||
]);
|
||||
|
||||
if ($request->hasFile('image')) {
|
||||
$path = $request->file('image')->store('products', 'public');
|
||||
$file = $request->file('image');
|
||||
|
||||
// Elevar temporalmente el límite de memoria para procesar la imagen
|
||||
ini_set('memory_limit', '512M');
|
||||
|
||||
// Usar Intervention Image v4 con driver GD para procesar la imagen
|
||||
$manager = new \Intervention\Image\ImageManager(new \Intervention\Image\Drivers\Gd\Driver());
|
||||
$image = $manager->decode($file->getRealPath());
|
||||
|
||||
// Redimensionar proporcionalmente si excede 1200px
|
||||
if ($image->width() > 1200 || $image->height() > 1200) {
|
||||
$image->scale(width: 1200, height: 1200);
|
||||
}
|
||||
|
||||
// Comprimir a JPEG con 80% de calidad
|
||||
$encoded = $image->encode(new \Intervention\Image\Encoders\JpegEncoder(80));
|
||||
|
||||
// Nombre de archivo único
|
||||
$path = 'products/' . uniqid() . '.jpg';
|
||||
|
||||
// Guardar en disco public
|
||||
Storage::disk('public')->put($path, $encoded->toString());
|
||||
$validated['image_path'] = $path;
|
||||
}
|
||||
unset($validated['image']);
|
||||
@@ -106,9 +132,14 @@ class ProductosController extends Controller
|
||||
'stock_quantity' => 'required|integer|min:0',
|
||||
'min_stock_alert' => 'required|integer|min:0',
|
||||
'serial_number' => 'nullable|string|max:100',
|
||||
'image' => 'nullable|image|mimes:jpeg,png,jpg,webp|max:2048',
|
||||
'image' => 'nullable|image|mimes:jpeg,png,jpg,webp|max:20480',
|
||||
'suppliers_id' => 'required|exists:suppliers,id',
|
||||
'description' => 'nullable|string'
|
||||
], [
|
||||
'image.uploaded' => 'La imagen supera el límite de subida permitido por el servidor.',
|
||||
'image.max' => 'La imagen no debe pesar más de 20 MB.',
|
||||
'image.image' => 'El archivo debe ser una imagen válida.',
|
||||
'image.mimes' => 'La imagen debe tener formato jpeg, png, jpg o webp.',
|
||||
]);
|
||||
|
||||
if ($request->hasFile('image')) {
|
||||
@@ -116,7 +147,28 @@ class ProductosController extends Controller
|
||||
Storage::disk('public')->delete($product->image_path);
|
||||
}
|
||||
|
||||
$path = $request->file('image')->store('products', 'public');
|
||||
$file = $request->file('image');
|
||||
|
||||
// Elevar temporalmente el límite de memoria para procesar la imagen
|
||||
ini_set('memory_limit', '512M');
|
||||
|
||||
// Usar Intervention Image v4 con driver GD para procesar la imagen
|
||||
$manager = new \Intervention\Image\ImageManager(new \Intervention\Image\Drivers\Gd\Driver());
|
||||
$image = $manager->decode($file->getRealPath());
|
||||
|
||||
// Redimensionar proporcionalmente si excede 1200px
|
||||
if ($image->width() > 1200 || $image->height() > 1200) {
|
||||
$image->scale(width: 1200, height: 1200);
|
||||
}
|
||||
|
||||
// Comprimir a JPEG con 80% de calidad
|
||||
$encoded = $image->encode(new \Intervention\Image\Encoders\JpegEncoder(80));
|
||||
|
||||
// Nombre de archivo único
|
||||
$path = 'products/' . uniqid() . '.jpg';
|
||||
|
||||
// Guardar en disco public
|
||||
Storage::disk('public')->put($path, $encoded->toString());
|
||||
$validated['image_path'] = $path;
|
||||
}
|
||||
unset($validated['image']);
|
||||
|
||||
Reference in New Issue
Block a user