Pure Javascript image handling library (in binary form, not through DOM)

后端 未结 4 1311
Happy的楠姐
Happy的楠姐 2021-01-07 01:32

Since File API will enable access to the content of local files it is now possible to do image resize before upload (a fairly common task) without any additional te

相关标签:
4条回答
  • 2021-01-07 02:17

    Even if you do find something that understands images in pure javascript, you would still need the DOM to render it, making it incredibly slow.

    0 讨论(0)
  • 2021-01-07 02:18

    Don't know if this is what you want, but there are some scripts on Userscripts.org that handle images: http://userscripts.org/scripts/show/38736

    0 讨论(0)
  • 2021-01-07 02:24

    Although I haven't find such libs, I have found a way how to accomplish the described task:

    • Read image as Data URL
    • Load an image into a canvas tag and resize it
    • Encode canvas image data into some image format, for example JPG. Or use Canvas.toDataUrl() and then (optionaly) some base64 decoder.

    Client side image resizing and upload with pure javascript. That's cool, isn't it?

    0 讨论(0)
  • 2021-01-07 02:26

    One problem with calavera.info's answer (sorry I cannot seem to comment directly on that answer) is that the call to either CanvasRenderingContext2D.getImageData or Canvas.toDataURL mentioned in the third bullet will fail. They each throw a SECURITY_ERR: DOM Exception 18 as the image is not from the same origin or domain as the document that owns the canvas element. That seems inevitable since the image comes from the local file system (via an input type="file" tag), but the page comes from your web server.

    0 讨论(0)
提交回复
热议问题