简体   繁体   English

将.jpg文件存储在本地存储中

[英]Storing .jpg files in local storage

I am trying to find a way to store .jpg files from my website into the localstorage to increase the site speed. 我试图找到一种方法将.jpg文件从我的网站存储到localstorage以提高网站速度。 Theoretical its simple: convert the picture.jpg into a base64 string and store it with setitem into the localstorage. 理论上很简单:将picture.jpg转换为base64字符串,并将其与setitem一起存储到localstorage中。 to display the picture again just load the base64 string from the localstorage and decode back to jpg. 再次显示图片只需从localstorage加载base64字符串并解码回jpg。 But, as always, the practice is more difficult. 但是,与往常一样,这种做法更加困难。 Im trying to find a way to convert the .jpg files on-the-fly to base64 using html5 or javascript (no php!). 我试图找到一种方法,使用html5或javascript(没有PHP!)将.jpg文件即时转换为base64。 Does someone had the same problem and was able to find a solution and could share the code? 有人有同样的问题,能够找到解决方案并可以共享代码吗?

I'm also for using HTML5 cache manifest which supports the offline case too and is designed for your problem. 我也使用HTML5缓存清单,它也支持离线情况 ,专为您的问题而设计。 Don't use local storage with base64 because: 不要在base64中使用本地存储,因为:

  • Base64 encoding increases the file size to 137% (!) Base64编码将文件大小增加到137%(!)
  • The algorithm will slow down your application, because not only the Internet Speed limits your application, also the javascript couldn't be executed as fast as on desktop computers. 该算法会降低您的应用程序速度,因为不仅Internet速度限制了您的应用程序,而且javascript无法像在桌面计算机上那样快速执行。 In my tests on moblie phones i had problems with common javascripts, so i would reduce the javascript to a minimum and your context isn't needed. 在我对移动电话的测试中,我遇到了常见的javascripts问题,所以我会将javascript降低到最低限度并且不需要你的上下文。
  • local storage isn't evertime supported and has also a limitation! 本地存储不是时间支持,也有限制!

For Cache Manifests you can look to w3.org - Cache Manifests also on html5 Rocks there is a beginner guide. 对于缓存清单,你可以查看w3.org - 缓存清单也在html5 Rocks上有一个初学者指南。

If you do not want to use HTML5 chache manifest, you should try to increase the speed as much as possible, described in many posts here on stackoverflow, i liked the link to the presentation in the post about increasing Math Object 如果你不想使用HTML5 chache manifest,你应该尽量提高速度,在stackoverflow上的很多帖子中有描述,我喜欢关于增加Math对象的帖子中的演示文稿的链接

You can use canvas element and toDataURL method where supported. 您可以在支持的地方使用canvas元素和toDataURL方法。 Something like that: 像这样的东西:

var ctx = canvas.getContext("2d");

var img = new Image();

img.onload = function() {
   canvas.width = this.width;
   cavans.height = this.height;

   ctx.drawImage(this, 0, 0);

   var base64jpeg = canvas.toDataURL("image/jpeg");
}

img.src = "/images/myjpeg.jpg";

But if you want to do that to "increase the site speed", use HTML5 manifest for caching: it was designed exactly for that purpose (and offline app, of course). 但是如果你想这样做“提高网站速度”,请使用HTML5清单进行缓存:它的设计完全是为了这个目的(当然还有离线应用程序)。

By the way, localStorage is better than browser cache. 顺便说一句, localStorage比浏览器缓存更好。 Google and Bing did some tests trying to see which caching method is faster and here are the results. Google和Bing做了一些测试,试图查看哪种缓存方法更快, 结果如下。 SPOILER ALERT!!!! SPOILER ALERT !!!! localStorage is better. localStorage更好。

通过创建缓存清单来使用HTML5缓存可能更好/更容易。

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

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