@@ -142,7 +142,7 @@ class Image {
142
142
return JSON . stringify ( opts , function ( key , value ) {
143
143
// allows `transform` functions to be truthy for in-memory key
144
144
if ( typeof value === "function" ) {
145
- return "<fn>" ;
145
+ return "<fn>" + ( value . name || "" ) ;
146
146
}
147
147
return value ;
148
148
} ) ;
@@ -467,7 +467,7 @@ class Image {
467
467
}
468
468
}
469
469
470
- let stats = {
470
+ let statEntry = {
471
471
format : outputFormat ,
472
472
width : width ,
473
473
height : height ,
@@ -479,11 +479,11 @@ class Image {
479
479
} ;
480
480
481
481
if ( outputFilename ) {
482
- stats . filename = outputFilename ; // optional
483
- stats . outputPath = path . join ( this . options . outputDir , outputFilename ) ; // optional
482
+ statEntry . filename = outputFilename ; // optional
483
+ statEntry . outputPath = path . join ( this . options . outputDir , outputFilename ) ; // optional
484
484
}
485
485
486
- return stats ;
486
+ return statEntry ;
487
487
}
488
488
489
489
// https://jdhao.github.io/2019/07/31/image_rotation_exif_info/
@@ -531,7 +531,7 @@ class Image {
531
531
532
532
for ( let outputFormat of outputFormats ) {
533
533
if ( ! outputFormat || outputFormat === "auto" ) {
534
- throw new Error ( "When using statsSync or statsByDimensionsSync, `formats: [null | auto]` to use the native image format is not supported." ) ;
534
+ throw new Error ( "When using statsSync or statsByDimensionsSync, `formats: [null | ' auto' ]` to use the native image format is not supported." ) ;
535
535
}
536
536
537
537
if ( outputFormat === "svg" ) {
@@ -557,9 +557,7 @@ class Image {
557
557
} else { // not outputting SVG (might still be SVG input though!)
558
558
let widths = Image . getValidWidths ( metadata . width , this . options . widths , metadata . format === "svg" && this . options . svgAllowUpscale , this . options . minimumThreshold ) ;
559
559
for ( let width of widths ) {
560
- // Warning: if this is a guess via statsByDimensionsSync and that guess is wrong
561
- // The aspect ratio will be wrong and any height/widths returned will be wrong!
562
- let height = Math . floor ( width * metadata . height / metadata . width ) ;
560
+ let height = Image . getAspectRatioHeight ( metadata , width ) ;
563
561
564
562
results . push ( this . getStat ( outputFormat , width , height ) ) ;
565
563
}
@@ -569,6 +567,39 @@ class Image {
569
567
return this . #transformRawFiles( results ) ;
570
568
}
571
569
570
+ static getDimensionsFromSharp ( sharpInstance , stat ) {
571
+ let dims = { } ;
572
+ if ( sharpInstance . options . width > - 1 ) {
573
+ dims . width = sharpInstance . options . width ;
574
+ dims . resized = true ;
575
+ }
576
+ if ( sharpInstance . options . height > - 1 ) {
577
+ dims . height = sharpInstance . options . height ;
578
+ dims . resized = true ;
579
+ }
580
+
581
+ if ( dims . width || dims . height ) {
582
+ if ( ! dims . width ) {
583
+ dims . width = Image . getAspectRatioWidth ( stat , dims . height ) ;
584
+ }
585
+ if ( ! dims . height ) {
586
+ dims . height = Image . getAspectRatioHeight ( stat , dims . width ) ;
587
+ }
588
+ }
589
+
590
+ return dims ;
591
+ }
592
+
593
+ static getAspectRatioWidth ( originalDimensions , newHeight ) {
594
+ return Math . floor ( newHeight * originalDimensions . width / originalDimensions . height ) ;
595
+ }
596
+
597
+ static getAspectRatioHeight ( originalDimensions , newWidth ) {
598
+ // Warning: if this is a guess via statsByDimensionsSync and that guess is wrong
599
+ // The aspect ratio will be wrong and any height/widths returned will be wrong!
600
+ return Math . floor ( newWidth * originalDimensions . height / originalDimensions . width ) ;
601
+ }
602
+
572
603
getOutputSize ( contents , filePath ) {
573
604
if ( contents ) {
574
605
if ( this . options . svgCompressionSize === "br" ) {
@@ -643,12 +674,23 @@ class Image {
643
674
644
675
let sharpInstance = sharpInputImage . clone ( ) ;
645
676
let transform = this . options . transform ;
677
+ let isTransformResize = false ;
678
+
646
679
if ( transform ) {
647
680
if ( typeof transform !== "function" ) {
648
681
throw new Error ( "Expected `function` type in `transform` option. Received: " + transform ) ;
649
682
}
650
683
651
684
await transform ( sharpInstance ) ;
685
+
686
+ // Resized in a transform (maybe for a crop)
687
+ let dims = Image . getDimensionsFromSharp ( sharpInstance , stat ) ;
688
+ if ( dims . resized ) {
689
+ isTransformResize = true ;
690
+
691
+ // Overwrite current `stat` object with new sizes and file names
692
+ stat = this . getStat ( stat . format , dims . width , dims . height ) ;
693
+ }
652
694
}
653
695
654
696
// https://github.com/11ty/eleventy-img/issues/244
@@ -661,15 +703,19 @@ class Image {
661
703
if ( this . options . fixOrientation || this . needsRotation ( metadata . orientation ) ) {
662
704
sharpInstance . rotate ( ) ;
663
705
}
664
- if ( stat . width < metadata . width || ( this . options . svgAllowUpscale && metadata . format === "svg" ) ) {
665
- let resizeOptions = {
666
- width : stat . width
667
- } ;
668
- if ( metadata . format !== "svg" || ! this . options . svgAllowUpscale ) {
669
- resizeOptions . withoutEnlargement = true ;
670
- }
671
706
672
- sharpInstance . resize ( resizeOptions ) ;
707
+ if ( ! isTransformResize ) {
708
+ if ( stat . width < metadata . width || ( this . options . svgAllowUpscale && metadata . format === "svg" ) ) {
709
+ let resizeOptions = {
710
+ width : stat . width
711
+ } ;
712
+
713
+ if ( metadata . format !== "svg" || ! this . options . svgAllowUpscale ) {
714
+ resizeOptions . withoutEnlargement = true ;
715
+ }
716
+
717
+ sharpInstance . resize ( resizeOptions ) ;
718
+ }
673
719
}
674
720
675
721
// Format hooks take priority over Sharp processing.
0 commit comments