简体   繁体   English

在图像密集型网页上的加载速度

[英]Load speed on an image-intensive webpage

I'm currently working on a portfolio site wherein I would have to load a large quantity of photos on the same page. 我目前在一个投资组合网站上工作,在那里我必须在同一页面上加载大量照片。

These images are loaded dynamically through PHP, and at the moment I have opted to save thumbnail versions of these images beforehand and load these. 这些图像是通过PHP动态加载的,目前,我选择了预先保存这些图像的缩略图版本并进行加载。

My concern, however, is that this might not be an optimal solution should the site have a large number of users: I would basically have duplicates of each image multiplied by the number of images users have uploaded. 但是,我担心的是,如果该站点有大量用户,这可能不是最佳解决方案:我基本上会将每个图像的重复数乘以用户上传的图像数。

My question for you is whether or not there are better solutions to this problem? 我的问题是,对于这个问题是否有更好的解决方案? A way to load a page as fast as possible without compromising too much space? 一种在不损害太多空间的情况下尽快加载页面的方法?

Thanks a ton. 万分感谢。

Loading a webpage full of lots of images will be slow. 加载包含大量图像的网页会很慢。 The reason for this is because of the bandwidth needed to transfer these images. 其原因是由于传输这些图像所需的带宽。

You have a couple of options 您有几种选择

  1. Load full images in tiled mode. 以平铺模式加载完整图像。 These images will be full images, just resized to fit in a "thumbnail" view. 这些图像将是完整图像,只是调整大小以适合“缩略图”视图。 The advantage of this is that you are only saving 1 image, but that image is full sized, and will take a long time to load. 这样做的好处是您只保存1张图像,但是该图像是全尺寸的,因此加载时间很长。

  2. Load the thumbnails as you said you are doing. 按照您说的那样加载缩略图。 The advantage to this is performance, but you need to store two copies of each image. 这样做的好处是性能,但是您需要为每个图像存储两个副本。 Also, depending on how you tackle the thumbnail creation, you may require users to upload two copies of the image to provide their own thumbnail... which could stink. 另外,根据您处理缩略图创建的方式,您可能会要求用户上传图像的两个副本以提供自己的缩略图...这可能会发臭。

  3. Load thumbnails, but dynamically generate them on upload. 加载缩略图,但在上传时动态生成缩略图。 You are essentially keeping two copies of the image on disk, but you are dynamically creating it through some php image modification API. 您实际上是在磁盘上保留映像的两个副本,但是您是通过一些php映像修改API动态创建的。 This will load faster, but still eats up disk space. 这样可以更快地加载,但是仍然会占用磁盘空间。 It also minimizes user/administrative requirements to provide a thumbnail. 它还最小化了提供缩略图的用户/管理要求。

  4. Load thumbnails on-demand, as the page is requested. 根据要求,按需加载缩略图。 This approach would take some testing, as I've never tried it. 这种方法需要进行一些测试,因为我从未尝试过。 Basically, you would invoke the php image modification API (or better yet out-source to a native solution!) to create a one-time-use (or cached) thumbnail to be used. 基本上,您将调用php图像修改API(或将其更好地外包给本机解决方案!)来创建要使用的一次性(或缓存的)缩略图。 You might say "OMG, that'll take so long!". 您可能会说“ OMG,这将花费很长时间!”。 I think this approach might actually be usable if you apply an appropriate caching mechanism so you aren't constantly recreating the same thumbnails. 我认为,如果您应用适当的缓存机制,那么这种方法实际上可能会有用,这样您就不会不断地重新创建相同的缩略图。 It will keep down on bandwidth, and since the limiting factor here is the network connection, it might be faster then just sending the full images (since the limiting factor of creating the thumbnails would now be CPU/Memory/Hard Disk). 它将限制带宽,并且由于这里的限制因素是网络连接,因此它可能比仅发送完整图像更快(因为创建缩略图的限制因素现在是CPU /内存/硬盘)。

I think #4 is an interesting concept, and might be worth exploring. 我认为#4是一个有趣的概念,可能值得探讨。

  1. Generate thumbnail of large images on-the-fly. 动态生成大图像的缩略图。 ( Some hint ) 一些提示
  2. Load images asynchronously (Try JAIL ) 异步加载图像(尝试JAIL
  3. Paginate 分页
  4. Caching is also an option, works from the second time the site is loaded though. 缓存也是一种选择,但是从第二次加载站点开始就可以使用。

What i think is : 我认为是:

A. Take Advantage of Cache (System Cache , Cache In Headers , HTTP Cache .. all Cache ) A.利用高速缓存(系统高速缓存,标头中的高速缓存,HTTP高速缓存..所有高速缓存)

B. Don't generate Thumb all the time B.不要一直产生Thumb

C. Use a Job Queing System such as Gearman or beanstalkd to generate thumbs so that you don't have to do it instantly C.使用诸如Gearmanbeanstalkd的Job Queing系统生成拇指,这样您就不必立即做

D. Use Imagick is more efficient D.使用Imagick更有效

E. Paginate E.分页

F. Example only generate thumb when the original file has been modified F.仅在修改原始文件后,示例才会生成Thumb

$file = "a.jpg" ;
$thumbFile = "a.thumb.jpg" ;
$createThumb = true;
if(is_file($thumbFile))
{
    if((filemtime($file) - 10) < filemtime($thumbFile));
    {
        $createThumb = false;
    }
}


if($createThumb === true)
{
    $thumb = new Imagick();
    $thumb->readImage($file);
    $thumb->thumbnailImage(50, null);
    $thumb->writeImage($thumbFile);
    $thumb->destroy();
}

Consider using a sprite or a collage of all the images, so that only one larger image is loaded, saving bandwidth and decreasing page load time. 考虑对所有图像使用精灵或拼贴,以便仅加载一个较大的图像,从而节省带宽并减少页面加载时间。

Also, as suggested already, pagination and and async loading can improve it some. 同样,正如已经建议的,分页和异步加载可以改善它。

References: 参考文献:

Yes , caching the thumbnails is a good idea, and will work out fine when done right. 是的 ,缓存缩略图是个好主意,如果操作正确,效果会很好。 This kind of thing is a great place for horizontal scaling . 这种东西是水平缩放的好地方。

  1. You should look into using a CDN . 您应该考虑使用CDN (Or possibly implementing something similar.) The caching system will generate images of commonly-requested sizes, and groom those off if they become infrequently requested at a later time. (或者可能实现类似的东西。)缓存系统将生成通常要求的大小的图像,并在以后不经常请求时将其整理掉。
  2. You could also scale horizontally and transplant this service to another server or vserver in your network or cloud. 您还可以水平扩展并将该服务移植到网络或云中的另一台服务器或虚拟服务器上。

It is a disk-intensive and bandwidth-intensive thing, image delivery. 它是磁盘密集型和带宽密集型的事物,即图像传递。 Not as bad as video! 不如视频差!

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

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