{"id":10021178,"date":"2026-01-09T09:21:12","date_gmt":"2026-01-09T01:21:12","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021178"},"modified":"2026-01-09T09:21:12","modified_gmt":"2026-01-09T01:21:12","slug":"%e6%8b%92%e7%bb%9d%e5%8d%a1%e9%a1%bf%ef%bc%81%e5%b0%8f%e7%a8%8b%e5%ba%8f%e5%9b%be%e7%89%87%e6%9c%ac%e5%9c%b0%e6%9e%81%e9%80%9f%e6%97%8b%e8%bd%ac%e4%b8%8e%e6%a0%bc%e5%bc%8f%e8%bd%ac","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021178","title":{"rendered":"\u62d2\u7edd\u5361\u987f\uff01\u5c0f\u7a0b\u5e8f\u56fe\u7247\u672c\u5730\u201c\u6781\u901f\u201d\u65cb\u8f6c\u4e0e\u683c\u5f0f\u8f6c\u6362\uff1aOffscreenCanvas \u6027\u80fd\u8c03\u4f18\u5b9e\u6218"},"content":{"rendered":"<p>\u5728\u5f00\u53d1\u5c0f\u7a0b\u5e8f\u56fe\u7247\u5de5\u5177\u65f6\uff0c\u6211\u4eec\u7ecf\u5e38\u9762\u4e34\u201c\u4e24\u96be\u201d\u5883\u5730\uff1a<\/p>\n<ol>\n<li><strong>\u7528\u6237\u4e0a\u4f20\u539f\u56fe<\/strong>\uff1a\u73b0\u4ee3\u624b\u673a\u62cd\u6444\u7684\u7167\u7247\u52a8\u8f84 4000&#215;3000 \u5206\u8fa8\u7387\uff0c\u5728 iOS \u8bbe\u5907\u4e0a DPR\uff08\u8bbe\u5907\u50cf\u7d20\u6bd4\uff09\u901a\u5e38\u4e3a 3\u3002<\/li>\n<li><strong>\u5185\u5b58\u7206\u70b8<\/strong>\uff1a\u5982\u679c\u76f4\u63a5\u6309\u539f\u56fe\u6e32\u67d3\uff0c\u753b\u5e03\u50cf\u7d20\u9ad8\u8fbe <code>(4000*3) * (3000*3) \u2248 1\u4ebf\u50cf\u7d20<\/code>\uff01\u8fd9\u8fdc\u8d85\u5c0f\u7a0b\u5e8f\u7684 Canvas \u5185\u5b58\u9650\u5236\uff0c\u5bfc\u81f4<strong>\u5fae\u4fe1\u5ba2\u6237\u7aef\u76f4\u63a5\u95ea\u9000<\/strong>\u3002<\/li>\n<li><strong>\u4f20\u7edf\u65b9\u6848\u5f0a\u7aef<\/strong>\uff1a\u4e0a\u4f20\u670d\u52a1\u5668\u5904\u7406\u8d39\u6d41\u91cf\u4e14\u6162\uff1b\u666e\u901a Canvas \u6e32\u67d3\u53c8\u5361\u987f\u754c\u9762\u3002<\/li>\n<\/ol>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u6253\u78e8\u51fa\u4e86\u4e00\u5957\u57fa\u4e8e <code>OffscreenCanvas<\/code> \u7684\u9ad8\u6027\u80fd\u672c\u5730\u5904\u7406\u65b9\u6848\uff0c\u6838\u5fc3\u5728\u4e8e\u201c<strong>\u667a\u80fd\u8ba1\u7b97\uff0c\u52a8\u6001\u964d\u7ea7<\/strong>\u201d\u3002<\/p>\n<h2>2. \u6838\u5fc3\u601d\u8def\uff1a\u79bb\u5c4f\u6e32\u67d3 + \u667a\u80fd\u9632\u7206<\/h2>\n<p>\u6211\u4eec\u7684\u65b9\u6848\u5305\u542b\u4e24\u4e2a\u5173\u952e\u6280\u672f\u70b9\uff1a<\/p>\n<ol>\n<li><strong>OffscreenCanvas\uff082D \u79bb\u5c4f\u753b\u5e03\uff09<\/strong>\uff1a<br \/>\u76f8\u6bd4\u4f20\u7edf Canvas\uff0c\u5b83\u5728\u5185\u5b58\u4e2d\u6e32\u67d3\uff0c\u4e0d\u5360\u7528 DOM\uff0c\u6ca1\u6709\u4efb\u4f55 UI \u5f00\u9500\uff0c\u7ed8\u56fe\u6307\u4ee4\u6267\u884c\u6781\u5feb\u3002<\/li>\n<li>\n<p><strong>\u667a\u80fd DPR \u9650\u5236\uff08\u6838\u5fc3\u9ed1\u79d1\u6280\uff09<\/strong>\uff1a<br \/>\u8fd9\u662f\u9632\u6b62\u95ea\u9000\u7684\u5173\u952e\u3002\u6211\u4eec\u5728\u7ed8\u5236\u524d\u8ba1\u7b97\u201c\u76ee\u6807\u753b\u5e03\u5c3a\u5bf8\u201d\u3002<\/p>\n<ul>\n<li><strong>\u5224\u65ad<\/strong>\uff1a\u5982\u679c <code>\u903b\u8f91\u5c3a\u5bf8 * \u7cfb\u7edfDPR<\/code> \u8d85\u8fc7\u4e86\u5b89\u5168\u9608\u503c\uff08\u5982 4096px\uff09\u3002<\/li>\n<li><strong>\u964d\u7ea7<\/strong>\uff1a\u5f3a\u5236\u964d\u4f4e\u4f7f\u7528\u7684 DPR \u503c\uff0c\u786e\u4fdd\u6700\u7ec8\u7eb9\u7406\u5c3a\u5bf8\u5728\u5b89\u5168\u8303\u56f4\u5185\u3002<\/li>\n<li><strong>\u7ed3\u679c<\/strong>\uff1a\u727a\u7272\u8089\u773c\u96be\u4ee5\u5bdf\u89c9\u7684\u6781\u5fae\u5c0f\u6e05\u6670\u5ea6\uff0c\u6362\u53d6 <strong>100% \u4e0d\u95ea\u9000<\/strong> \u7684\u7a33\u5b9a\u6027\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h2>3. \u786c\u6838\u4ee3\u7801\u5b9e\u73b0<\/h2>\n<p>\u4ee5\u4e0b\u662f<code>\u5e2e\u5c0f\u5fd9\u5de5\u5177\u7bb1<\/code>\u5c0f\u7a0b\u5e8f\u5c01\u88c5\u597d\u7684 <code>imageUtils.js<\/code> \u6838\u5fc3\u6e90\u4ee3\u7801\uff0c\u5305\u542b<strong>\u683c\u5f0f\u8f6c\u6362<\/strong>\u548c<strong>\u5e26\u9632\u7206\u903b\u8f91\u7684\u65cb\u8f6c<\/strong>\u529f\u80fd\u3002<\/p>\n<pre><code class=\"javascript\">\/\/ utils\/imageUtils.js\n\n\/\/ 1. \u83b7\u53d6\u7cfb\u7edf\u57fa\u7840\u4fe1\u606f\nconst wxt = {\n  dpr: wx.getSystemInfoSync().pixelRatio || 2\n};\n\n\/\/ 2. \u56fe\u7247\u5bf9\u8c61\u7f13\u5b58\u6c60\uff08\u907f\u514d\u91cd\u590d\u52a0\u8f7d\u540c\u4e00\u5f20\u56fe\uff09\nconst cacheCanvasImageMap = new Map();\n\n\/**\n * \u5185\u90e8\u65b9\u6cd5\uff1a\u83b7\u53d6\/\u521b\u5efa Canvas Image \u5bf9\u8c61\n *\/\nasync function getCanvasImage(canvas, imageUrl) {\n  if (cacheCanvasImageMap.has(imageUrl)) {\n    return cacheCanvasImageMap.get(imageUrl);\n  }\n  \n  \/\/ \u517c\u5bb9 Promise.withResolvers \u6216\u4f7f\u7528 new Promise\n  const { promise, resolve, reject } = Promise.withResolvers();\n  const image = canvas.createImage();\n  image.onload = () =&gt; {\n    cacheCanvasImageMap.set(imageUrl, image);\n    resolve(image);\n  };\n  image.onerror = (e) =&gt; reject(new Error(`\u56fe\u7247\u52a0\u8f7d\u5931\u8d25: ${e.errMsg}`));\n  image.src = imageUrl;\n  await promise;\n  return image;\n}\n\n\/**\n * \u529f\u80fd\u4e00\uff1a\u79bb\u5c4f Canvas \u8f6c\u6362\u56fe\u7247\u683c\u5f0f (PNG\/HEIC -&gt; JPG)\n * @param {string} imageUrl \u56fe\u7247\u8def\u5f84\n * @param {string} destFileType \u76ee\u6807\u7c7b\u578b 'jpg' | 'png'\n * @param {number} quality \u8d28\u91cf 0-1\n *\/\nexport async function convertImageType(imageUrl, destFileType = 'jpg', quality = 1) {\n  const offscreenCanvas = wx.createOffscreenCanvas({ type: '2d' });\n  const image = await getCanvasImage(offscreenCanvas, imageUrl);\n  const { width, height } = image;\n\n  \/\/ \u57fa\u7840\u8f6c\u6362\uff1a\u76f4\u63a5\u4f7f\u7528\u7cfb\u7edf DPR \u4fdd\u8bc1\u9ad8\u6e05\n  offscreenCanvas.width = width * wxt.dpr;\n  offscreenCanvas.height = height * wxt.dpr;\n\n  const ctx = offscreenCanvas.getContext('2d');\n  ctx.scale(wxt.dpr, wxt.dpr);\n  ctx.drawImage(image, 0, 0, width, height);\n\n  const res = await wx.canvasToTempFilePath({\n    canvas: offscreenCanvas,\n    fileType: destFileType,\n    quality: quality,\n  });\n  return res.tempFilePath;\n}\n\n\/**\n * \u529f\u80fd\u4e8c\uff1a\u6781\u901f\u65cb\u8f6c\u56fe\u7247 (\u542b\u5185\u5b58\u4fdd\u62a4)\n * @param {string} imageUrl \u56fe\u7247\u8def\u5f84\n * @param {number} degree \u65cb\u8f6c\u89d2\u5ea6 (90, 180, 270...)\n *\/\nexport async function rotateImage(imageUrl, degree = 90, destFileType = 'jpg', quality = 1) {\n  const offscreenCanvas = wx.createOffscreenCanvas({ type: '2d' });\n  const image = await getCanvasImage(offscreenCanvas, imageUrl);\n  const { width, height } = image;\n\n  const radian = (degree * Math.PI) \/ 180;\n  \n  \/\/ 1. \u8ba1\u7b97\u65cb\u8f6c\u540e\u7684\u903b\u8f91\u5305\u56f4\u76d2\u5bbd\u9ad8\n  const newWidth = Math.abs(width * Math.cos(radian)) + Math.abs(height * Math.sin(radian));\n  const newHeight = Math.abs(width * Math.sin(radian)) + Math.abs(height * Math.cos(radian));\n\n  \/\/ --- \u26a1\ufe0f \u6027\u80fd\u4f18\u5316\u6838\u5fc3 Start ---\n  \n  \/\/ 2. \u667a\u80fd\u8ba1\u7b97 DPR\uff1a\u907f\u514d\u753b\u5e03\u8fc7\u5927\u70b8\u5185\u5b58\n  \/\/ \u8bbe\u5b9a\u5b89\u5168\u7eb9\u7406\u9608\u503c\uff0c4096px \u662f\u5927\u591a\u6570\u79fb\u52a8\u7aef GPU \u7684\u5b89\u5168\u7ebf\n  const LIMIT_SIZE = 4096; \n  let useDpr = wxt.dpr;\n\n  \/\/ \u6838\u5fc3\u5224\u65ad\uff1a\u5982\u679c (\u903b\u8f91\u8fb9\u957f * dpr) \u8d85\u8fc7\u9650\u5236\uff0c\u81ea\u52a8\u8ba1\u7b97\u6700\u5927\u5141\u8bb8\u7684 dpr\n  if (Math.max(newWidth, newHeight) * useDpr &gt; LIMIT_SIZE) {\n    useDpr = LIMIT_SIZE \/ Math.max(newWidth, newHeight);\n    console.warn(`[ImageRotate] \u56fe\u7247\u8fc7\u5927\uff0c\u89e6\u53d1\u81ea\u52a8\u964d\u7ea7\uff0cDPR\u8c03\u6574\u4e3a: ${useDpr.toFixed(2)}`);\n  }\n\n  \/\/ 3. \u8bbe\u7f6e\u7269\u7406\u753b\u5e03\u5c3a\u5bf8 (\u4f7f\u7528\u8ba1\u7b97\u540e\u7684\u5b89\u5168 DPR)\n  offscreenCanvas.width = newWidth * useDpr;\n  offscreenCanvas.height = newHeight * useDpr;\n\n  const ctx = offscreenCanvas.getContext('2d');\n  ctx.scale(useDpr, useDpr); \n  \n  \/\/ --- \u6027\u80fd\u4f18\u5316\u6838\u5fc3 End ---\n\n  \/\/ 4. \u7ed8\u56fe\u903b\u8f91\uff1a\u5e73\u79fb -&gt; \u65cb\u8f6c -&gt; \u7ed8\u5236\n  ctx.translate(newWidth \/ 2, newHeight \/ 2);\n  ctx.rotate(radian);\n  ctx.drawImage(image, -width \/ 2, -height \/ 2, width, height);\n\n  \/\/ 5. \u5bfc\u51fa\u6587\u4ef6 \n  const res = await wx.canvasToTempFilePath({\n    canvas: offscreenCanvas,\n    fileType: destFileType,\n    quality: quality,\n  });\n\n  return res.tempFilePath;\n}<\/code><\/pre>\n<h2>4. \u907f\u5751\u4e0e\u5b9e\u6218\u7ecf\u9a8c<\/h2>\n<ol>\n<li><strong>\u56fe\u7247\u8f6cpdf\u573a\u666f\u7ecf\u9a8c<\/strong><br \/>\u56fe\u7247\u8f6c\u6210pdf\uff0c\u5728\u4f7f\u7528<code>pdf-lib<\/code>\u63d2\u5165\u56fe\u7247\u65f6\uff0c\u53ea\u652f\u6301jpg\u3001png\u5728\u63d2\u5165\u524d\u5148\u5224\u65ad\u4e00\u4e0b\u662f\u5426\u7b26\u5408\uff0c\u7528\u6237\u53ef\u80fd\u4e0a\u4f20webp\u7b49\u56fe\u7247\uff08\u6709\u4e9b\u4eba\u89c9\u5f97\u9650\u5236\u4e0a\u4f20\u7c7b\u578b\uff0c\u4f46\u56fe\u7247\u540e\u7f00\u6709\u53ef\u80fd\u88ab\u7be1\u6539\u8fc7\uff09\uff0c\u5c31\u9700\u8981\u5148\u8f6c\u6362\uff1b\u53e6\u5916\u5982\u679c\u8981\u4fdd\u8bc1pdf\u662f\u7eb5\u5411\u7684\uff0c\u4f7f\u7528canvas\u63d0\u524d\u786e\u4fdd\u56fe\u7247\u4e3a\u7eb5\u5411\u7684\uff0c\u5c31\u7b80\u5355\u5f88\u591a\uff0c\u65e0\u9700\u5728<code>pdf-lib<\/code>\u505a\u5750\u6807\u53d8\u6362<\/li>\n<li><strong>DPR \u7684\u53d6\u820d\u827a\u672f<\/strong>\uff1a<br \/>\u5f88\u591a\u5f00\u53d1\u8005\u559c\u6b22\u5199\u6b7b <code>offscreenCanvas.width = width<\/code>\uff0c\u8fd9\u6837\u5bfc\u51fa\u7684\u56fe\u662f\u6a21\u7cca\u7684\u3002\u4e5f\u6709\u4eba\u5199\u6b7b <code>width * systemDpr<\/code>\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u5927\u56fe\u95ea\u9000\u3002<br \/><strong>\u6700\u4f73\u5b9e\u8df5<\/strong>\u5c31\u662f\u4ee3\u7801\u4e2d\u7684 <code>Math.min<\/code> \u903b\u8f91\uff1a<strong>\u5728\u5b89\u5168\u8303\u56f4\u5185\uff0c\u5c3d\u53ef\u80fd\u9ad8\u6e05<\/strong>\u3002<\/li>\n<li>\n<p><strong>\u517c\u5bb9\u6027\u63d0\u793a<\/strong>\uff1a<br \/>\u4ee3\u7801\u4e2d\u4f7f\u7528\u4e86 <code>Promise.withResolvers()<\/code>\uff0c\u8fd9\u662f ES2024 \u65b0\u7279\u6027\u3002\u6211\u5168\u5c40\u5185\u7f6e\u517c\u5bb9\u4ee3\u7801\u3002<\/p>\n<pre><code class=\"js\">\/**\n * \u521b\u5efawithResolvers\u51fd\u6570\n *\/\nPromise.withResolvers =\nPromise.withResolvers ||\nfunction () {\n    let resolve, reject;\n    const promise = new Promise((res, rej) =&gt; {\n        resolve = res;\n        reject = rej;\n    });\n    return {\n        promise,\n        resolve,\n        reject,\n    };\n};<\/code><\/pre>\n<\/li>\n<\/ol>\n<h2>\u5199\u5728\u6700\u540e<\/h2>\n<p>\u901a\u8fc7\u8fd9\u4e00\u5957\u7ec4\u5408\u62f3\uff0c\u6211\u4eec\u6210\u529f\u5728\u5c0f\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u4e86\u7a33\u5b9a\u3001\u9ad8\u6548\u7684\u672c\u5730\u56fe\u7247\u5904\u7406\u3002\u65e0\u8bba\u7528\u6237\u4f7f\u7528\u51e0\u5e74\u524d\u7684\u5b89\u5353\u673a\u8fd8\u662f\u6700\u65b0\u7684 iPhone\uff0c\u90fd\u80fd\u6d41\u7545\u5730\u5b8c\u6210\u56fe\u7247\u65cb\u8f6c\u4e0e\u8f6c\u6362\uff0c\u518d\u4e5f\u4e0d\u7528\u62c5\u5fc3\u5185\u5b58\u6ea2\u51fa\u5e26\u6765\u7684\u95ea\u9000\u5669\u68a6\u4e86\uff01<\/p>\n<p>\u5e0c\u671b\u8fd9\u7bc7\u5b9e\u6218\u5206\u4eab\u80fd\u5e2e\u4f60\u89e3\u51b3 Canvas \u5f00\u53d1\u4e2d\u7684\u6027\u80fd\u96be\u9898\uff01<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047530812\">https:\/\/segmentfault.com\/a\/1190000047530812<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5f00\u53d1\u5c0f\u7a0b\u5e8f\u56fe\u7247\u5de5\u5177\u65f6\uff0c\u6211\u4eec\u7ecf\u5e38\u9762\u4e34\u201c\u4e24\u96be\u201d\u5883\u5730\uff1a \u7528\u6237\u4e0a\u4f20\u539f\u56fe\uff1a\u73b0\u4ee3\u624b\u673a\u62cd\u6444\u7684\u7167\u7247\u52a8\u8f84 4000&#215; [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021178","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021178"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021178\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}