Fatal error: Allowed memory size of 10485760 bytes exhausted (tried to allocate 2186 bytes)

0

That the people! I have this problem I already tried the php.ini and it does not work for me. Apparently I have some problem in the code but I can not find out. And I am very new in this. In the error, line 26 marks me, this would be

$original_imagen = imagecreatefromjpeg($temporal_imagen);

This is the rest of the code

$ruta = "img/articulos-img/";
$ruta_miniatura = $ruta."/miniaturas/";

function recortar($temporal,$imagen){
    global $ruta_miniatura;
    $temporal_imagen = $temporal;
    $nombre_imagen = $imagen;

    $extension_imagen = end(explode(".", $nombre_imagen));

    //Abrir la foto original
    if ($extension_imagen == "jpg"){
        $original_imagen = imagecreatefromjpeg($temporal_imagen);
    }else {
        $original_imagen = imagecreatefrompng($temporal_imagen);
    }

    $ancho_original =imagesx($original_imagen);
    $alto_original =imagesy($original_imagen);
    //Crear un lienzo vacio
    $copia_imagen = imagecreatetruecolor(400,424);

    //copiar original en el lienzo copia
    imagecopyresampled($copia_imagen, $original_imagen, 0, 0, 0, 0, 400, 424, $ancho_original, $alto_original);
    //Exportar imagen 

    if ($extension_imagen == "jpg"){
        imagejpeg($copia_imagen,$ruta_miniatura.$nombre_imagen,100);
    }else {
        imagepng($copia_imagen,$ruta_miniatura.$nombre_imagen,9);
    }

    return $ruta_miniatura.$nombre_imagen;
}

function rearrange( $arr ){
foreach( $arr as $key => $all ){
    foreach( $all as $i => $val ){
        $new[$i][$key] = $val;    
    }    
}
return $new;
}

if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_FILES)) {
    $fotos = rearrange($_FILES['fotos']);

    $miniatura = recortar($fotos[0]['tmp_name'],$fotos[0]['name']);

    $statement = $conexion->prepare('
        INSERT INTO articulos (titulo,descripcion,precio,descuento,miniatura)
         VALUES (:titulo,:descripcion,:precio,:descuento,:miniatura)
    ');

    $statement->execute(array(
        ':titulo' => $_POST['titulo'],
        ':descripcion' => $_POST['descripcion'],
        ':precio' => $_POST['precio'],
        ':descuento' => $_POST['descuento'],
        ':miniatura' => $miniatura
    ));

    $statement = $conexion->prepare('
        SELECT idArticulo FROM 'articulos' WHERE titulo = :titulo && precio= :precio
    ');

    $statement->execute(array(
        ':titulo' => $_POST['titulo'],
        ':precio' => $_POST['precio']
    ));

    $resultado = $statement->fetch();
    $idArticulo = $resultado['idArticulo'];





    for($i = 0; $i<count($fotos); $i++ ){
        if ($fotos[$i]['name']) {
            move_uploaded_file($fotos[$i]['tmp_name'], $ruta.$fotos[$i]['name']);
            $statement = $conexion->prepare('
                INSERT INTO imagenes (ruta,idArticulo)
                 VALUES (:ruta,:idArticulo)
            ');

            $statement->execute(array(
                ':ruta' => $ruta.$fotos[$i]['name'],
                ':idArticulo' => $idArticulo
            ));

        }

    }

Try some things but I can not find the solution. It happens to me with a lot of files, even if they are not exempt.

    
asked by Yamil Vernet 18.01.2018 в 08:54
source

1 answer

0

Using imagedestroy($copia_imagen); as said amenadiel was solved. It seems that the script uses a lot of memory.

Greetings and thanks !!

    
answered by 21.01.2018 / 20:16
source