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?