如何在 html 中以最佳格式优化和提供图像?

How to optimize and serve images in the best format in html?

我对以下图像格式和 html 标签有些困惑。

  1. 通过 and 的下一代图像格式(使用 webp 和 jpeg 图像)
  2. 使用数据:image/png;base64,blahblahblah(使用 svg 图像)

你能给我推荐最好的方法吗?

使用下一代格式:

<picture>
  <source srcset="img/Image.webp" type="image/webp">
  <source srcset="img/Image.jpg" type="image/jpeg"> 
  <img src="img/Image.jpg" alt="My image">
</picture>

使用数据 uri:

<img src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjwhLS0gR2VuZXJhdG9yOiBBZG9iZSBJbGx1c3RyYXRvciAxNi4wLjMsIFNWRyBFeHBvcnQgUGx1Zy1JbiAuIFNWRyBWZXJzaW9uOiA2LjAwIEJ1aWxkIDApICAtLT4NCjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+DQo8c3ZnIHZlcnNpb249IjEuMSIgaWQ9IkxheWVyXzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4Ig0KCSB3aWR0aD0iMTY0cHgiIGhlaWdodD0iMjYuODMzcHgiIHZpZXdCb3g9IjAgMCAxNjQgMjYuODMzIiBlbmFibGUtYmFja2dyb3VuZD0ibmV3IDAgMCAxNjQgMjYuODMzIiB4bWw6c3BhY2U9InByZXNlcnZlIj4NCjxwYXRoIGZpbGw9IiM1ODU4NUIiIGQ9Ik03LjA5OSwxOS4yMDFWNi42aDEuOGgwLjlsMC44OTksMC4xMDFsMC44MDEsMC4xMDFsMC4xOTksMC4xdi02bC0xLTAuMUw4Ljk5OSwwLjcwMWgtOC4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0LDE3LjRsMC42MDQtMC42MDJsMC41LTAuNjk3bDAuNS0xbDAuMjk5LTAuODAzbDAuMjAxLTAuNzk5di0wLjcwMXYtMC44bC0wLjIwMS0wLjhsLTAuMjk5LTAuODAxbC0wLjQtMC44MDFsLTAuNS0wLjY5OQ0KCWwtMC45LTAuNzk5TDEzMy4xMDQsNy43bC0wLjgwMS0wLjM5OUwxMzEuNSw3LjFMMTMwLjYsN0gxMzAuMlYwLjNoMC44bDEuMzAxLDAuMWwxLjE5NywwLjMwMWwxLjE5OSwwLjRsMS4xMDIsMC41bDEuMTAyLDAuNjAxDQoJbDEuMTAyLDAuOGwxLjEsMC44OThsMC44OTgsMWwwLjgwMSwxbDAuNjAyLDEuMTAxbDAuNSwxLjEwMWwwLjMwMSwxbDAuMSwwLjM5OVYwLjg5OWg2LjEwMmw4LjEwMiwxMnYtMTIuMWw2LjcwMSwwLjF2MjQuMjAyaC02DQoJTDE0OC45MDIsMTMuMnYxMS45MDFoLTYuNlYxNi40bC0wLjE5OSwwLjVsLTAuMzk4LDEuMTAybC0wLjYwMiwxLjFsLTAuNzAxLDFsLTAuNzk5LDFMMTM4LjcwNSwyMmwtMSwwLjgwMWwtMSwwLjY5OWwtMS4xMDIsMC42MDINCglsLTEuMjAxLDAuNUwxMzMuMTA0LDI1bC0xLjMwMSwwLjMwMWwtMS4xOTksMC4xSDEzMC4xTDEzMC4xLDI1LjR6Ii8+DQo8L3N2Zz4NCg=="
data-src="img/image.svg" alt="My image">

我们可以合并它们吗? 哪个选项最适合快速加载和完全浏览器兼容性? 我可能会错过任何其他选项。

Can we merge both of them?

是的,您可以在任何可以使用 URL 的地方使用 data: 方案 URL,包括 srcset 属性。

Which option is best for fast loading and full browser compatibility?

视情况而定。 Base64 编码 臃肿 并限制缓存但保存 HTTP 请求(使用 HTTP 时 1.x)。

当然,如果您内联您不使用的图像的替代版本,那将是非常低效的。