Use of memory PHP images

7

I have an upload of images and to handle the images I have already used ( ImageWorkShop and #

Tested images:

Imagem 01 (1920x1080 1,27MB JPG); 
Imagem 02 (4134x2362 132KB PNG);

I have the following test code to upload (library ImageWorkShop is commented):

<?php

require 'vendor/autoload.php';

use PHPImageWorkshop\Core\Exception\ImageWorkshopLayerException;
use PHPImageWorkshop\Core\ImageWorkshopLayer;
use PHPImageWorkshop\Exception\ImageWorkshopException;
use PHPImageWorkshop\ImageWorkshop;
use Imagine\Image\Box;

if (isset($_FILES['imagem'])) {
    echo "Uso de memoria no inicio:".memory_get_usage(true)." bytes <br>";
    $file = $_FILES['imagem'];

    //$imagem = ImageWorkshop::initFromPath($file['tmp_name']);
    //$imagem->save( realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR .'images', sha1(uniqid(rand(), true)) . '.' . explode('/', $file['type'])[1], true, null, 70);
    //$imagemThumb = $imagem;
    //$imagemThumb->resizeInPixel(470, 350, true);
    //$imagemThumb->save(realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'images', sha1(uniqid(rand() , true)) . '_t.' . explode('/', $file['type'])[1], true, null, 70);

    $imagine = new Imagine\Gd\Imagine();

    $savePath = realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR;
    $extension = explode('/', $file['type'])[1];
    $name = sha1(uniqid(rand(), true));
    $options = [];

    switch($extension) {
        case 'png':
            $options['png_compression_level'] = 9;
            break;
        case 'jpg':
        case 'jpeg':
            $options['jpeg_quality'] = 70;
            break;
    }

    $imagine->open($file['tmp_name'])->save($savePath.$name. '.' .$extension, $options)->resize(new Box(470, 350))->save($savePath.$name . '_t' . '.' .$extension, $options);

    echo "Uso de memoria no final:".memory_get_usage(true)." bytes <br>";
    echo "O Pico de memoria:".memory_get_peak_usage(true)." bytes<br>";
}
?>

<html>
<body>
    <form enctype="multipart/form-data" method="POST">
        <input type="file" name="imagem" >
        <input type="submit" value="Enviar">
    </form>
</body>
</html>

Memory result "Image 01" :

Uso de memoria no inicio:262144 bytes (0.25 MB)
Uso de memoria no final:12320768 bytes (11.75 MB)
O Pico de memoria:13107200 bytes (12.5 MB)

Memory result "Image 02" :

Uso de memoria no inicio:262144 bytes (0.25 MB)
Uso de memoria no final:30670848 bytes (29.25 MB)
O Pico de memoria:90963968 bytes (86.75 MB)

OBS: From one library to another does not change much the consumption, they are relatively similar ...

I would like to know what are the reasons for consuming this absurdly high amount of memory and what could I do to reduce this consumption?

    
asked by anonymous 08.05.2015 / 20:22

1 answer

9

The consumption is high even though the reading of the image in memory is "decoded" and these decoded data are also in memory, we can say that it is almost impossible to reduce the peak of memory usage, but at the end of the process probably there must be a "destruct" method in these classes that you have used that you should facilitate.

Note that from PHP5.3 started using Garbage Collector / a>, therefore it manages the use itself, you will not be able to force to release the memory, but it can facilitate the life of the Collector of Garbage by setting NULL to some unused variables for example. When I have time I might try to formulate a more detailed answer.

  

Note: A personal experience, GD when loading the images consumed a lot of memory and images with < EXIF (even with no change in quality or size) caused memory consumption that was much higher than normal, which often exceeded the memory, causing the following error:

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 14032 bytes) in /var/www/project/thumb.php on line 10

What I recommend is to do the compression or generate the different types of sizes on the front end using tagged questions tagged "tagged / html5" 'html5' "> html5 /

08.05.2015 / 22:12