dowm41315 2015-01-11 14:54
浏览 32
已采纳

衡量客户端图像大小调整与带宽考虑因素的相对重要性

My site is using a php function to resize images so the browser doesn't have to do it:

function resizeImg($img, $newW, $newH, $rotateTrue) {
  //image resize code, then

  return base64_encode($image_data);
}

where the function is called like this:

<img src="data:image/png;base64,<?= resizeImg('myImg', newWidth, newHeight, 1); ?>" alt="pic">

No problems. However, the image data that is returned is sizeable. When I inspect the element in Chrome, > 1kB of encoding data is returned per resized image.

This is fine for a scattered image here and there, but what if you have a huge table? For example, one table on my site is > 1500 rows with one image per row. EACH image is currently resized by the browser, which I know is not ideal. However, if I resized every one of these 1500 images using my server-side resize function, I would add roughly this amount of encoded data:

1500 images x ~1.1 kB encoding data per image = 1650 kB

to this table.

For a high-performance site, which should I give more importance to, bandwidth savings (i.e., resize client side) or browser-savings (i.e., resize server-side)?

  • 写回答

2条回答 默认 最新

  • dsshsta97935 2015-01-11 15:03
    关注

    tl;dr BOTH

    The main issue with your approach is that you are open to very easy DDoS attacks by multiplying the computational impact of a page request to your server, since you are generating new images on the fly. Needless to say that the code you posted and the term high performance are not going well together because of this problem.

    Another issue is the fact that clients are not able to cache the images you sent them (unless you cache the complete webpage you delivered) since they are inlined via base64.

    The solution is simple. Resize the images to a set of predefined sizes and store them on your server. Deliver those various sizes to the client and let the client to the rest of the work. Of course you have to properly configure your server for client side caching of those image files.

    You could use the new <picture> element (picturefill for old browsers) on the client side to support multiple devices with different resolutions. Of course some scaling will happen but you cannot generate images for absolutely every device out there.


    If your question is only regarding upscaling, just don’t do it at all. Not on the server and not on the client side. Upscaled images simply look ugly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)