简体   繁体   English

PHP生成的特定图像尺寸

[英]Specific image dimensions generated by PHP

As we know, if we specify width and height attributes in <img /> element, browser can immidiately locate image in rendered layout. 众所周知,如果在<img />元素中指定widthheight属性,则浏览器可以立即在渲染的布局中定位图像。 It's not a really big boost of speed, but it's really nice optimisation practice. 这并不是真正提高速度,但它确实是一种很好的优化实践。

Ok, but sometimes image has different dimensions. 好的,但是有时候图像有不同的尺寸。 So we can get it by PHP getimagesize() function. 因此,我们可以通过PHP getimagesize()函数来获取它。

And here is the question, if we don't have specific dimensions is it worthwhile to use PHP? 这就是问题,如果我们没有特定的尺寸,是否值得使用PHP? Of course, we have to write additional code, but if we cope with gallery we anyhow use PHP, so in that case it's only 2 lines of code. 当然,我们必须编写其他代码,但是如果要处理图库,无论如何我们都将使用PHP,因此在这种情况下,只有两行代码。

What is your opinion? 你有什么意见?

It depends on what you're trying to do. 这取决于您要执行的操作。 The benefits of having the dimensions on the <img/> tag are: <img/>标签上标注尺寸的好处是:

  1. Browser knows the space to reserve for the image as it's downloaded. 浏览器知道下载图像时要保留的空间。 This means your layout won't jump around as the images load. 这意味着您的布局不会在图像加载时跳来跳去。
  2. It saves the browser from performing a reflow and repaint once the images load (speed optimization). 一旦图像加载(速度优化),它将使浏览器免于执行重排和重绘。
  3. You'll have the dimensions available to any Javascript processing as soon as the HTML is finished loading. HTML加载完成后,您将拥有可用于所有Javascript处理的尺寸。

On the other hand, adding the dimensions will increase your page size and cause you to use more bandwidth. 另一方面,添加尺寸会增加页面大小,并导致您使用更多带宽。

You have to realize that getimagesize causes an I/O read (unless you're doing blob reads from db, then it's okay). 您必须意识到getimagesize会导致I / O读取(除非您正在从db中进行blob读取,否则就可以了)。 So, then you have doubled your I/O reads just for loading images. 因此,那么您已将仅用于加载图像的I / O读取增加了一倍。 Computers are generally fast enough that computing width and height of images for browsers, is not really much of an issue. 计算机通常足够快,以至于为浏览器计算图像的宽度和高度并不是什么大问题。

Then again, you have other venues of optimization: 再说一次,您还有其他优化的地方:

  • sprites 精灵
  • putting assets in CDNs instead of app 将资产放入CDN而不是应用中
  • validate your code 验证您的代码

I think generally, it's impractical to implement this, because it creates more inefficiencies that it optimizes. 我认为一般来说,实施此方法是不切实际的,因为它会产生更多无法优化的低效率问题。

Well I never bother. 好吧,我从来没有打扰。 And frankly most of the websites don't bother as well. 坦率地说,大多数网站也不会打扰。 The thing is that browser doesn't need to download the whole image to get it's dimensions. 事实是,浏览器不需要下载整个图像即可获得尺寸。 Most of the image formats have the image size in a header of the file. 大多数图像格式在文件的标题中都有图像大小。 so once browser got first few bytes of the file it already knows what the dimensions are. 因此,一旦浏览器获得了文件的前几个字节,它就已经知道尺寸了。

Function getimagesize() tries to parse image size from the file every time your PHP is called. 每次调用PHP时,函数getimagesize()都会尝试从文件中解析图像大小。 So basically instead of one disk seek you have two (plus parsing - if you parse some huge video file instead of image it could be literally seconds). 因此,基本上没有一个磁盘搜索,而是有两个磁盘(加上解析-如果您解析一些巨大的视频文件而不是图像,则可能需要几秒钟的时间)。

I personally have a CMS that reads image size only on image upload and then saves it in DB with other data, so no extra disk access is needed. 我个人有一个CMS,仅在上传图像时读取图像大小,然后将其与其他数据一起保存在DB中,因此不需要额外的磁盘访问。 Of course this might be overkill for you - YMMV. 当然,这对您来说可能算是过高了-YMMV。

Best advice: test it. 最佳建议:进行测试。 It is a simple test and it will give you the most relevant answer. 这是一个简单的测试,它将为您提供最相关的答案。

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

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