为什么Google PageSpeed会抱怨Retina图片?

9
我正在开发响应式网站,并使用代码为不同的屏幕大小加载正确尺寸的图片。但我遇到了一个问题,我在一些移动设备上使用Retina图像,这意味着客户端下载的图像尺寸更大。现在Google Pagespeed提醒我应该优化我的图像,但问题是如果我减小图像尺寸,那么Retina显示屏将会失去图像质量。请问Google Pagespeed支持Retina图像吗?有没有一种方法告诉Google Pagespeed这些是Retina图像?或者是否有Google关于Retina图像的最佳实践?
2个回答

0

如果您使用srcset属性,PageSpeed现在支持视网膜图像:

<img style="height: 100px" 
    src="rex_1x.png"
    srcset="rex_1x.png 1x, rex_2x.png 2x"
    alt="My dog Rex"/>

在上面的例子中,rex_1x.png 将是一个 100 x 100 像素的图片,而 rex_2x.png 将是一个 200 x 200 像素的图片。
使用这段代码,PageSpeed 不会抱怨该图片。

-1
我发现谷歌页面速度有时候不太可靠。有时候我测试一个得分是88/100的东西,然后5分钟后它就变成了95/100,但什么都没改。
我认为在使用谷歌页面速度时最好的做法是确保你:
1)有一个完美设计的标题,涵盖了谷歌喜欢看到的所有小细节。
<!DOCTYPE html>
<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html lang="en"> <!--<![endif]-->
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="description" content="" />
    <meta name="keywords" content="" />
    <meta name="author" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <!-- Favicons -->
    <link rel="shortcut icon" href="/img/icons/favicon.ico" type="image/x-icon">
    <link rel="icon" href="/img/icons/favicon.ico" type="image/x-icon">

    <!-- App Screen / Icons -->
    <!-- Specifying a Webpage Icon for Web Clip
        Ref: https://developer.apple.com/library/ios/documentation/AppleApplications/Reference/SafariWebContent/ConfiguringWebApplications/ConfiguringWebApplications.html -->
    <link rel="apple-touch-icon" href="/img/icons/sptouch-icon-iphone.png">
    <link rel="apple-touch-icon" sizes="76x76" href="/img/icons/touch-icon-ipad.png">
    <link rel="apple-touch-icon" sizes="120x120" href="/img/icons/touch-icon-iphone-retina.png">
    <link rel="apple-touch-icon" sizes="152x152" href="/img/icons/touch-icon-ipad-retina.png">

    <!-- iOS web-app metas : hides Safari UI Components and Changes Status Bar Appearance -->
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">

    <!-- Startup image for web apps -->
    <link rel="apple-touch-startup-image" href="/img/icons/ipad-landscape.png" media="screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:landscape)">
    <link rel="apple-touch-startup-image" href="/img/icons/ipad-portrait.png" media="screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:portrait)">
    <link rel="apple-touch-startup-image" href="/img/icons/iphone.png" media="screen and (max-device-width: 320px)">

    <!--
    HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries.
    WARNING: Respond.js doesn't work if you view the page via file://
    -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

2) 将任何静态图像的Base64编码直接嵌入其img标签中,这样您仍然可以在它们上使用缩放CSS代码,并且永远不必处理诸如缺少或设置不正确的过期标头之类的问题。(当您还在Apache服务器上运行Google mod_pagespeed时,这是一个问题。)

3) 在上传之前,无论其类型如何,都要压缩所有其他图像。

4) 在页面底部使用onload JavaScript函数来加载所有CSS和JS文件,如下所示。

window.onload = function(){
    function loadjscssfile(filename, filetype) {
        if(filetype == "js") {
            var cssNode = document.createElement('script');
            cssNode.setAttribute("type", "text/javascript");
            cssNode.setAttribute("src", filename);
        } else if(filetype == "css") {
            var cssNode = document.createElement("link");
            cssNode.setAttribute("rel", "stylesheet");
            cssNode.setAttribute("type", "text/css");
            cssNode.setAttribute("href", filename);
        }
        if(typeof cssNode != "undefined") {
            var h = document.getElementsByTagName("head")[0];
            h.parentNode.insertBefore(cssNode, h);
        }
    }
    loadjscssfile("//fonts.googleapis.com/css?family=Open+Sans:300&amp;subset=latin,cyrillic-ext,latin-ext,cyrillic,greek-ext,greek,vietnamese", "css");
    loadjscssfile("//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css", "css");
    loadjscssfile("/css/style.css", "css");
    loadjscssfile("//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js", "js");
    loadjscssfile("//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js", "js");
};

除了使用雅虎的Yslow进行测试之外,不要太过纠结。你会为了达到100/100而抓狂数天,但这些工具并不完美。尽力而为,将所学应用于下一个项目。


6
这是有用的信息,但它并没有回答关于视网膜图像以及Google PageSpeed为提供它们所评估的惩罚的问题。 - Matty B

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接