1
+ 'use strict' ;
2
+
3
+ const sharp = require ( 'sharp' ) ;
4
+ // eslint-disable-next-line ghost/ghost-custom/node-assert-strict
5
+ const assert = require ( 'assert' ) ;
6
+ const imageTransform = require ( '../../' ) ;
7
+ const path = require ( 'path' ) ;
8
+ const fixtures = require ( './fixtures' ) ;
9
+
10
+ const makeOutpath = ( inPath , mod , ext ) => {
11
+ if ( ! ext ) {
12
+ ext = path . extname ( inPath ) ;
13
+ }
14
+ const fileName = path . basename ( inPath , ext ) ;
15
+ return path . join ( __dirname , `fixtures/output/${ fileName } _${ mod } ${ ext } ` ) ;
16
+ } ;
17
+
18
+ describe ( 'Image compression' , function ( ) {
19
+ describe ( 'JPEG' , function ( ) {
20
+ let fixtureBuffer ;
21
+
22
+ before ( async function ( ) {
23
+ fixtureBuffer = await sharp ( fixtures . inputJpeg ) . toBuffer ( ) ;
24
+ } ) ;
25
+
26
+ it ( 'should always compress JPEG images' , async function ( ) {
27
+ const inPath = fixtures . inputJpeg ;
28
+ const outPath = makeOutpath ( inPath , 'base' , '.jpg' ) ;
29
+
30
+ await imageTransform . resizeFromPath ( { in : inPath , out : outPath } ) ;
31
+
32
+ await sharp ( outPath )
33
+ . toBuffer ( function ( err , result ) {
34
+ if ( err ) {
35
+ throw err ;
36
+ }
37
+ assert ( result instanceof Buffer ) ;
38
+ assert ( result . length < fixtureBuffer . length ) ;
39
+ } ) ;
40
+ } ) ;
41
+
42
+ it ( 'should compress JPEG images with width attribute' , async function ( ) {
43
+ const inPath = fixtures . inputJpeg ;
44
+ const outPath = makeOutpath ( inPath , '1000w' , '.jpg' ) ;
45
+
46
+ await imageTransform . resizeFromPath ( { in : inPath , out : outPath , width : 1000 } ) ;
47
+
48
+ await sharp ( outPath )
49
+ . toBuffer ( function ( err , result , info ) {
50
+ if ( err ) {
51
+ throw err ;
52
+ }
53
+ assert ( result instanceof Buffer ) ;
54
+ assert ( result . length < fixtureBuffer . length ) ;
55
+ assert ( info . width === 1000 ) ;
56
+ } ) ;
57
+ } ) ;
58
+
59
+ it ( 'can create a JPEG from another format by passing the format option' , async function ( ) {
60
+ const inputPngBuffer = await sharp ( fixtures . inputPng ) . toBuffer ( ) ;
61
+
62
+ const outputBuffer = await imageTransform . resizeFromBuffer ( inputPngBuffer , { format : 'jpeg' } ) ;
63
+
64
+ sharp ( outputBuffer ) . metadata ( ) . then ( function ( metadata ) {
65
+ assert . equal ( metadata . format , 'jpeg' ) ;
66
+ } ) ;
67
+ } ) ;
68
+ } ) ;
69
+
70
+ describe ( 'PNG' , function ( ) {
71
+ let fixtureBuffer ;
72
+
73
+ before ( async function ( ) {
74
+ fixtureBuffer = await sharp ( fixtures . inputPng ) . toBuffer ( ) ;
75
+ } ) ;
76
+
77
+ it ( 'should compress PNG images with width attribute' , async function ( ) {
78
+ const inPath = fixtures . inputPng ;
79
+ const outPath = makeOutpath ( inPath , '1000w' , '.png' ) ;
80
+
81
+ await imageTransform . resizeFromPath ( { in : inPath , out : outPath , width : 1000 } ) ;
82
+
83
+ await sharp ( outPath )
84
+ . toBuffer ( function ( err , result , info ) {
85
+ if ( err ) {
86
+ throw err ;
87
+ }
88
+ assert ( result instanceof Buffer ) ;
89
+ assert ( result . length < fixtureBuffer . length ) ;
90
+ assert ( info . width === 1000 ) ;
91
+ } ) ;
92
+ } ) ;
93
+
94
+ it ( 'can create PNG from another format by passing the format option' , async function ( ) {
95
+ const inputJpegBuffer = await sharp ( fixtures . inputJpeg ) . toBuffer ( ) ;
96
+
97
+ const outputBuffer = await imageTransform . resizeFromBuffer ( inputJpegBuffer , { format : 'png' } ) ;
98
+
99
+ sharp ( outputBuffer ) . metadata ( ) . then ( function ( metadata ) {
100
+ assert . equal ( metadata . format , 'png' ) ;
101
+ } ) ;
102
+ } ) ;
103
+ } ) ;
104
+
105
+ describe ( 'WEBP' , function ( ) {
106
+ let fixtureBuffer ;
107
+
108
+ before ( async function ( ) {
109
+ fixtureBuffer = await sharp ( fixtures . inputWebp ) . toBuffer ( ) ;
110
+ } ) ;
111
+
112
+ it ( 'should compress WEBP images with width attribute' , async function ( ) {
113
+ const inPath = fixtures . inputWebp ;
114
+ const outPath = makeOutpath ( inPath , '1000w' , '.webp' ) ;
115
+
116
+ await imageTransform . resizeFromPath ( { in : inPath , out : outPath , width : 1000 } ) ;
117
+
118
+ await sharp ( outPath )
119
+ . toBuffer ( function ( err , result , info ) {
120
+ if ( err ) {
121
+ throw err ;
122
+ }
123
+ assert ( result instanceof Buffer ) ;
124
+ assert ( result . length < fixtureBuffer . length ) ;
125
+ assert ( info . width === 1000 ) ;
126
+ } ) ;
127
+ } ) ;
128
+ } ) ;
129
+ } ) ;
0 commit comments