composer para agregar imagenes, descomentar el extension=gd del php.ini
This commit is contained in:
@@ -167,26 +167,28 @@ class ProductosController extends Controller
|
||||
|
||||
$file = $request->file('image');
|
||||
|
||||
// Elevar temporalmente el límite de memoria para procesar la imagen
|
||||
ini_set('memory_limit', '512M');
|
||||
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());
|
||||
// 1. Iniciar el ImageManager (Sintaxis V2)
|
||||
$manager = new \Intervention\Image\ImageManager(['driver' => 'gd']);
|
||||
$image = $manager->make($file->getRealPath());
|
||||
|
||||
// Redimensionar proporcionalmente si excede 1200px
|
||||
// 2. Redimensionar proporcionalmente si excede 1200px
|
||||
if ($image->width() > 1200 || $image->height() > 1200) {
|
||||
$image->scale(width: 1200, height: 1200);
|
||||
$image->resize(1200, 1200, function ($constraint) {
|
||||
$constraint->aspectRatio(); // Mantiene la proporción original (no deforma)
|
||||
$constraint->upsize(); // Evita que imágenes chicas se agranden y pixelen
|
||||
});
|
||||
}
|
||||
|
||||
// Comprimir a JPEG con 80% de calidad
|
||||
$encoded = $image->encode(new \Intervention\Image\Encoders\JpegEncoder(80));
|
||||
// 3. Comprimir a JPEG con 80% de calidad y convertir a texto para guardar
|
||||
$encoded = (string) $image->encode('jpg', 80);
|
||||
|
||||
// Nombre de archivo único
|
||||
$path = 'products/' . uniqid() . '.jpg';
|
||||
|
||||
// Guardar en disco public
|
||||
Storage::disk('public')->put($path, $encoded->toString());
|
||||
Storage::disk('public')->put($path, $encoded);
|
||||
$validated['image_path'] = $path;
|
||||
}
|
||||
unset($validated['image']);
|
||||
|
||||
Reference in New Issue
Block a user