{"id":8498,"date":"2021-06-18T17:00:14","date_gmt":"2021-06-18T09:00:14","guid":{"rendered":"https:\/\/sdeno.com\/?p=8498"},"modified":"2021-06-18T17:00:14","modified_gmt":"2021-06-18T09:00:14","slug":"js%e5%a4%84%e7%90%86heicheif%e6%a0%bc%e5%bc%8f%e7%9a%84%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=8498","title":{"rendered":"js\u5904\u7406heic(heif)\u683c\u5f0f\u7684\u56fe\u7247"},"content":{"rendered":"<pre>import heic2any from 'heic2any';\r\n\r\nconst fileReader = new FileReader();\r\nconst fileReaderBuffer = new FileReader();\r\n\r\nconst file  \/\/ \u56fe\u7247\u6587\u4ef6\r\n\r\n\/\/ \u52a0\u8f7d\u56fe\u7247\r\nfunction loadImg(img) {\r\n  return new Promise((resolve, reject) =&gt; {\r\n    img.onload = (e) =&gt; {\r\n      resolve(e.target.result);\r\n    };\r\n    img.onerror = (e) =&gt; {\r\n      reject(e);\r\n    };\r\n  });\r\n}\r\n\r\n\/\/ \u538b\u7f29\u56fe\u7247\r\nfileReader.onload = async (e) =&gt; {\r\n  const base64 = e.target.result;\r\n  const img = new Image();\r\n  let imgData = null;\r\n  img.src = base64;\r\n  img.filename = file.name;\r\n  try {\r\n    await loadImg(img);\r\n    imgData = await compressImg(img);\r\n    resolve(imgData);\r\n  } catch (error) {\r\n    console.error(error);\r\n    resolve({\r\n      file: null,\r\n      url: ''\r\n    });\r\n  }\r\n};\r\n\r\n\/\/ \u8bfb\u53d6\u662f\u5426\u662fheic\u683c\u5f0f\u56fe\u7247\r\nfileReaderBuffer.onload = async () =&gt; {\r\n  const type = getFileType(fileReaderBuffer);\r\n  if (type === 'unknown') {\r\n    console.error('unknown image type');\r\n    resolve({\r\n      file: null,\r\n      url: ''\r\n    });\r\n    return;\r\n  }\r\n  if (type.includes('\/heic')) {\r\n    heic2any({ blob: file, toType: 'image\/jpeg' }).then((blob) =&gt; {\r\n      fileReader.readAsDataURL(blob);\r\n    }).catch(() =&gt; {\r\n      resolve({\r\n        file: null,\r\n        url: ''\r\n      });\r\n    });\r\n    return;\r\n  }\r\n  fileReader.readAsDataURL(file);\r\n};\r\n\r\nfileReaderBuffer.readAsArrayBuffer(file);<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import heic2any from &#8216;heic2any&#8217;; const fileReader = new [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-8498","post","type-post","status-publish","format-standard","hentry","category-11"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/8498","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8498"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/8498\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}