@@ -2,48 +2,44 @@ const sharp = require('sharp');
2
2
const fs = require ( 'fs' ) ;
3
3
const path = require ( 'path' ) ;
4
4
const sizeOf = require ( 'image-size' ) ;
5
- const inputDir = path . join ( __dirname , '../static' , 'img' , 'blogposts' , 'full-size-images' ) ;
6
- const outputDir = path . join ( __dirname , '../static' , 'img' , 'blogposts' , 'resized-images' ) ;
5
+ const fullSizeDir = path . join ( __dirname , '../static' , 'img' , 'blogposts' , 'full-size-images' ) ;
6
+ const reducedSizeDir = path . join ( __dirname , '../static' , 'img' , 'blogposts' , 'resized-images' ) ;
7
7
const containerHeight = 180 ;
8
8
const containerWidth = 273 ;
9
- const containerAspectRatio = containerWidth / containerHeight ;
10
- console . log ( 'container Aspect Ratio:' , containerAspectRatio ) ;
11
9
12
- if ( ! fs . existsSync ( outputDir ) ) {
13
- fs . mkdirSync ( outputDir , { recursive : true } ) ;
10
+ if ( ! fs . existsSync ( reducedSizeDir ) ) {
11
+ fs . mkdirSync ( reducedSizeDir , { recursive : true } ) ;
14
12
}
15
13
16
- fs . readdirSync ( inputDir ) . forEach ( ( file ) => {
17
- const inputPath = path . join ( inputDir , file ) ;
18
- if ( fs . existsSync ( inputPath ) ) {
19
- const dimensions = sizeOf ( inputPath ) ;
20
-
21
- const aspectRatio = dimensions . width / dimensions . height ;
22
- const outputPath = path . join ( outputDir , file ) ;
23
- let targetWidth , targetHeight
24
- let width ;
25
- if ( aspectRatio <= containerAspectRatio ) {
26
- targetHeight = containerHeight
27
- targetWidth = Math . round ( targetHeight * aspectRatio ) ;
28
- } else {
29
- targetWidth = containerWidth * 2 ; /* factor 2 to increase the resolution*/
30
- targetHeight = Math . round ( targetWidth / aspectRatio ) ;
31
- }
32
-
33
- // Check that the image fits within the container
34
- if ( targetWidth > containerWidth * 2 ) {
35
- targetWidth = containerWidth * 2 ;
36
- targetHeight = Math . round ( targetWidth / aspectRatio ) ;
37
- }
14
+ function calculateImageSize ( fullSizePath ) {
15
+ if ( fs . existsSync ( fullSizePath ) ) {
16
+ const width = sizeOf ( fullSizePath ) . width ;
17
+ const height = sizeOf ( fullSizePath ) . height ;
18
+ let targetWidth , targetHeight ;
19
+ if ( width * containerHeight > containerWidth * height ) {
20
+ targetWidth = containerWidth * 2 ;
21
+ targetHeight = Math . round ( targetWidth / width * height ) ;
38
22
if ( targetHeight > containerHeight ) {
39
23
targetHeight = containerHeight ;
40
- targetWidth = Math . round ( targetHeight * aspectRatio ) ;
24
+ targetWidth = Math . round ( targetHeight * width / height ) ;
41
25
}
42
-
43
- if ( / \. ( p n g | j p g ) $ / i. test ( file ) ) {
44
- sharp ( inputPath )
45
- . resize ( targetWidth , targetHeight )
46
- . toFile ( outputPath )
47
26
}
27
+ else {
28
+ targetHeight = containerHeight ;
29
+ targetWidth = Math . round ( targetHeight / width * height ) ;
30
+ }
31
+ return [ targetWidth , targetHeight ] ;
32
+ }
33
+ }
34
+
35
+ fs . readdirSync ( fullSizeDir ) . forEach ( ( file , index ) => {
36
+ console . log ( index ) ;
37
+ const fullSizePath = path . join ( fullSizeDir , file ) ;
38
+ const reducedSizePath = path . join ( reducedSizeDir , file ) ;
39
+ const [ targetWidth , targetHeight ] = calculateImageSize ( fullSizePath ) ;
40
+ if ( / \. ( p n g | j p g ) $ / i. test ( file ) ) {
41
+ sharp ( fullSizePath )
42
+ . resize ( targetWidth , targetHeight )
43
+ . toFile ( reducedSizePath )
48
44
}
49
- } ) ;
45
+ } )
0 commit comments