forked from GoogleChrome/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage-size-responsive.js
331 lines (306 loc) · 11.9 KB
/
image-size-responsive.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
* @license Copyright 2020 The Lighthouse Authors. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
/**
* @fileoverview Checks to see if the size of the visible images used on
* the page are large enough with respect to the pixel ratio. The
* audit will list all visible images that are too small.
*/
'use strict';
const Audit = require('./audit.js');
const URL = require('../lib/url-shim.js');
const i18n = require('../lib/i18n/i18n.js');
/** @typedef {LH.Artifacts.ImageElement & Required<Pick<LH.Artifacts.ImageElement, 'naturalDimensions'>>} ImageWithNaturalDimensions */
const UIStrings = {
/** Title of a Lighthouse audit that provides detail on the size of visible images on the page. This descriptive title is shown to users when all images have correct sizes. */
title: 'Serves images with appropriate resolution',
/** Title of a Lighthouse audit that provides detail on the size of visible images on the page. This descriptive title is shown to users when not all images have correct sizes. */
failureTitle: 'Serves images with low resolution',
/** Description of a Lighthouse audit that tells the user why they should maintain an appropriate size for all images. This is displayed after a user expands the section to see more. No character length limits. 'Learn More' becomes link text to additional documentation. */
description: 'Image natural dimensions should be proportional to the display size and the ' +
'pixel ratio to maximize image clarity. [Learn more](https://web.dev/serve-responsive-images/).',
/** Label for a column in a data table; entries in the column will be a string representing the displayed size of the image. */
columnDisplayed: 'Displayed size',
/** Label for a column in a data table; entries in the column will be a string representing the actual size of the image. */
columnActual: 'Actual size',
/** Label for a column in a data table; entries in the column will be a string representing the expected size of the image. */
columnExpected: 'Expected size',
};
const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings);
// Factors used to allow for smaller effective density.
// A factor of 1 means the actual device pixel density will be used.
// A factor of 0.5, means half the density is required. For example if the device pixel ratio is 3,
// then the images should have at least a density of 1.5.
const SMALL_IMAGE_FACTOR = 1.0;
const LARGE_IMAGE_FACTOR = 0.75;
// An image has must have both its dimensions lower or equal to the threshold in order to be
// considered SMALL.
const SMALL_IMAGE_THRESHOLD = 64;
/** @typedef {{url: string, elidedUrl: string, displayedSize: string, actualSize: string, actualPixels: number, expectedSize: string, expectedPixels: number}} Result */
/**
* @param {{top: number, bottom: number, left: number, right: number}} imageRect
* @param {{innerWidth: number, innerHeight: number}} viewportDimensions
* @return {boolean}
*/
function isVisible(imageRect, viewportDimensions) {
return (
(imageRect.bottom - imageRect.top) * (imageRect.right - imageRect.left) > 0 &&
imageRect.top <= viewportDimensions.innerHeight &&
imageRect.bottom >= 0 &&
imageRect.left <= viewportDimensions.innerWidth &&
imageRect.right >= 0
);
}
/**
* @param {{top: number, bottom: number, left: number, right: number}} imageRect
* @param {{innerWidth: number, innerHeight: number}} viewportDimensions
* @return {boolean}
*/
function isSmallerThanViewport(imageRect, viewportDimensions) {
return (
(imageRect.bottom - imageRect.top) <= viewportDimensions.innerHeight &&
(imageRect.right - imageRect.left) <= viewportDimensions.innerWidth
);
}
/**
* @param {LH.Artifacts.ImageElement} image
* @return {boolean}
*/
function isCandidate(image) {
/** image-rendering solution for pixel art scaling.
* https://developer.mozilla.org/en-US/docs/Games/Techniques/Crisp_pixel_art_look
*/
const artisticImageRenderingValues = ['pixelated', 'crisp-edges'];
// https://html.spec.whatwg.org/multipage/images.html#pixel-density-descriptor
const densityDescriptorRegex = / \d+(\.\d+)?x/;
if (image.displayedWidth <= 1 || image.displayedHeight <= 1) {
return false;
}
if (
!image.naturalDimensions ||
!image.naturalDimensions.width ||
!image.naturalDimensions.height
) {
return false;
}
if (image.mimeType === 'image/svg+xml') {
return false;
}
if (image.isCss) {
return false;
}
if (image.computedStyles.objectFit !== 'fill') {
return false;
}
// Check if pixel art scaling is used.
if (artisticImageRenderingValues.includes(image.computedStyles.imageRendering)) {
return false;
}
// Check if density descriptor is used.
if (densityDescriptorRegex.test(image.srcset)) {
return false;
}
return true;
}
/**
* Type check to ensure that the ImageElement has natural dimensions.
*
* @param {LH.Artifacts.ImageElement} image
* @return {image is ImageWithNaturalDimensions}
*/
function imageHasNaturalDimensions(image) {
return !!image.naturalDimensions;
}
/**
* @param {ImageWithNaturalDimensions} image
* @param {number} DPR
* @return {boolean}
*/
function imageHasRightSize(image, DPR) {
const [expectedWidth, expectedHeight] =
allowedImageSize(image.displayedWidth, image.displayedHeight, DPR);
return image.naturalDimensions.width >= expectedWidth &&
image.naturalDimensions.height >= expectedHeight;
}
/**
* @param {ImageWithNaturalDimensions} image
* @param {number} DPR
* @return {Result}
*/
function getResult(image, DPR) {
const [expectedWidth, expectedHeight] =
expectedImageSize(image.displayedWidth, image.displayedHeight, DPR);
return {
url: image.src,
elidedUrl: URL.elideDataURI(image.src),
displayedSize: `${image.displayedWidth} x ${image.displayedHeight}`,
actualSize: `${image.naturalDimensions.width} x ${image.naturalDimensions.height}`,
actualPixels: image.naturalDimensions.width * image.naturalDimensions.height,
expectedSize: `${expectedWidth} x ${expectedHeight}`,
expectedPixels: expectedWidth * expectedHeight,
};
}
/**
* Compute the size an image should have given the display dimensions and pixel density in order to
* pass the audit.
*
* For smaller images, typically icons, the size must be proportional to the density.
* For larger images some tolerance is allowed as in those cases the perceived degradation is not
* that bad.
*
* @param {number} displayedWidth
* @param {number} displayedHeight
* @param {number} DPR
* @return {[number, number]}
*/
function allowedImageSize(displayedWidth, displayedHeight, DPR) {
let factor = SMALL_IMAGE_FACTOR;
if (displayedWidth > SMALL_IMAGE_THRESHOLD || displayedHeight > SMALL_IMAGE_THRESHOLD) {
factor = LARGE_IMAGE_FACTOR;
}
const requiredDpr = quantizeDpr(DPR);
const width = Math.ceil(factor * requiredDpr * displayedWidth);
const height = Math.ceil(factor * requiredDpr * displayedHeight);
return [width, height];
}
/**
* Compute the size an image should have given the display dimensions and pixel density.
*
* @param {number} displayedWidth
* @param {number} displayedHeight
* @param {number} DPR
* @return {[number, number]}
*/
function expectedImageSize(displayedWidth, displayedHeight, DPR) {
const width = Math.ceil(quantizeDpr(DPR) * displayedWidth);
const height = Math.ceil(quantizeDpr(DPR) * displayedHeight);
return [width, height];
}
/**
* Remove repeated entries for the same source.
*
* It will keep the entry with the largest expected size.
*
* @param {Result[]} results
* @return {Result[]}
*/
function deduplicateResultsByUrl(results) {
results.sort((a, b) => a.url === b.url ? 0 : (a.url < b. url ? -1 : 1));
/** @type {Result[]} */
const deduplicated = [];
for (const r of results) {
const previousResult = deduplicated[deduplicated.length - 1];
if (previousResult && previousResult.url === r.url) {
// If the URL was the same, this is a duplicate. Keep the largest image.
if (previousResult.expectedPixels < r.expectedPixels) {
deduplicated[deduplicated.length - 1] = r;
}
} else {
deduplicated.push(r);
}
}
return deduplicated;
}
/**
* Sort entries in descending order by the magnitude of the size deficit, i.e. most pressing issues listed first.
*
* @param {Result[]} results
* @return {Result[]}
*/
function sortResultsBySizeDelta(results) {
return results.sort(
(a, b) => (b.expectedPixels - b.actualPixels) - (a.expectedPixels - a.actualPixels));
}
class ImageSizeResponsive extends Audit {
/**
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
id: 'image-size-responsive',
title: str_(UIStrings.title),
failureTitle: str_(UIStrings.failureTitle),
description: str_(UIStrings.description),
requiredArtifacts: ['ImageElements', 'ViewportDimensions'],
};
}
/**
* @param {LH.Artifacts} artifacts
* @return {LH.Audit.Product}
*/
static audit(artifacts) {
const DPR = artifacts.ViewportDimensions.devicePixelRatio;
const results = Array
.from(artifacts.ImageElements)
.filter(isCandidate)
.filter(imageHasNaturalDimensions)
.filter(image => !imageHasRightSize(image, DPR))
.filter(image => isVisible(image.clientRect, artifacts.ViewportDimensions))
.filter(image => isSmallerThanViewport(image.clientRect, artifacts.ViewportDimensions))
.map(image => getResult(image, DPR));
/** @type {LH.Audit.Details.Table['headings']} */
const headings = [
{key: 'url', itemType: 'thumbnail', text: ''},
{key: 'elidedUrl', itemType: 'url', text: str_(i18n.UIStrings.columnURL)},
{key: 'displayedSize', itemType: 'text', text: str_(UIStrings.columnDisplayed)},
{key: 'actualSize', itemType: 'text', text: str_(UIStrings.columnActual)},
{key: 'expectedSize', itemType: 'text', text: str_(UIStrings.columnExpected)},
];
const finalResults = sortResultsBySizeDelta(deduplicateResultsByUrl(results));
return {
score: Number(results.length === 0),
details: Audit.makeTableDetails(headings, finalResults),
};
}
}
/**
* Return a quantized version of the DPR.
*
* This is to relax the required size of the image.
* There's strong evidence that 3 DPR images are not perceived to be significantly better to mobile users than
* 2 DPR images. The additional high byte cost (3x images are ~225% the file size of 2x images) makes this practice
* difficult to recommend.
*
* Human minimum visual acuity angle = 0.016 degrees (see Sun Microsystems paper)
* Typical phone operating distance from eye = 12 in
*
* A
* _
* \ | B
* \|
* θ
* A = minimum observable pixel size = ?
* B = viewing distance = 12 in
* θ = human minimum visual acuity angle = 0.016 degrees
*
* tan θ = A / B ---- Solve for A
* A = tan (0.016 degrees) * B = 0.00335 in
*
* Moto G4 display width = 2.7 in
* Moto G4 horizontal 2x resolution = 720 pixels
* Moto G4 horizontal 3x resolution = 1080 pixels
*
* Moto G4 1x pixel size = 2.7 / 360 = 0.0075 in
* Moto G4 2x pixel size = 2.7 / 720 = 0.00375 in
* Moto G4 3x pixel size = 2.7 / 1080 = 0.0025 in
*
* Wasted additional pixels in 3x image = (.00335 - .0025) / (.00375 - .0025) = 68% waste
*
*
* @see https://www.swift.ac.uk/about/files/vision.pdf
* @param {number} dpr
* @return {number}
*/
function quantizeDpr(dpr) {
if (dpr >= 2) {
return 2;
}
if (dpr >= 1.5) {
return 1.5;
}
return 1.0;
}
module.exports = ImageSizeResponsive;
module.exports.UIStrings = UIStrings;