简体   繁体   English

根据窗口大小发送正确尺寸的图像

[英]Sending the right size of image depending on window size

I'm developing an image hosting website, mainly just to learn the ropes, and I'm using php and mysql, as well as html, css, and javascript. 我正在开发一个图片托管网站,主要是为了学习绳索,并且我正在使用php和mysql以及html,css和javascript。

As it is, on an image page, it loads up the full-size image, and the squishes it into a box in the layout. 照原样,它在图像页面上加载完整尺寸的图像,并将其挤压到布局的一个框中。 I get that that isn't the right thing to do; 我知道那不是正确的选择。 I should be sending the image that is already the right size. 我应该发送已经正确大小的图像。 But here's the trouble: the box that the image is resized to fit into is based on the browser window's size. 但这是麻烦所在:调整图像大小以适合其中的框是基于浏览器窗口的大小的。 So it's a larger image if the page is larger, and a smaller image if the page is smaller. 因此,如果页面较大,则图像较大;如果页面较小,则图像较小。

One solution I thought of would be to store the image in several different sizes, then use javascript on the page to determine the window size, then the box's size, and then use that information to choose the right image to load. 我想到的一种解决方案是将图像存储为几种不同的大小,然后在页面上使用javascript确定窗口大小,然后确定框的大小,然后使用该信息选择要加载的正确图像。 But all that inline javascript seems like it would slow things down a lot as well. 但是所有这些内联javascript似乎也会使速度变慢。

What's the best solution for this? 最好的解决方案是什么? What if the only thing I'm aiming for is speed? 如果我的唯一目标是速度怎么办? What if I have limited space to store multiple sizes of images? 如果我的存储空间有限,该怎么办? What if I have limited computing power? 如果我的计算能力有限怎么办?

I'm trying to learn about this stuff; 我正在尝试了解这些东西; not just implement it. 不只是实施它。 Indecisive answers that analyse various options would be great. 分析各种选项的优柔寡断的答案将是不错的选择。

You should be able to detect the window size in JavaScript before the document even loads, so the would really be no performance hit in assessing the window sizing and then loading the proper image (which would have been a second request anyway). 您应该能够在JavaScript加载之前就检测到JavaScript中的窗口大小,因此在评估窗口大小并随后加载适当的图像(无论如何会是第二次请求)时,这实际上不会对性能造成任何影响。 You should just check the window size, create a single PHP script which takes the desired image size, and then use that to pull in the proper image after the document is loaded by attaching arguments to the img src attribute. 您应该只检查窗口大小,创建一个单个的PHP脚本即可获取所需的图像大小,然后在通过将参数附加到img src属性来加载文档之后,使用该脚本来提取适当的图像。 Really no matter how you do it along those lines the performance impact will be insignificant since images are by default distinct requests and that will be your bottleneck. 实际上,无论您如何按照这些原则进行操作,性能影响都将是微不足道的,因为默认情况下图像是不同的请求,这将成为您的瓶颈。

If you have limited space and a lot of images you could use PHP to dynamically create the image per request (with adjusted caching). 如果空间有限且图像很多,则可以使用PHP为每个请求动态创建图像(调整缓存后)。 If you had limited computing power you'd want to cache more heavily or you could have the images pre-made if you have a small set of possibilities. 如果您的计算能力有限,那么您可能希望缓存更多,或者如果可能性很小,则可以预先制作图像。 Alternately you could have certain "milepost" types of images since image resampling with a large variance normally has to be done in multiple passes, so each milepost could allow for fewer passes to sample down to the desired exact size. 或者,您可能具有某些“里程碑”类型的图像,因为通常必须在多次通过中进行具有较大方差的图像重采样,因此每个里程碑都可以允许更少的通过次数来采样到所需的准确大小。

If transfer speed was theoretically a major concern you could also use image slicing. 如果从理论上讲传输速度是一个主要问题,您也可以使用图像切片。 Since most clients will allow for 2+ consecutive connections to the same host you could split the image if it was large enough to warrant it (offsetting the cost of request initiation) and then display the sections merged on the page (kind of like multiplexing). 由于大多数客户端都允许与同一主机建立2个以上的连续连接,因此如果图像足够大(可以保证请求启动的成本),则可以拆分图像(然后抵消页面的显示),然后在页面上显示合并的部分(有点像多路复用) 。

Ok so what you are trying to attack is speed. 好的,所以您要攻击的是速度。 Learning the "perfect" way is something to work up too. 学习“完美”的方法也是有待改进的。

A "perfect" solution, would be using a variety of images with different scales as uve described, or using vector based images that dont lose quality with scale. “完美”的解决方案将是使用如上所述的具有不同比例的各种图像,或者使用不会随比例丢失质量的基于矢量的图像。

This gets more into application architecture. 这更多地涉及到应用程序体系结构。

What you want is to learn though first are some basic javascript event handlers. 您首先要学习的是一些基本的javascript事件处理程序。

Learning the basics of dynamically resizing DOM elements is what you should look into. 了解如何动态调整DOM元素的大小的基础知识。

If you are going to be dynamically resizing the height and width of images, you should research the .bind('resize') event. 如果要动态调整图像的高度和宽度大小,则应研究.bind('resize')事件。

I will try this: 我会尝试的:

  • load a small image as a place holder 加载小图像作为占位符
  • use javascript to detect the window size 使用javascript检测窗口大小
  • make the ajax call to load the larger version 进行ajax调用以加载较大的版本
  • Save that screen size into a cookie or session variable 将屏幕尺寸保存到Cookie或会话变量中
  • call the following images base on that variable 根据该变量调用以下图像
  • detect resize event and update that variable 检测调整大小事件并更新该变量

On the server, prepare several size at the time of upload. 在服务器上,上载时准备几个大小。

When you say size do you mean in pixels or in kilobytes? 当您说大小时 ,您是指像素还是千字节?

If in pixels, put your image in a box and use css to manipulate the image size. 如果以像素为单位,则将图像放在一个框中,然后使用css调整图像大小。 Set the css property of the img tag to be fluid. 将img标签的css属性设置为流畅。 Use percentage based styling. 使用基于百分比的样式。

Example

.img_elem{ width: 100%; }

In html 在html中

<Div class"what-ever-ur-container-is">
     <Img class"img_elem" src"../path/to/file.jpg" />
</div>

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

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