jamesssooi / Croppr.js

A vanilla JavaScript image cropper that's lightweight, awesome, and has absolutely zero dependencies.
https://jamesooi.design/Croppr.js
MIT License
396 stars 91 forks source link

export #31

Open graficzny-chlopak opened 5 years ago

graficzny-chlopak commented 5 years ago

How to export a cut picture?

ryanb commented 3 years ago

Croppr doesn't crop the image, it just provides you with the coordinates of the crop rectangle. It's up to you to do the cropping which could be done on the server side or the client side.

Here's an example of doing it on the client side with an HTML canvas.

const sourceImage = document.getElementById("sourceImage");
const croppr = new Croppr(sourceImage, {});

// When finished cropping
const cropRect = croppr.getValue();
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
canvas.width = cropRect.width;
canvas.height = cropRect.height;
context.drawImage(
  croppr.imageEl,
  cropRect.x,
  cropRect.y,
  cropRect.width,
  cropRect.height,
  0,
  0,
  canvas.width,
  canvas.height,
);
const destinationImage = document.getElementById("destinationImage");
destinationImage.src = canvas.toDataURL();

You can do whatever else you want with the data url, such as put it in a hidden field or send it to the server over an AJAX request.