Foliotek / Croppie

A Javascript Image Cropper
http://foliotek.github.io/Croppie
MIT License
2.56k stars 885 forks source link

Upload and crop to fix size. #722

Open igramnet opened 4 years ago

igramnet commented 4 years ago

Hello. I what to upload images and crop it to size 720x450 px. If the size smaller the user should see a message. Can I do it?

bret-miller commented 4 years ago

I don't think Croppie will validate the size for you. If the image uploaded is smaller, it'll allow it to zoom bigger than the uploaded image to a point. Any empty area will be filled with the background color. I would suggest validating the size in the upload prior to passing the image to Croppie if you want to disallow smaller images.

igramnet commented 4 years ago

Ok. I have a window with width 500px. But I want to get the size - 720px (width). How can I do that?

bret-miller commented 4 years ago

As long as your viewport aspect ratio is correct, you can upload an image larger than the actual viewport size and it will scale to fit. Just make sure when you call Croppie to get the result, you specify the actual size you want.

Where cw is the container width and imgsize[0] is the desired width and imgsize[1] is the desired height. Load the image:

var vpw=parseInt(cw*0.8); var vph=parseInt(vpw/imgsize[0]*imgsize[1]); var img=$('#image-crop-container img').attr('src'); $('#image-crop-container img'). ({ url:img, enableOrientation:1, enforceBoundary:0, viewport:{width:vpw,height:vph,type:'square'} });

Get the result:

$('#image-crop-container img').croppie('result',{type:'base64',size:{width:imgsize[0],height:imgsize[1]},format:'png'}).then(function(r) { gciSaveImage(r); ; });