I get the size of the images as they load. Imagick:
$im->getImageSize();
This returns the size in bytes of the image.
I would like to set the compression level automatically so that the file size never exceeds a certain size. If I wanted to limit it to 70 kb with a minimum acceptable compression level of 60 (on a scale from 0 to 100), I would start to do something like this:
public function getCompLevel($size) { $maxsize = 70000; // Set rough max size of file $mincomp = 60; // Set minimum compression level allowed if($size > $maxsize ){ // If file size exceeds max allowed size, perform calculation $comp = **EQUATION** } return ($comp < $mincomp) ? $mincomp : $comp; // if output is less than minimum allowed compression , return minimum. If not return calculated compression level }
What I'm trying to understand is the equation needed to calculate a close approximation of the required compression level based on file size. I understand that this may not be so accurate due to colors affecting the file size, but I would like to get as close as possible.
Any help would be greatly appreciated.
source share