How can I fit my image into box with known dimensions?

Here is an example that fits any uploaded image into 500x500 container:

See the Pen VmJwpq by uploadcare (@uploadcare) on CodePen.