【问题标题】:Why is Google PageSpeed complaining about Retina images?为什么 Google PageSpeed 抱怨 Retina 图片?
【发布时间】:2014-08-19 16:36:45
【问题描述】:

我正在开发响应式网站。我有代码可以为屏幕尺寸加载正确的图像尺寸。

我面临的问题是我在某些移动设备上使用 Retina 图像。这意味着客户端下载的图像具有更大的尺寸。现在 Google pagespeed 现在抱怨说我应该优化我的图像。但问题是,如果我减小图像的尺寸,我会降低视网膜显示的质量。

Google Pagespeed 不支持 Retina 图片吗?有没有办法告诉 Google Pagespeed 这些是视网膜图像?

或者 Google 有没有针对 Retina 图片的最佳做法?

【问题讨论】:

    标签: image performance pagespeed


    【解决方案1】:

    我发现 Google PageSpeed 在最好的时候有点不稳定。我将测试结果为 88/100 的东西,然后 5 分钟后它是 95/100 并且没有任何变化。

    我认为 Google PageSpeed 的最佳做法是确保您:

    1) 有一个设计完美的标题,它涉及 Google 喜欢看到的所有小东西。

    <!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");
    };
    

    除了使用 Yahoo 的 Yslow 进行的测试之外,别担心。你会花几天时间努力达到 100/100,但这些工具并不完美。尽你最大的努力,把你学到的东西带到下一个项目中。

    【讨论】:

    • 这是有用的信息,但它不能回答有关视网膜图像的问题以及 Google PageSpeed 为提供这些图像而评估的惩罚。
    【解决方案2】:

    如果您使用 srcset attribute,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 不会抱怨图像。

    【讨论】:

      猜你喜欢
      • 2015-06-09
      • 2020-08-12
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      • 2018-11-17
      • 2022-11-08
      • 2019-07-18
      • 1970-01-01
      相关资源
      最近更新 更多