热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

为网站自动视网膜图像-AutomaticRetinaimagesforwebsites

WiththenewAppleMacBookProwithretinadisplay,ifyouprovideastandardimageonyourwebsit

With the new Apple MacBook Pro with retina display, if you provide a "standard" image on your website, it'll be a little fuzzy. So you have to provide a retina image.

如果你在你的网站上提供了一个“标准”的图片,那么你的网站上就会有一些模糊的东西。所以你需要提供视网膜图像。

Is there a way to automatically switch to @2x images, like iOS (with Objective-C) does? What I've found is: CSS for high-resolution images on mobile and retina displays, but I wish I could find an automatic process for all my images, without CSS or Javascript.

有没有一种方法可以自动切换到@2x图像,就像iOS(使用Objective-C)那样?我发现的是:在移动设备和视网膜上显示高分辨率图像的CSS,但是我希望我能找到一个自动处理所有图像的过程,不需要CSS或Javascript。

Is it possible?

是可能的吗?

UPDATE
I would emphasize this interesting article suggested by @Paul D. Waite and an interesting discussion about it linked by Sebastian.

更新我要强调这篇@Paul D. Waite建议的有趣文章和Sebastian链接的关于它的有趣讨论。

10 个解决方案

#1


112  

There is a new attribute for the img tag that allows you to add a retina src attribute, namely srcset. No Javascript or CSS needed, no double loading of images.

img标签有一个新属性,允许您添加retina src属性,即srcset。不需要Javascript或CSS,不需要重复加载图像。

Browser Support: http://caniuse.com/#search=srcset

浏览器支持:http://caniuse.com/搜索= srcset

Other Resources:

其他资源:

  • WebKit release post
  • WebKit版本发布
  • W3C documentation for srcset
  • W3C文档srcset
  • good explanation about why and how to use srcset
  • 关于为什么和如何使用srcset的很好的解释。
  • Chris Coyer's post for more good info
  • Chris Coyer的文章提供了更多的信息

#2


14  

There are different solutions, each with its own pros and cons. Which one is best for you depends on various factors, such as how your website is designed, what kind of technology your typical visitors are using etc. Note that retina displays are not limited to the Macbook Pro Retina and the coming iMacs, but also include mobile devices, which may have their own needs.

有不同的解决方案,每个都有自己的优点和缺点。哪一个最适合你取决于各种各样的因素,比如你的网站设计,典型的游客正在使用什么样的技术等等。注意,视网膜显示视网膜并不仅限于Macbook Pro和未来的imac,但也包括移动设备,这可能有他们自己的需求。

The problem is also closely related to images in responsive designs in general. In fact, it is probably best to utilize generic responsive design techniques, instead of designing for specific devices. After all, technology will keep changing all the time in the future, too.

这个问题也与一般的反应性设计中的图像密切相关。事实上,最好是利用通用的响应性设计技术,而不是为特定的设备设计。毕竟,技术在未来也会不断变化。

Some of the solutions/discussions I noted:

我注意到的一些解决办法/讨论:

  • Vectors wherever possible including CSS techniques (gradients, rounded corners etc.), SVG and icon fonts.
  • 向量尽可能包括CSS技术(渐变、圆角等)、SVG和图标字体。
  • Serving high resolution ("retina") images, but compress them more (JPEG quality), as suggested by Yoav Weiss, or let the mobile networks compress them when really needed (i.e. when mobile), as suggested by Paul Boag.
  • 提供高分辨率(“视网膜”)图像,但是像Yoav Weiss建议的那样压缩它们(JPEG质量),或者让移动网络在真正需要时(比如移动时)压缩它们,正如Paul Boag所建议的那样。
  • Adaptive Images, a (mostly) server side solution. It is based on a COOKIE storing the screen resolution, a web server configured to serve images from a PHP script, and named script to read the COOKIE and serve the appropriate image.
  • 自适应图像,一个(主要)服务器端解决方案。它基于一个存储屏幕分辨率的COOKIE,一个配置为从PHP脚本中提供图像的web服务器,以及读取COOKIE并提供适当图像的命名脚本。
  • A bunch of possibilities well described and discussed on Smashing Magazine.
  • 《Smashing》杂志很好地描述和讨论了一系列可能性。
  • Serving just slightly higher resolutions to smooth retina portrayal a little, as suggested in a video by Paul Boag.
  • 就像Paul Boag在一段视频中建议的那样,用稍微高一些的分辨率来描绘视网膜的图像。
  • The @1.5x technique on A List Apart is basically the same idea.
  • 清单上的@1.5x技术基本上是相同的。
  • In the near future, the tag may become a solution supported by a W3C working group and even Apple.
  • 在不久的将来, 标记可能成为W3C工作组甚至苹果支持的解决方案。
  • A Javascript technique proposed by Jake Archebald.
  • Jake Archebald提出的一种Javascript技术。
  • An extensive discussion of different techniques on Smashing Magazine and the problem in general.
  • 对粉碎杂志的不同技术和一般问题进行了广泛的讨论。

As the other answers show, there are even more techniques - but probably no best practice, yet.

正如其他答案显示的那样,还有更多的技术——但可能还没有最佳实践。

One thing I wonder is how to test and debug some of these techniques, without having the respective device(s) available...

我想知道的是,如何在没有相应设备可用的情况下测试和调试这些技术。

#3


10  

Here is the less mixin I use to achieve this for background images. retina.js doesn't work for background images if you are using dotLess, since it requires its own mixin which itself uses script evaluation which isn't supported in dotLess.

这里是我用于实现背景图像的较少的混合。视网膜。如果您正在使用dotLess,那么js不支持背景图像,因为它需要自己的mixin来使用dotLess不支持的脚本求值。

The trick with all of this is to get IE8 support. It can't easily do background-size so the base case (non mobile media query) has to be a simple, non-scaled icon. The media query then handles the case of retina and is free to use the background-size class since retina will never be used on IE8.

所有这些的诀窍在于获得IE8的支持。它不容易实现后台大小,所以基本情况(非移动媒体查询)必须是一个简单的、非缩放的图标。然后,媒体查询处理视网膜的情况,并且可以自由地使用背景尺寸类,因为视网膜永远不会在IE8上使用。

.retina-background-image( @path, @filename,@extension, @size )
{
     .background-size( cover );
     background-image: url( "@{path}@{filename}@{extension}" );
         @media only screen and ( -webkit-min-device-pixel-ratio: 2 ),
                only screen and ( -moz-min-device-pixel-ratio: 2 ),
                only screen and ( -o-min-device-pixel-ratio: 2/1 ),
                only screen and ( min-device-pixel-ratio: 2 )
         {
             background-image:url( "@{path}@{filename}@x2@{extension}" );
             background-size:@size @size;
         }
}

Usage sample:

使用示例:

.retina-background-image( "../references/Images/", "start_grey-97_12", ".png", 12px );

Ths requires you to have two files:

Ths要求你有两个文件:

  • start_grey-97_12.png
  • start_grey - 97 _12.png
  • start_grey-97_12@2x.png
  • start_grey - 97 _12@2x.png

Where the 2x file is double resolution for retina.

这里的2x文件是视网膜的双分辨率。

#4


9  

Since no one's mentioned the obvious yet, I'll float it out there: where possible, just use SVG. They appear at beautiful retina resolutions with no effort whatsoever.

由于还没有人提到这一点,所以我将它放在那里:尽可能使用SVG。它们出现在漂亮的视网膜分辨率上,毫不费力。

Support for it is good with IE8 being the main dinosaur to worry about. Gzipped file sizes are often better than bitmapped (png/jpg) formats and the images are more flexible; you can reuse them at different resolutions and restyle them if necessary, which saves both development time and download bandwidth.

对它的支持很好,IE8是主要的恐龙。压缩文件大小通常比位图(png/jpg)格式好,图像更灵活;您可以在不同的分辨率下重用它们,并在必要时重新设计它们的样式,这既节省了开发时间,也节省了下载带宽。

#5


6  

Just provide retina images to everyone, and squeeze the image to half its native size inside the image element. Like let's say your image is 400px wide and tall - just specify the image width as 200px to make it look sharp like this:

只需向每个人提供视网膜图像,并将图像压缩到图像元素内原有大小的一半。假设你的图片宽400px,高400px——只要将图片宽度指定为200px,让它看起来像这样:


If your image is photographic, you can probably increase the JPG compression on it without making it look worse, because the JPG compression artifacts probably won't be visible when the image is displayed at 2x: see http://blog.netvlies.nl/design-interactie/retina-revolution/

如果您的图像是照相的,那么您可以增加对它的JPG压缩,而不会使它看起来更糟,因为当图像显示为2x时,JPG压缩构件很可能是不可见的:请参见http://blog.netvlies.nl/design- revolution -interactie/retina- /

#6


1  

if its background images a simple way to do this is:

如果它的背景图像有一个简单的方法,那就是:

    #image { background: url(image.png); }

@media only screen and (-webkit-min-device-pixel-ratio: 2),
       only screen and (-moz-min-device-pixel-ratio: 2),
       only screen and (-o-min-device-pixel-ratio: 2/1),
       only screen and (min-device-pixel-ratio: 2) {
           #image { background: url(image@2x.png); background-size: 50%; }
}

another simple way is to use this method:

另一种简单的方法是使用这种方法:

Just replace:

只是替换:


with


#7


1  

I've found this interesting way for providing multiple resolution images.
It actually uses CSS, something I wanted to avoid, and works in Safari and Chrome only.
I'm talking about image-set.

我发现了提供多分辨率图像的有趣方式。它实际上使用了CSS,这是我想避免的,并且只在Safari和Chrome中使用。我说的是图像集。

Here's an example, provided by Apple (here):

苹果(Apple)提供了一个例子:

header {
    background: -webkit-image-set( url(images/header.jpg)    1x,
                                   url(images/header_2x.jpg) 2x);
    height: 150px; /* height in CSS pixels */
    width: 800px; /* width in CSS pixels */
}

I wanna share also these two links:

我也想分享这两个链接:

  • Safari 6 and Chrome 21 add image-set to support retina images
  • Safari 6和Chrome 21增加了支持视网膜图像的图像集
  • The image-set() notation @W3C
  • 图像集()符号@W3C

#8


1  

If you are not frustrated by fear of using java-script then here is a good article http://www.highrobotics.com/articles/web/ready-for-retina.aspx. It has very simple solution.

如果你没有因为害怕使用java脚本而感到沮丧,那么这里有一篇很好的文章:http://www.highrobotics.com/articles/web/press/a.aspx。它有很简单的解。

And the example in JSFiddle is worth a thousand words.

而JSFiddle的例子价值千言万语。

Using:

使用:


JS:

JS:

/* RETINA READY IMG SRC */
function getImgSrc(img, src) {
    var srcResult = src;
    // if high-res screen then change _x1 on _x2
    if (window.devicePixelRatio > 1 && 
        src.indexOf("_x1.")>=0) {
          srcResult = src.replace("_x1.", "_x2.");
    }
    img.Onload= null; //protect from second rasing
    img.src = srcResult;    
}

$(document).ready(function(){
  // fire onload trigger on IMG tags that have empty SRC attribute
  var images = $('img:not([src=""])');
    images.each(function(i) {
        $(this).trigger('onload');            
    });
});

#9


1  

My code snippet does just this. No jQuery required, just use the data-hdimg attribute.

我的代码片段就是这样做的。不需要jQuery,只需使用data-hdimg属性。

https://github.com/ajkochanowicz/High-Res-Images

https://github.com/ajkochanowicz/High-Res-Images

document.addEventListener 'DOMContentLoaded', ->
  HDimgs = document.querySelectorAll '[data-hdimg]'
  (
    image = new Image()
    image.Onload= ->
      console.info 'Image loaded'
      img.src = image.src
      img.style.width = "#{image.width / 2}px"
      img.removeAttribute 'data-hdimg'
    image.Onerror= ->
      console.error 'Could not load image'
    image.src = img.dataset.hdimg
  ) for img in HDimgs

#10


0  

This problem is especially tricky with responsive sites where and image can be of varying width dependant on browser size. Also when dealing with a CMS where multiple editors are potentially uploading 1000s of images it to me seemed unrealistic for me to ask people to upload specially compressed images.

对于响应性网站来说,这个问题尤其棘手,因为这些网站和图片的宽度取决于浏览器的大小。同时,当处理一个可能有多个编辑器上传上千张图片的CMS时,我觉得让人们上传特别压缩的图片是不现实的。

So I wrote a script that takes this into account, it fires at the bottom of the page and on resize finish. Each time taking into account pixel density and the size the image is occupying.

因此,我编写了一个脚本,考虑到这一点,它在页面底部触发并在resize finish上启动。每次考虑像素密度和图像占用的大小。

http://caracaldigital.com/retina-handling-code/

http://caracaldigital.com/retina-handling-code/


推荐阅读
  • 本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文介绍了使用AJAX的POST请求实现数据修改功能的方法。通过ajax-post技术,可以实现在输入某个id后,通过ajax技术调用post.jsp修改具有该id记录的姓名的值。文章还提到了AJAX的概念和作用,以及使用async参数和open()方法的注意事项。同时强调了不推荐使用async=false的情况,并解释了JavaScript等待服务器响应的机制。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 本文介绍了一些好用的搜索引擎的替代品,包括网盘搜索工具、百度网盘搜索引擎等。同时还介绍了一些笑话大全、GIF笑话图片、动态图等资源的搜索引擎。此外,还推荐了一些迅雷快传搜索和360云盘资源搜索的网盘搜索引擎。 ... [详细]
  • Webmin远程命令执行漏洞复现及防护方法
    本文介绍了Webmin远程命令执行漏洞CVE-2019-15107的漏洞详情和复现方法,同时提供了防护方法。漏洞存在于Webmin的找回密码页面中,攻击者无需权限即可注入命令并执行任意系统命令。文章还提供了相关参考链接和搭建靶场的步骤。此外,还指出了参考链接中的数据包不准确的问题,并解释了漏洞触发的条件。最后,给出了防护方法以避免受到该漏洞的攻击。 ... [详细]
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • Nginx使用AWStats日志分析的步骤及注意事项
    本文介绍了在Centos7操作系统上使用Nginx和AWStats进行日志分析的步骤和注意事项。通过AWStats可以统计网站的访问量、IP地址、操作系统、浏览器等信息,并提供精确到每月、每日、每小时的数据。在部署AWStats之前需要确认服务器上已经安装了Perl环境,并进行DNS解析。 ... [详细]
  • 本文介绍了Java工具类库Hutool,该工具包封装了对文件、流、加密解密、转码、正则、线程、XML等JDK方法的封装,并提供了各种Util工具类。同时,还介绍了Hutool的组件,包括动态代理、布隆过滤、缓存、定时任务等功能。该工具包可以简化Java代码,提高开发效率。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 生成对抗式网络GAN及其衍生CGAN、DCGAN、WGAN、LSGAN、BEGAN介绍
    一、GAN原理介绍学习GAN的第一篇论文当然由是IanGoodfellow于2014年发表的GenerativeAdversarialNetworks(论文下载链接arxiv:[h ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
author-avatar
杰仔
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有