Error importing image: Allowed memory size

Asked

Viewed 464 times

3

I’m using a simple script to crop the image, while cropping some images it gives this error:

Allowed memory size of 268435456 bytes exhausted (tried to allocate 155648 bytes) in /home/site/public_html/admin/controller/photo.php on line 65

The strange thing is that the image is only 65kb, does anyone know what it might be? There’s a way I can generate a more detailed log?

below the script and the commented line 65:

//RECORTAR E IMPORTAR AS FOTOS NO FTP ****************************************
foreach($_FILES as $key => $ft){
    if($ft['tmp_name']!=''){
        $pastafoto = $root.'imagens/usuarios/';
        $photo_url = $ft['tmp_name'];
        list($width, $height, $type, $attr) = getimagesize($photo_url);
        $nomearquivo=$ft['name'];
        if($key=='foto'){
            $proporcao=1;
            $tamanho='150';
        }elseif($key=='capa'){
            $tamanho='1060';
            $proporcao=6.057142857142857;
        }

        if(!is_dir($pastafoto.$usuario['id'])){
            mkdir($pastafoto.$usuario['id'], 0755);
        }
        $dest = $pastafoto.$usuario['id'].'/' . $nomearquivo;
        $source_image = $photo_url;
        $nome=$nomearquivo;
        $destination = $pastafoto.$usuario['id'].'/'.$local.'/'.$nome;
        $tn_w = $tamanho;
        $tn_h = ($tamanho * $proporcao);
        $quality = 80;
        $wmsource = '';
        $success = image_handler($source_image,$destination,$tn_w,$tn_h,$quality,$wmsource);
        $sql = "UPDATE ptp_usuarios SET {$key}='{$nome}' WHERE id = {$usuario['id']}";
        $result = $db->prepare( $sql );
        $result->execute();
    }
}
//FUNÇÃO COPIA FOTO
function image_handler($source_image,$destination,$tn_w = 100,$tn_h = 100,$quality = 100,$wmsource = false) {
    $info = getimagesize($source_image);
    $imgtype = image_type_to_mime_type($info[2]);
    switch ($imgtype) {
        case 'image/jpeg':
        $source = imagecreatefromjpeg($source_image);
        break;
        case 'image/gif':
        $source = imagecreatefromgif($source_image);
        break;
        case 'image/png':
        $source = imagecreatefrompng($source_image);
        break;
        default:
        die('Invalid image type.');
    }
    $src_w = imagesx($source);
    $src_h = imagesy($source);
    $src_ratio = $src_w/$src_h;
    if ($tn_w/$tn_h > $src_ratio) {
        $new_h = $tn_w/$src_ratio;
        $new_w = $tn_w;
    } else {
        $new_w = $tn_h*$src_ratio;
        $new_h = $tn_h;
    }
    $x_mid = $new_w/2;
    $y_mid = $new_h/2;

    // Now actually apply the crop and resize!

//ESSA LINHA ABAIXO É A QUE FALA QUE DEU ERRO! ***********************************************

    $newpic = imagecreatetruecolor(round($new_w), round($new_h));
    imagecopyresampled($newpic, $source, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
    $final = imagecreatetruecolor($tn_w, $tn_h);
    imagecopyresampled($final, $newpic, 0, 0, ($x_mid-($tn_w/2)), ($y_mid-($tn_h/2)), $tn_w, $tn_h, $tn_w, $tn_h);
                    // If a watermark source file is specified, get the information about the watermark as well. This is the same thing we did above for the source image.
    if($wmsource) {
        $info = getimagesize($wmsource);
        $imgtype = image_type_to_mime_type($info[2]);
        switch ($imgtype) {
            case 'image/jpeg':
            $watermark = imagecreatefromjpeg($wmsource);
            break;
            case 'image/gif':
            $watermark = imagecreatefromgif($wmsource);
            break;
            case 'image/png':
            $watermark = imagecreatefrompng($wmsource);
            break;
            default:
            die('Invalid watermark type.');
        }
        // Determine the size of the watermark, because we're going to specify the placement from the top left corner of the watermark image, so the width and height of the watermark matter.
        $wm_w = imagesx($watermark);
        $wm_h = imagesy($watermark);
                        // Now, figure out the values to place the watermark in the bottom right hand corner. You could set one or both of the variables to "0" to watermark the opposite corners, or do your own math to put it somewhere else.
                        //$wm_x = $tn_w - $wm_w;
                        //$wm_y = $tn_h - $wm_h;
        $wm_x = ($tn_w / 2) - ($wm_w / 2);
        $wm_y = ($tn_h / 2) - ($wm_h / 2);
                        // Copy the watermark onto the original image
                        // The last 4 arguments just mean to copy the entire watermark
        imagecopy($final, $watermark, $wm_x, $wm_y, 0, 0, $tn_w, $tn_h);
    }
                    // Ok, save the output as a jpeg, to the specified destination path at the desired quality.
                    // You could use imagepng or imagegif here if you wanted to output those file types instead.
    if(Imagejpeg($final,$destination,$quality)) {
    return true;
    }
                    // If something went wrong
    return false;
}       
  • the answer helped you? If yes, try to schedule it to help other colleagues.

1 answer

2

Good morning, you can check your php.ini the directive memory_limit change to something around 1024M or more.

Some returns referring to memory limit:

PHP: Fatal Error: Allowed Memory Size of 8388608 Bytes Exhausted - 8 MB
PHP: Fatal Error: Allowed Memory Size of 16777216 Bytes Exhausted - 16 MB
PHP: Fatal Error: Allowed Memory Size of 33554432 Bytes Exhausted - 32 MB
PHP: Fatal Error: Allowed Memory Size of 67108864 Bytes Exhausted - 64 MB
PHP: Fatal Error: Allowed Memory Size of 134217728 Bytes Exhausted - 128 MB
PHP: Fatal Error: Allowed Memory Size of 268435456 Bytes Exhausted - 256 MB
PHP: Fatal Error: Allowed Memory Size of 536870912 Bytes Exhausted - 512 MB
PHP: Fatal Error: Allowed Memory Size of 1073741824 Bytes Exhausted - 1 GB

As you can see, yours is at 256MB.

You can also check these other directives that are also related to file uploads (but the first solution above already solves your problem):

upload_max_filesize
file_uploads
upload_max_filesize
max_file_uploads
post_max_size
max_input_time

Remembering that after making this change in your php.ini your apache must be restarted to work.

If you want to put the solution in your file use the code below. But I recommend changing php.ini.

<?php
    ini_set('memory_limit', '1024M'); // no caso usando 1G

I hope I’ve helped

  • i did, but now it appeared: Fatal error: Out of memory (allocated 884998144) (tried to allocate 155648 bytes) in /home/site/public_html/admin/controller/foto.php on line 65

  • @Leandromarzullo what is Qtd of your server’s RAM?

  • Okay, I can’t tell you, I can’t see that in php.ini ne? I use Cpanel, somewhere I can see?

  • @Leandromarzullo does the following, contact your hosting so you can check which is your RAM, sometimes it can be shared server and is giving you this problem. Another thing. changes the memory_limit directive to 128MB. Because I’m guessing your server doesn’t have much memory available. Using 128MB can solve for you.

  • but a question, in this message he’s not telling you that he has more space than he needs? allocated 884998144 - tried to allocate 155648 bytes, in theory it should not work?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.