javascriptcropperjs

How to resize an image keeping aspect ratio with cropperjs


I'm trying to resize an image to a predefined size, keeping the aspect ratio of it. I've been using the following code to get the image data, but I don't know how to resize.

cropper.getCroppedCanvas().toDataURL('image/jpeg');

Solution

  • You can potentially use

    cropper.getCroppedCanvas({ width: DESIRED_WIDTH, height: DESIRED_HEIGHT}).toDataURL(ORIGINAL_MIME_TYPE);
    

    Its important to specify the original mime type of the image (png/gif etc) because otherwise you may end up with a significantly bigger image size in terms of file size.