Search code examples
phpresizejpegimage-resizing

Calculate image file size before saving to Disk in PHP


I am kind of experimenting with the PHP Image saving. I want to compare the image's actual file sizes before upload and after resize/compress processing, but want to know the file size that will going to be if I save it to the disk.

So that if the file size is less, then the newly processed file will replace old one, else no change will be made.

This is the code I am using to save jpeg image.

imagejpeg($this->newImage, $savePath, $imageQuality);

I tried getting the actual file size and size after

print_r(filesize($actual_file_size));
print_r($this->newImage);

where $actual_file_size gives the data by reading from tmp object file, however $this->newImage is giving blank (no value).

I am using ResizeImage class from github

Is there any way I can get the file size before saving to disk? So that I can keep the minimal size version.


Solution

  • There is no way to calculate the size before saving it to the disk. I advice you create some kind of temporary folder to save the image, then check the size and if it is what you want move it to the folder you see fit, if you dont want to save it, delete it from the temporary folder.