Skip to content

Commit 40702a5

Browse files
committed
Fixes #52 with transform function option.
1 parent 87f002d commit 40702a5

5 files changed

+55
-5
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"@eslint/js": "^9.17.0",
5454
"ava": "^6.2.0",
5555
"eslint": "^9.17.0",
56+
"exifr": "^7.1.3",
5657
"globals": "^15.14.0",
5758
"pixelmatch": "^5.3.0"
5859
},

src/global-options.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ const DEFAULTS = {
6161

6262
fixOrientation: false, // always rotate images to ensure correct orientation
6363

64-
// Removed, no longer necessary in v6.0
65-
// useCacheValidityInHash: true,
66-
6764
// When the original width is smaller than the desired output width, this is the minimum size difference
6865
// between the next smallest image width that will generate one extra width in the output.
6966
// e.g. when using `widths: [400, 800]`, the source image would need to be at least (400 * 1.25 =) 500px wide
@@ -76,7 +73,11 @@ const DEFAULTS = {
7673
// writes to the file system and speeding up builds!
7774
transformOnRequest: false,
7875

79-
// v5 `extensions` was removed (option to override output format with new file extension), it wasn’t being used anywhere or documented
76+
// operate on Sharp instance manually.
77+
transform: undefined,
78+
79+
// v5.0.0 Removed `extensions`, option to override output format with new file extension. It wasn’t being used anywhere or documented.
80+
// v6.0.0, removed `useCacheValidityInHash: true` see https://github.com/11ty/eleventy-img/issues/146#issuecomment-2555741376
8081
};
8182

8283
function getGlobalOptions(eleventyConfig, options, via) {

src/image.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,13 @@ class Image {
120120
opts.__originalSize = fs.statSync(this.src).size;
121121
}
122122

123-
return JSON.stringify(opts);
123+
return JSON.stringify(opts, function(key, value) {
124+
// allows `transform` functions to be truthy for in-memory key
125+
if (typeof value === "function") {
126+
return "<fn>";
127+
}
128+
return value;
129+
});
124130
}
125131

126132
getFileContents(overrideLocalFilePath) {
@@ -550,6 +556,15 @@ class Image {
550556
}
551557

552558
let sharpInstance = sharpImage.clone();
559+
let transform = this.options.transform;
560+
if(transform) {
561+
if(typeof transform !== "function") {
562+
throw new Error("Expected `function` type in `transform` option. Received: " + transform);
563+
}
564+
565+
await transform(sharpInstance);
566+
}
567+
553568
// Output images do not include orientation metadata (https://github.com/11ty/eleventy-img/issues/52)
554569
// Use sharp.rotate to bake orientation into the image (https://github.com/lovell/sharp/blob/v0.32.6/docs/api-operation.md#rotate):
555570
// > If no angle is provided, it is determined from the EXIF data. Mirroring is supported and may infer the use of a flip operation.

test/exif-sample-large.jpg

3.87 MB
Loading

test/transform-hooks-test.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const test = require("ava");
2+
const exifr = require("exifr");
3+
4+
const eleventyImage = require("../img.js");
5+
6+
test("Transforms Empty", async t => {
7+
let stats = await eleventyImage("./test/exif-sample-large.jpg", {
8+
formats: ["auto"],
9+
// transform: undefined,
10+
dryRun: true,
11+
});
12+
13+
let exif = await exifr.parse(stats.jpeg[0].buffer);
14+
t.deepEqual(exif, undefined);
15+
});
16+
17+
test("Transforms keep exif", async t => {
18+
let stats = await eleventyImage("./test/exif-sample-large.jpg", {
19+
formats: ["auto"],
20+
// Keep exif metadata
21+
transform: (sharp) => {
22+
sharp.keepExif();
23+
},
24+
dryRun: true,
25+
});
26+
27+
let exif = await exifr.parse(stats.jpeg[0].buffer);
28+
29+
t.is(Math.round(exif.latitude), 50);
30+
t.is(Math.round(exif.longitude), 15);
31+
t.is(exif.ApertureValue, 2);
32+
t.is(exif.BrightnessValue, 9.38);
33+
});

0 commit comments

Comments
 (0)