I have to upload image files that meet a max width dimension and max file size.
I have the code that checks width size and resizes the image to meet the max image w
Unfortunately, estimating final file size isn't something you can do as you have no access to the JPEG encoder's internal state.
One thing you can do is compress a smaller version of the image and extrapolate from there. Reducing the width and height by four means the computer has to deal with only one sixteenth of the pixels. The trick here is how to prepare a representative test image. Scaling the image down using imagecopyresampled() or imagecopyresized() won't work. It changes the compression characteristic too much. Instead, what you want to do is copy every fourth 8x8 tile from the original image:
$width1 = imagesx($img1);
$height1 = imagesy($img1);
$width2 = floor($width1 / 32) * 8;
$height2 = floor($height1 / 32) * 8;
$img2 = imagecreatetruecolor($width2, $height2);
for($x1 = 0, $x2 = 0; $x2 + 8 < $width2; $x1 += 32, $x2 += 8) {
for($y1 = 0, $y2 = 0; $y2 + 8 < $height2; $y1 += 32, $y2 += 8) {
imagecopy($img2, $img1, $x2, $y2, $x1, $y1, 8, 8);
}
}
Compress the smaller image at various quality levels and get the respected file sizes. Multiplying them by 16 to should yield a reasonably good estimate of what you would get with the original image.