subir y optimizar imagenes tarda demasiado

Iniciado por gAb1, 7 Mayo 2016, 21:50 PM

0 Miembros y 1 Visitante están viendo este tema.

gAb1

Edito: Ahora el problema es que ocupa demasiado tiempo y se detiene la ejecución...

En el formulario permito subir como mucho 8 imagenes de 7MB máximo, así es como tengo configurado mi archivo .user.ini:

Código (php) [Seleccionar]
upload_max_filesize = 7M
post_max_size = 60M
memory_limit = 70M
max_execution_time = 60


Si intento subir 8 imagenes de 5.2MB se detiene la ejecución y da el error. Sin embargo se procesan 7 imagenes y sus 7 thumbnails (he probado comentando las lineas del thumbnail y tarda exactamente lo mismo, no hay diferencia entre crear las imagenes solo y creas ambos: las imagenes y thumbnails).
Si en lugar de 8 subo 7 (igual de 5.2MB) el script tarda, casi lo mismo, pero termina de ejeuctarse.

No soy un experto, pero a lo mejor mi script necesita mejorarse para reducir el tiempo... Este es parte del script que sube las imagenes:

Código (php) [Seleccionar]
$tmp_name   = $_FILES['file']['tmp_name'];
$img_size   = $_FILES['file']['size'];

for($i = 0; $i < count($tmp_name); $i++) {

   if ($tmp_name[$i] != '') {

       if ($img_size[$i] <= 7340032) {

           $imageInfo = getimagesize($tmp_name[$i]);
           $memoryNeeded = round(($imageInfo[0] * $imageInfo[1] * $imageInfo['bits'] * $imageInfo['channels'] / 8 + Pow(2,16)) * 1.65);

           if (function_exists('memory_get_usage') && memory_get_usage() + $memoryNeeded (integer) ini_get('memory_limit') *pow(1024, 2)) {
               ini_set('memory_limit', (integer) ini_get('memory_limit') + ceil(((memory_get_usage() + $memoryNeeded) - (integer) ini_get('memory_limit') * pow(1024, 2)) / pow(1024, 2)) . 'M');
           }

           $img_name   = '/' . ($i + 1) . '.jpg';
           $thumb_name = '/thumbnail_' . ($i + 1) . '.jpg';

           list($width, $height) = getimagesize($tmp_name[$i]);

           if ($width >= 1100 && $height >= 650) {

               $img    = imagecreatefromjpeg($tmp_name[$i]);
               $img_p  = imagecreatetruecolor(1100, 650);
               $img_t  = imagecreatetruecolor(180, 180);
               imagecopyresampled($img_p, $img, 0, 0, 0, 0, 1100, 650, $width, $height);
               imagecopyresampled($img_t, $img_p, 0, 0, 0, 0, 180, 180, 1100, 650);

               if (!imagejpeg($img_p, $realpath . $img_name, 70) || !imagejpeg($img_t, $realpath . $thumb_name, 70)) {

                   $upload_err = TRUE;
                   throw new Exception('Error al subir la imagen (' . $_FILES['file']['name'][$i] . ')');

               } else $log .= "\n" . 'IMG: ' . $img_name . ' (original name: ' . $_FILES['file']['name'][$i] . ') uploaded successfully.';

               imagedestroy($img);
               imagedestroy($img_p);
               imagedestroy($img_t);

           } else {
               $upload_err = TRUE;
               throw new Exception('La imagen: ' . $_FILES['file']['name'][$i] . ' debe tener como mínimo 1100px de ancho y 650px de alto.');
           }
       } else {
           $upload_err = TRUE;
           throw new Exception('La imagen (' . $_FILES['file']['name'][$i] . ') pesa más de 7 MB.');
       }
   }
}


$img_p es la imagen optimizada y $img_t es el thumbnail que necesito guardar tambien. No estoy seguro si hay alguna diferencia entre como esta ahora y llamar imagedestroy antes, por lo menos para la primera imagen que es la que pesa bastante (suelen pesar 4-6MB resolución 4k de la camara del movil/fotos).

Tal vez se puede optimizar mi script.

Gracias!