Resize image to get a specific max. file size

Viewed 1753

I'm looking for a function that takes an image and which makes sure that the target file size is smaller than the given size, but as big as possible.

The reason is for creating a vcard the max. size of the photo is 224KB.
(Apple or vcard limitation: https://support.apple.com/en-us/HT202158)

I would like to have a function like this:

function (imageBase64, maxFileSize)
{
    // Check if image is smaller
    // If yes, return the old one.
    // If not, reduce image size proportional to fit in maxFileSize
    // return newImage
}

Do you have any hints how to solve it?

I would prefer to do this client side in js. But if it wouldn't be possible, I accept also server side php solutions.

1 Answers
Related