If your goal is to get an image below a certain data length, it's hard to guess what compression ratio you need unless you know that the original image will always be a certain data length. Here is a simple iterative approach that uses jpeg compression to achieve the target length ... say 1 MB to ask a question:
I saw several approaches that use the while to compress the image by 0.9 or any other until the target size is reached, but I think that you will lose image quality and processor cycles by sequentially compressing / restoring the image. In addition, the for loop is a bit safer here, because it automatically stops after trying to compress as much as possible (zero).
source share