You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2 年之前
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # image-tools
  2. 图像转换工具,可用于如下环境:uni-app、微信小程序、5+APP、浏览器(需允许跨域)
  3. ## 使用方式
  4. ### NPM
  5. ```
  6. npm i image-tools --save
  7. ```
  8. ```js
  9. import { pathToBase64, base64ToPath } from 'image-tools'
  10. ```
  11. ### 直接下载
  12. ```js
  13. // 以下路径需根据项目实际情况填写
  14. import { pathToBase64, base64ToPath } from '../../js/image-tools/index.js'
  15. ```
  16. ## API
  17. ### pathToBase64
  18. 从图像路径转换为base64,uni-app、微信小程序和5+APP使用的路径不支持网络路径,如果是网络路径需要先使用下载API下载下来。
  19. ```js
  20. pathToBase64(path)
  21. .then(base64 => {
  22. console.log(base64)
  23. })
  24. .catch(error => {
  25. console.error(error)
  26. })
  27. ```
  28. ### base64ToPath
  29. 将图像base64保存为文件,返回文件路径。
  30. ```js
  31. base64ToPath(base64)
  32. .then(path => {
  33. console.log(path)
  34. })
  35. .catch(error => {
  36. console.error(error)
  37. })
  38. ```
  39. ## 提示
  40. 可以利用promise来串行和并行的执行多个任务
  41. ```js
  42. // 并行
  43. Promise.all(paths.map(path => pathToBase64(path)))
  44. .then(res => {
  45. console.log(res)
  46. // [base64, base64...]
  47. })
  48. .catch(error => {
  49. console.error(error)
  50. })
  51. // 串行
  52. paths.reduce((promise, path) => promise.then(res => pathToBase64(path).then(base64 => (res.push(base64), res))), Promise.resolve([]))
  53. .then(res => {
  54. console.log(res)
  55. // [base64, base64...]
  56. })
  57. .catch(error => {
  58. console.error(error)
  59. })
  60. ```