以下是使用canvas实现的代码示例:

function cropImage(base64, x, y, width, height) {
  return new Promise((resolve, reject) => {
    const img = new Image();
    img.src = base64;
    img.onload = () => {
      const canvas = document.createElement("canvas");
      canvas.width = width;
      canvas.height = height;
      const ctx = canvas.getContext("2d");
      ctx.drawImage(img, x, y, width, height, 0, 0, width, height);
      const croppedBase64 = canvas.toDataURL();
      resolve(croppedBase64);
    };
    img.onerror = reject;
  });
}

使用示例:

const base64 = "data:image/png;base64,iVBORw0KG...";
const x = 10;
const y = 20;
const width = 100;
const height = 100;
cropImage(base64, x, y, width, height).then((croppedBase64) => {
  console.log(croppedBase64);
});
``
使用js实现输入base64图片原点坐标宽和高返回裁剪后的base64图片

原文地址: http://www.cveoy.top/t/topic/ck9F 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录