8
I have an image upload and to treat the images I’ve used (Imageworkshop and Imagine), however the problem is not the libraries, it is the absurd amount of memory that is used in some situations.
Tested images:
Imagem 01 (1920x1080 1,27MB JPG);
Imagem 02 (4134x2362 132KB PNG);
I got the following test code for upload (Imageworkshop library 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: Of a library for another does not change consumption much, are relatively similar...
I wonder what are the reasons for consuming this absurdly high amount of memory and what I could do to decrease that consumption?
If possible, you could formulate an example @Guilhermenascimento ?
– Rafael Withoeft
@Guilhermenascimento What I’m afraid of is, that in tests this happens... and further on there will be many... many requests that can come at the same time and end up bursting for the client... :/ I don’t know if it would really happen, but I want to be cautious of this situation...
– Rafael Withoeft
@Guilhermenascimento Great :) I would like to see an example, when you have a little time... I want to analyze right and try to implement here
– Rafael Withoeft