File tree 5 files changed +55
-5
lines changed
5 files changed +55
-5
lines changed Original file line number Diff line number Diff line change 53
53
"@eslint/js" : " ^9.17.0" ,
54
54
"ava" : " ^6.2.0" ,
55
55
"eslint" : " ^9.17.0" ,
56
+ "exifr" : " ^7.1.3" ,
56
57
"globals" : " ^15.14.0" ,
57
58
"pixelmatch" : " ^5.3.0"
58
59
},
Original file line number Diff line number Diff line change @@ -61,9 +61,6 @@ const DEFAULTS = {
61
61
62
62
fixOrientation : false , // always rotate images to ensure correct orientation
63
63
64
- // Removed, no longer necessary in v6.0
65
- // useCacheValidityInHash: true,
66
-
67
64
// When the original width is smaller than the desired output width, this is the minimum size difference
68
65
// between the next smallest image width that will generate one extra width in the output.
69
66
// 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 = {
76
73
// writes to the file system and speeding up builds!
77
74
transformOnRequest : false ,
78
75
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
80
81
} ;
81
82
82
83
function getGlobalOptions ( eleventyConfig , options , via ) {
Original file line number Diff line number Diff line change @@ -120,7 +120,13 @@ class Image {
120
120
opts . __originalSize = fs . statSync ( this . src ) . size ;
121
121
}
122
122
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
+ } ) ;
124
130
}
125
131
126
132
getFileContents ( overrideLocalFilePath ) {
@@ -550,6 +556,15 @@ class Image {
550
556
}
551
557
552
558
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
+
553
568
// Output images do not include orientation metadata (https://github.com/11ty/eleventy-img/issues/52)
554
569
// Use sharp.rotate to bake orientation into the image (https://github.com/lovell/sharp/blob/v0.32.6/docs/api-operation.md#rotate):
555
570
// > If no angle is provided, it is determined from the EXIF data. Mirroring is supported and may infer the use of a flip operation.
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments