简体   繁体   English

图像需要一段时间才能加载

[英]Images take a while load

我有一个可以打印图像的PHP代码( echo"<img src='uploads/icon.png'/>"; ),问题是,第一次运行此代码时,img需要一秒钟才能出现,然后我刷新浏览器,图像立即显示,有没有办法在加载前将img缓存到浏览器?

You couldn't cache something that was not yet loaded, but what you could do is lazy-load the image (for example, using JQuery LazyLoad ), or even load a low-quality version of the image and then asynchronously load the full-quality image using client side JS. 您无法缓存尚未加载的内容,但是可以延迟加载图像(例如,使用JQuery LazyLoad ),甚至加载低质量版本的图像,然后异步加载完整的图像。使用客户端JS的高质量图像。

Either way, the image's gonna have to make it to the user before you can cache it, you can only control whether or not it will load in sync with the rest of the page. 无论哪种方式,图片都必须先交给用户,然后才能缓存它,您只能控制图片是否将与页面的其余部分同步加载。

There are many techniques how to speed up image loading. 有许多技术可以加快图像加载速度。 Depending on your scenario it could be 根据您的情况,可能是

  • image sprites 图像精灵
  • image compression and correct format depending on purpose 图像压缩和正确格式取决于目的
  • image preloading 图像预加载
  • async image loading 异步图片加载
  • use CDN 使用CDN

You could also use separate subdomain for images and other resources. 您还可以为图像和其他资源使用单独的子域。

You can use Leverage browser caching and Gzip compression which will help you much with the loading speed 您可以使用杠杆式浏览器缓存和Gzip压缩,这将大大帮助您加快加载速度

For leverage caching you can add below code to the .htaccess 对于杠杆缓存,您可以将以下代码添加到.htaccess中

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
## EXPIRES CACHING ##

And you can enable Gzip compression by adding below code to .htaccess 您可以通过将以下代码添加到.htaccess来启用Gzip压缩

<IfModule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text, XML and fonts
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml

  # Remove browser bugs (only needed for really old browsers)
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  Header append Vary User-Agent
</IfModule>

You can add them both and you will see a vast change in loading time. 您可以同时添加它们,并且加载时间会发生巨大变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM