{"id":1175,"date":"2024-11-10T16:03:12","date_gmt":"2024-11-10T08:03:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1175\/"},"modified":"2024-11-10T16:03:12","modified_gmt":"2024-11-10T08:03:12","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%90%8e%e7%ab%af%e9%85%8d%e5%90%88%e5%89%8d%e7%ab%af%e5%ae%9e%e7%8e%b0%e5%9b%be%e7%89%87%e6%87%92%e5%8a%a0%e8%bd%bd%ef%bc%8c%e9%81%bf%e5%85%8d%e9%81%8d%e5%8e%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1175\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528\u540e\u7aef\u914d\u5408\u524d\u7aef\u5b9e\u73b0\u56fe\u7247\u61d2\u52a0\u8f7d\uff0c\u907f\u514d\u904d\u5386\u9875\u9762\u83b7\u53d6\u6240\u6709 img \u5143\u7d20\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173028470846986.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528\u540e\u7aef\u914d\u5408\u524d\u7aef\u5b9e\u73b0\u56fe\u7247\u61d2\u52a0\u8f7d\uff0c\u907f\u514d\u904d\u5386\u9875\u9762\u83b7\u53d6\u6240\u6709 img \u5143\u7d20\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528\u540e\u7aef\u914d\u5408\u524d\u7aef\u5b9e\u73b0\u56fe\u7247\u61d2\u52a0\u8f7d\uff0c\u907f\u514d\u904d\u5386\u9875\u9762\u83b7\u53d6\u6240\u6709 img \u5143\u7d20\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u56fe\u7247\u61d2\u52a0\u8f7d\u61d2\u4eba\u4e0d\u61d2<\/strong><\/p>\n<p>\u7ed9\u56fe\u7247\u6dfb\u52a0 data-src \u5c5e\u6027\u662f\u56fe\u7247\u61d2\u52a0\u8f7d\u7684\u5173\u952e\u6b65\u9aa4\u3002\u867d\u7136\u53ef\u4ee5\u904d\u5386\u9875\u9762\u83b7\u53d6\u6240\u6709 img \u5143\u7d20\uff0c\u4f46\u8fd9\u662f\u4e00\u79cd inefficient \u7684\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u63d2\u4ef6\u5316\u601d<\/strong><\/p>\n<p>\u4f60\u60f3\u8981\u5c01\u88c5\u6210\u63d2\u4ef6\uff0c\u6240\u4ee5\u9700\u8981\u4e00\u4e2a\u66f4\u6709\u901a\u7528\u6027\u7684\u89e3\u51b3\u65b9\u6848\u3002\u8003\u8651\u8ba9\u540e\u7aef\u670d\u52a1\u5668\u4f20\u8f93\u56fe\u7247\u7684\u539f\u59cb\u5c3a\u5bf8\uff08\u5bbd\u5ea6\u548c\u9ad8\u5ea6\uff09\uff0c\u7136\u540e\u4f7f\u7528 javascript \u52a8\u6001\u8ba1\u7b97\u5b9e\u9645\u663e\u793a\u5c3a\u5bf8\uff0c\u5e76\u4e3a\u672a\u52a0\u8f7d\u7684\u56fe\u7247\u8bbe\u7f6e data-src \u5c5e\u6027\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u540e\u7aef\u914d\u5408<\/strong><\/p>\n<p>\u5728\u540e\u7aef\uff0c\u4f60\u9700\u8981\u4f7f\u7528\u56fe\u50cf\u5904\u7406\u5e93\uff08\u6bd4\u5982 imagemagick \u6216 graphicsmagick\uff09\u83b7\u53d6\u56fe\u50cf\u7684\u539f\u59cb\u5c3a\u5bf8\u3002\u7136\u540e\uff0c\u5c06\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u4fe1\u606f\u901a\u8fc7 http \u54cd\u5e94\u5934\u6216 json \u683c\u5f0f\u7684\u6570\u636e\u8fd4\u56de\u7ed9\u6d4f\u89c8\u5668\u3002<\/p>\n<p><strong>\u524d\u7aef\u5904\u7406<\/strong><\/p>\n<p>\u5728\u524d\u7aef javascript \u4e2d\uff0c\u83b7\u53d6\u54cd\u5e94\u5934\u6216 json \u6570\u636e\uff0c\u5e76\u4f7f\u7528 javascript \u52a8\u6001\u8ba1\u7b97\u56fe\u7247\u7684\u5b9e\u9645\u663e\u793a\u5c3a\u5bf8\u3002\u5bf9\u4e8e\u672a\u52a0\u8f7d\u7684\u56fe\u7247\uff08\u5373\u5c1a\u672a\u8bf7\u6c42\u7684\u56fe\u7247\uff09\uff0c\u8bbe\u7f6e data-src \u5c5e\u6027\u503c\u4e3a\u8be5\u56fe\u7247\u7684\u5b8c\u6574 url\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b<\/strong><\/p>\n<pre>\/\/ \u7ed9\u5b9a\u540e\u7aef\u8fd4\u56de\u7684 HTTP \u54cd\u5e94\u5934\uff1a\"Image-Width: 1000px; Image-Height: 500px\"\n\n\/\/ \u540e\u7aef\u83b7\u53d6\u56fe\u50cf\u539f\u59cb\u5c3a\u5bf8\u5e76\u8fd4\u56de HTTP \u54cd\u5e94\u5934\nfunction getImgDimensions(imgElement) {\n  \/\/ \u5047\u8bbe imgElement \u7684 src \u5c5e\u6027\u4e3a \"img.jpg\"\n  var url = imgElement.src;\n  var xhr = new XMLHttpRequest();\n  xhr.open(\"HEAD\", url, true);\n  xhr.onload = function() {\n    if (xhr.status == 200) {\n      var width = xhr.getResponseHeader(\"Image-Width\");\n      var height = xhr.getResponseHeader(\"Image-Height\");\n      calculateActualSize(imgElement, width, height);\n    }\n  };\n  xhr.send();\n}\n\n\/\/ \u6839\u636e\u539f\u59cb\u5c3a\u5bf8\u8ba1\u7b97\u5b9e\u9645\u663e\u793a\u5c3a\u5bf8\nfunction calculateActualSize(imgElement, width, height) {\n  var parentWidth = imgElement.parentElement.clientWidth;\n  var parentHeight = imgElement.parentElement.clientHeight;\n\n  \/\/ \u8ba1\u7b97\u6bd4\u4f8b\n  var scale = Math.min(parentWidth \/ width, parentHeight \/ height);\n\n  \/\/ \u8bbe\u7f6e\u5b9e\u9645\u663e\u793a\u5c3a\u5bf8\n  imgElement.width = width * scale;\n  imgElement.height = height * scale;\n\n  \/\/ \u8bbe\u7f6e data-src \u5c5e\u6027\n  imgElement.dataset.src = imgElement.src;\n}\n\n\/\/ \u9875\u9762\u52a0\u8f7d\u65f6\u904d\u5386\u6240\u6709 img \u5143\u7d20\nwindow.addEventListener(\"load\", function() {\n  var imgElements = document.querySelectorAll(\"img\");\n  for (var i = 0; i &lt; imgElements.length; i++) {\n    getImgDimensions(imgElements[i]);\n  }\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u52a8\u6001\u5730\u4e3a\u6240\u6709\u56fe\u50cf\u6dfb\u52a0 data-src \u5c5e\u6027\uff0c\u800c\u65e0\u9700\u4fee\u6539 html \u4ee3\u7801\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528\u540e\u7aef\u914d\u5408\u524d\u7aef\u5b9e\u73b0\u56fe\u7247\u61d2\u52a0\u8f7d\uff0c\u907f\u514d\u904d\u5386\u9875\u9762\u83b7\u53d6\u6240\u6709 img \u5143\u7d20\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u56fe\u7247\u61d2\u52a0\u8f7d\u61d2\u4eba\u4e0d\u61d2 \u7ed9\u56fe\u7247\u6dfb\u52a0 data-src \u5c5e\u6027\u662f\u56fe\u7247\u61d2\u52a0\u8f7d\u7684\u5173\u952e\u6b65\u9aa4\u3002\u867d\u7136\u53ef\u4ee5\u904d\u5386\u9875\u9762\u83b7\u53d6\u6240\u6709 img \u5143\u7d20\uff0c\u4f46\u8fd9\u662f\u4e00\u79cd inefficient \u7684\u65b9\u6cd5\u3002 \u63d2\u4ef6\u5316\u601d \u4f60\u60f3\u8981\u5c01\u88c5\u6210\u63d2\u4ef6\uff0c\u6240\u4ee5\u9700\u8981\u4e00\u4e2a\u66f4\u6709\u901a\u7528\u6027\u7684\u89e3\u51b3\u65b9\u6848\u3002\u8003\u8651\u8ba9\u540e\u7aef\u670d\u52a1\u5668\u4f20\u8f93\u56fe\u7247\u7684\u539f\u59cb\u5c3a\u5bf8\uff08\u5bbd\u5ea6\u548c\u9ad8\u5ea6\uff09\uff0c\u7136\u540e\u4f7f\u7528 javascript \u52a8\u6001\u8ba1\u7b97\u5b9e\u9645\u663e\u793a\u5c3a\u5bf8\uff0c\u5e76\u4e3a\u672a\u52a0\u8f7d\u7684\u56fe\u7247\u8bbe\u7f6e data-src \u5c5e\u6027\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u540e\u7aef\u914d\u5408 \u5728\u540e\u7aef\uff0c\u4f60\u9700\u8981\u4f7f\u7528\u56fe\u50cf\u5904\u7406\u5e93\uff08\u6bd4\u5982 imagemagick \u6216 graphicsmagick\uff09\u83b7\u53d6\u56fe\u50cf\u7684\u539f\u59cb\u5c3a\u5bf8\u3002\u7136\u540e\uff0c\u5c06\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u4fe1\u606f\u901a\u8fc7 http \u54cd\u5e94\u5934\u6216 json \u683c\u5f0f\u7684\u6570\u636e\u8fd4\u56de\u7ed9\u6d4f\u89c8\u5668\u3002 \u524d\u7aef\u5904\u7406 \u5728\u524d\u7aef javascript \u4e2d\uff0c\u83b7\u53d6\u54cd\u5e94\u5934\u6216 json \u6570\u636e\uff0c\u5e76\u4f7f\u7528 javascript \u52a8\u6001\u8ba1\u7b97\u56fe\u7247\u7684\u5b9e\u9645\u663e\u793a\u5c3a\u5bf8\u3002\u5bf9\u4e8e\u672a\u52a0\u8f7d\u7684\u56fe\u7247\uff08\u5373\u5c1a\u672a\u8bf7\u6c42\u7684\u56fe\u7247\uff09\uff0c\u8bbe\u7f6e data-src \u5c5e\u6027\u503c\u4e3a\u8be5\u56fe\u7247\u7684\u5b8c\u6574 url\u3002 \u4ee3\u7801\u793a\u4f8b \/\/ \u7ed9\u5b9a\u540e\u7aef\u8fd4\u56de\u7684 HTTP \u54cd\u5e94\u5934\uff1a&#8221;Image-Width: 1000px; Image-Height: 500px&#8221; \/\/ \u540e\u7aef\u83b7\u53d6\u56fe\u50cf\u539f\u59cb\u5c3a\u5bf8\u5e76\u8fd4\u56de HTTP \u54cd\u5e94\u5934 function getImgDimensions(imgElement) { \/\/ \u5047\u8bbe imgElement \u7684 src [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-1175","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=1175"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1175\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}