elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Estamos en la red social de Mastodon


+  Foro de elhacker.net
|-+  Programación
| |-+  Desarrollo Web
| | |-+  PHP (Moderador: #!drvy)
| | | |-+  subir y optimizar imagenes tarda demasiado
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: subir y optimizar imagenes tarda demasiado  (Leído 2,134 veces)
gAb1


Desconectado Desconectado

Mensajes: 731


Ver Perfil
subir y optimizar imagenes tarda demasiado
« en: 7 Mayo 2016, 21:50 pm »

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
  1. upload_max_filesize = 7M
  2. post_max_size = 60M
  3. memory_limit = 70M
  4. 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
  1. $tmp_name   = $_FILES['file']['tmp_name'];
  2. $img_size   = $_FILES['file']['size'];
  3.  
  4. for($i = 0; $i < count($tmp_name); $i++) {
  5.  
  6.    if ($tmp_name[$i] != '') {
  7.  
  8.        if ($img_size[$i] <= 7340032) {
  9.  
  10.            $imageInfo = getimagesize($tmp_name[$i]);
  11.            $memoryNeeded = round(($imageInfo[0] * $imageInfo[1] * $imageInfo['bits'] * $imageInfo['channels'] / 8 + Pow(2,16)) * 1.65);
  12.  
  13.            if (function_exists('memory_get_usage') && memory_get_usage() + $memoryNeeded (integer) ini_get('memory_limit') *pow(1024, 2)) {
  14.                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');
  15.            }
  16.  
  17.            $img_name   = '/' . ($i + 1) . '.jpg';
  18.            $thumb_name = '/thumbnail_' . ($i + 1) . '.jpg';
  19.  
  20.            list($width, $height) = getimagesize($tmp_name[$i]);
  21.  
  22.            if ($width >= 1100 && $height >= 650) {
  23.  
  24.                $img    = imagecreatefromjpeg($tmp_name[$i]);
  25.                $img_p  = imagecreatetruecolor(1100, 650);
  26.                $img_t  = imagecreatetruecolor(180, 180);
  27.                imagecopyresampled($img_p, $img, 0, 0, 0, 0, 1100, 650, $width, $height);
  28.                imagecopyresampled($img_t, $img_p, 0, 0, 0, 0, 180, 180, 1100, 650);
  29.  
  30.                if (!imagejpeg($img_p, $realpath . $img_name, 70) || !imagejpeg($img_t, $realpath . $thumb_name, 70)) {
  31.  
  32.                    $upload_err = TRUE;
  33.                    throw new Exception('Error al subir la imagen (' . $_FILES['file']['name'][$i] . ')');
  34.  
  35.                } else $log .= "\n" . 'IMG: ' . $img_name . ' (original name: ' . $_FILES['file']['name'][$i] . ') uploaded successfully.';
  36.  
  37.                imagedestroy($img);
  38.                imagedestroy($img_p);
  39.                imagedestroy($img_t);
  40.  
  41.            } else {
  42.                $upload_err = TRUE;
  43.                throw new Exception('La imagen: ' . $_FILES['file']['name'][$i] . ' debe tener como mínimo 1100px de ancho y 650px de alto.');
  44.            }
  45.        } else {
  46.            $upload_err = TRUE;
  47.            throw new Exception('La imagen (' . $_FILES['file']['name'][$i] . ') pesa más de 7 MB.');
  48.        }
  49.    }
  50. }

$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!


« Última modificación: 9 Mayo 2016, 11:40 am por gAb1 » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
optimizar y redimensionar imagenes
Java
imaginawireless 0 1,292 Último mensaje 27 Septiembre 2012, 18:54 pm
por imaginawireless
[SOLUCIONADO] Mi app tarda demasiado en iniciar « 1 2 »
.NET (C#, VB.NET, ASP)
Eleкtro 11 14,617 Último mensaje 10 Enero 2013, 02:25 am
por spiritdead
Porque cuando utilizo reaver para wpa en backtrack 5 tarda demasiado.
GNU/Linux
DuXGreck 6 7,332 Último mensaje 29 Abril 2013, 21:30 pm
por robriap
PC TARDA DEMASIADO EN ARRANCAR Y SE PETA
Hardware
LuisCardenas123 3 2,651 Último mensaje 7 Julio 2019, 23:58 pm
por nightcode
MOVIDO: PC TARDA DEMASIADO EN ARRANCAR Y SE PETA
Software
MCKSys Argentina 0 1,494 Último mensaje 6 Julio 2019, 16:12 pm
por MCKSys Argentina
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines