HTML5 - resize image and keep EXIF in resized image

后端 未结 4 630
一生所求
一生所求 2020-12-16 01:31

How can I resize an image (using an HTML5 canvas element) and keep the EXIF information from the original image? I can extract EXIF info from from original imag

4条回答
  •  忘掉有多难
    2020-12-16 01:59

    You can use copyExif.js.

    This module is more efficient than Martin's solution and uses only Blob and ArrayBuffer without Base64 encoder/decoder.

    Besides, there is no need to use exif.js if you only want to keep EXIF. Just copy the entire APP1 marker from the original JPEG to the destination canvas blob and it would just work. It is also how copyExif.js does.

    Usage

    demo: https://codepen.io/tonytonyjan/project/editor/XEkOkv

    
    
    import copyExif from "./copyExif.js";
    
    document.getElementById("file").onchange = async ({ target: { files } }) => {
      const file = files[0],
        canvas = document.createElement("canvas"),
        ctx = canvas.getContext("2d");
    
      ctx.drawImage(await blobToImage(file), 0, 0, canvas.width, canvas.height);
      canvas.toBlob(
        async blob =>
          document.body.appendChild(await blobToImage(await copyExif(file, blob))),
        "image/jpeg"
      );
    };
    
    const blobToImage = blob => {
      return new Promise(resolve => {
        const reader = new FileReader(),
          image = new Image();
        image.onload = () => resolve(image);
        reader.onload = ({ target: { result: dataURL } }) => (image.src = dataURL);
        reader.readAsDataURL(blob);
      });
    };
    

提交回复
热议问题