How to calculate the memory needed for image processing?

I want to put some restrictions on images uploaded by users, so the script that processes them is never exhausted.

Images that take up more memory are higher resolution images. They should not be large in bytes. For example, a 46 KB image with a resolution of 4000x2500 and some transparencies (PNG) took about 90 MB to resize it.

Is there a way to pre-calculate the required memory?

Any ideas?

+6
php memory image-processing
source share
2 answers

As a rule, the rule of thumb is: width x height x 4 (rgba) for both the source and target images, and do not forget about the amount of memory that the script itself has already used.

+3
source share

Using the GD library after downloading the file, you can use getimagesize () . You can then check the height and width to determine if you want to handle it or return an error, and also returns the number of bits used for each color, if you want to take that into account too.

Thus, your check may be related to the size and size of the files.

0
source share

All Articles