> we will only have "quality = 0" case now

the more I think about this, the more I think we should not try "quality = 0" as the file is too damaged with this. If at "quality = 0.25" we still exceed the limit, we should resize more.

> Only downsize the limit here may cause the infinity loop here. We need to have some another parameter to force the canvas size smaller then original computed size.

Yep, you're perfectly right here :) Probably the ratio (calculated using the limit) should be calculated only once, passed as a parameter to the function instead of or in addition to the limit, and we would then lower the ratio in this call then.