@@ -11,20 +11,15 @@ const Themes = require('./themes.js');
1111const Utils = require ( './helpers/utils.js' ) ;
1212const slug = require ( './helpers/slug' ) ;
1313const Jimp = require ( 'jimp' ) ;
14-
15- let sharp ;
16-
17- if ( process . platform !== 'linux' ) {
18- sharp = require ( 'sharp' ) ;
19- }
14+ const sharp = require ( 'sharp' ) ;
2015
2116class Image extends Model {
2217 constructor ( appInstance , imageData ) {
2318 super ( appInstance , imageData ) ;
2419 // Post ID
2520 this . id = parseInt ( imageData . id , 10 ) ;
2621
27- if ( imageData . id === 'website' ) {
22+ if ( imageData . id === 'website' ) {
2823 this . id = 'website' ;
2924 }
3025
@@ -35,7 +30,7 @@ class Image extends Model {
3530 // Image Type
3631 this . imageType = 'contentImages' ;
3732
38- if ( imageData . imageType ) {
33+ if ( imageData . imageType ) {
3934 this . imageType = imageData . imageType ;
4035 }
4136 }
@@ -52,8 +47,9 @@ class Image extends Model {
5247 // Store it in the temp directory
5348 this . id = 'temp' ;
5449 }
50+
5551 // For images added to existing posts
56- if ( ! this . path ) {
52+ if ( ! this . path ) {
5753 return ;
5854 }
5955
@@ -69,14 +65,14 @@ class Image extends Model {
6965 let galleryDirPath = '' ;
7066 let responsiveDirPath = '' ;
7167
72- if ( this . id === 'website' ) {
68+ if ( this . id === 'website' ) {
7369 dirPath = path . join ( this . siteDir , 'input' , 'media' , 'website' ) ;
7470 responsiveDirPath = path . join ( this . siteDir , 'input' , 'media' , 'website' , 'responsive' ) ;
7571 } else {
7672 dirPath = path . join ( this . siteDir , 'input' , 'media' , 'posts' , ( this . id ) . toString ( ) ) ;
7773 responsiveDirPath = path . join ( this . siteDir , 'input' , 'media' , 'posts' , ( this . id ) . toString ( ) , 'responsive' ) ;
7874
79- if ( this . imageType === 'galleryImages' ) {
75+ if ( this . imageType === 'galleryImages' ) {
8076 galleryDirPath = path . join ( this . siteDir , 'input' , 'media' , 'posts' , ( this . id ) . toString ( ) , 'gallery' ) ;
8177 }
8278 }
@@ -101,7 +97,7 @@ class Image extends Model {
10197 finalFileName = slug ( finalFileName . name , false , true ) + finalFileName . ext ;
10298 newPath = path . join ( dirPath , finalFileName ) ;
10399
104- if ( this . imageType === 'galleryImages' ) {
100+ if ( this . imageType === 'galleryImages' ) {
105101 newPath = path . join ( galleryDirPath , finalFileName ) ;
106102 }
107103
@@ -116,7 +112,7 @@ class Image extends Model {
116112 let pathData = path . parse ( newPath ) ;
117113
118114 // Save responsive images
119- if ( generateResponsiveImages && self . allowedImageExtension ( pathData . ext ) ) {
115+ if ( generateResponsiveImages && self . allowedImageExtension ( pathData . ext ) ) {
120116 self . createResponsiveImages ( newPath , self . imageType ) ;
121117 }
122118
@@ -132,9 +128,10 @@ class Image extends Model {
132128 }
133129 }
134130
131+ // Get image dimensions
135132 let dimensions = [ false , false ] ;
136133
137- if ( path . parse ( this . path ) . ext === '.svg' ) {
134+ if ( path . parse ( this . path ) . ext === '.svg' ) {
138135 dimensions = this . getSvgImageDimensions ( this . path ) ;
139136 } else {
140137 try {
@@ -166,26 +163,39 @@ class Image extends Model {
166163 let siteConfig = JSON . parse ( fs . readFileSync ( siteConfigPath ) ) ;
167164 let imagesQuality = 60 ;
168165 let imageExtension = path . parse ( originalPath ) . ext ;
166+ let imageDimensions = {
167+ width : false ,
168+ height : false
169+ } ;
169170
170- if ( ! this . allowedImageExtension ( imageExtension ) ) {
171+ if ( ! this . allowedImageExtension ( imageExtension ) ) {
171172 return [ ] ;
172173 }
173174
174- if (
175+ try {
176+ imageDimensions = sizeOf ( this . path ) ;
177+ } catch ( e ) {
178+ imageDimensions = {
179+ width : false ,
180+ height : false
181+ } ;
182+ }
183+
184+ if (
175185 siteConfig . advanced &&
176186 siteConfig . advanced . imagesQuality &&
177187 ! isNaN ( parseInt ( siteConfig . advanced . imagesQuality , 10 ) )
178188 ) {
179189 imagesQuality = siteConfig . advanced . imagesQuality ;
180190 imagesQuality = parseInt ( imagesQuality ) ;
181191
182- if ( imagesQuality < 1 || imagesQuality > 100 ) {
192+ if ( imagesQuality < 1 || imagesQuality > 100 ) {
183193 imagesQuality = 60 ;
184194 }
185195 }
186196
187197 // If there is no selected theme
188- if ( currentTheme === 'not selected' && imageType !== 'galleryImages' ) {
198+ if ( currentTheme === 'not selected' && imageType !== 'galleryImages' ) {
189199 return false ;
190200 }
191201
@@ -196,20 +206,20 @@ class Image extends Model {
196206 let dimensions = false ;
197207 let dimensionsConfig = false ;
198208
199- if ( imageType === 'featuredImages' && Utils . responsiveImagesConfigExists ( themeConfig , imageType ) ) {
209+ if ( imageType === 'featuredImages' && Utils . responsiveImagesConfigExists ( themeConfig , imageType ) ) {
200210 dimensions = Utils . responsiveImagesDimensions ( themeConfig , 'featuredImages' ) ;
201211 dimensionsConfig = Utils . responsiveImagesData ( themeConfig , 'featuredImages' ) ;
202- } else if ( imageType === 'optionImages' && Utils . responsiveImagesConfigExists ( themeConfig , imageType ) ) {
212+ } else if ( imageType === 'optionImages' && Utils . responsiveImagesConfigExists ( themeConfig , imageType ) ) {
203213 dimensions = Utils . responsiveImagesDimensions ( themeConfig , 'optionImages' ) ;
204214 dimensionsConfig = Utils . responsiveImagesData ( themeConfig , 'optionImages' ) ;
205- } else if ( imageType === 'contentImages' && Utils . responsiveImagesConfigExists ( themeConfig , 'contentImages' ) ) {
215+ } else if ( imageType === 'contentImages' && Utils . responsiveImagesConfigExists ( themeConfig , 'contentImages' ) ) {
206216 dimensions = Utils . responsiveImagesDimensions ( themeConfig , 'contentImages' ) ;
207217 dimensionsConfig = Utils . responsiveImagesData ( themeConfig , 'contentImages' ) ;
208- } else if ( imageType === 'galleryImages' && Utils . responsiveImagesConfigExists ( themeConfig , 'galleryImages' ) ) {
218+ } else if ( imageType === 'galleryImages' && Utils . responsiveImagesConfigExists ( themeConfig , 'galleryImages' ) ) {
209219 dimensions = Utils . responsiveImagesDimensions ( themeConfig , 'galleryImages' ) ;
210220 dimensionsConfig = Utils . responsiveImagesData ( themeConfig , 'galleryImages' ) ;
211221
212- if ( ! dimensionsConfig ) {
222+ if ( ! dimensionsConfig ) {
213223 dimensions = [ 'thumbnail' ] ;
214224
215225 dimensionsConfig = [ ] ;
@@ -221,20 +231,20 @@ class Image extends Model {
221231 }
222232 }
223233
224- if ( ! dimensions ) {
234+ if ( ! dimensions ) {
225235 return false ;
226236 }
227237
228238 let targetImagesDir = path . parse ( originalPath ) . dir ;
229239
230- if ( imageType !== 'galleryImages' ) {
240+ if ( imageType !== 'galleryImages' ) {
231241 targetImagesDir = path . join ( targetImagesDir , 'responsive' ) ;
232242 }
233243
234244 let promises = [ ] ;
235245
236246 // create responsive images for each size
237- for ( let name of dimensions ) {
247+ for ( let name of dimensions ) {
238248 let finalHeight = dimensionsConfig [ name ] . height ;
239249 let finalWidth = dimensionsConfig [ name ] . width ;
240250 let cropImage = dimensionsConfig [ name ] . crop ;
@@ -243,28 +253,36 @@ class Image extends Model {
243253 let destinationPath = path . join ( targetImagesDir , filename + '-' + name + extension ) ;
244254 let result ;
245255
246- if ( ! this . allowedImageExtension ( extension ) ) {
256+ if ( ! this . allowedImageExtension ( extension ) ) {
247257 continue ;
248258 }
249259
250- if ( finalHeight === 'auto' ) {
260+ if ( imageDimensions . width !== false && finalWidth !== 'auto' && finalWidth > imageDimensions . width ) {
261+ finalWidth = imageDimensions . width ;
262+ }
263+
264+ if ( imageDimensions . height !== false && finalHeight !== 'auto' && finalHeight > imageDimensions . height ) {
265+ finalHeight = imageDimensions . height ;
266+ }
267+
268+ if ( finalHeight === 'auto' ) {
251269 finalHeight = null ;
252270
253- if ( this . shouldUseJimp ( ) ) {
271+ if ( this . shouldUseJimp ( ) ) {
254272 finalHeight = Jimp . AUTO ;
255273 }
256274 }
257275
258- if ( finalWidth === 'auto' ) {
276+ if ( finalWidth === 'auto' ) {
259277 finalWidth = null ;
260278
261- if ( this . shouldUseJimp ( ) ) {
279+ if ( this . shouldUseJimp ( ) ) {
262280 finalWidth = Jimp . AUTO ;
263281 }
264282 }
265283
266- if ( cropImage ) {
267- if ( this . shouldUseJimp ( ) ) {
284+ if ( cropImage ) {
285+ if ( this . shouldUseJimp ( ) ) {
268286 result = new Promise ( ( resolve , reject ) => {
269287 Jimp . read ( originalPath , function ( err , image ) {
270288 if ( err ) {
@@ -322,7 +340,7 @@ class Image extends Model {
322340 } ) . catch ( err => console . log ( err ) ) ;
323341 }
324342 } else {
325- if ( this . shouldUseJimp ( ) ) {
343+ if ( this . shouldUseJimp ( ) ) {
326344 result = new Promise ( ( resolve , reject ) => {
327345 Jimp . read ( originalPath , function ( err , image ) {
328346 if ( err ) {
@@ -392,13 +410,13 @@ class Image extends Model {
392410 let svgHeight = svgFileContent . match ( / \< s v g .* h e i g h t = [ ' " ] { 1 } ( .* ?) [ ' " ] { 1 } .* \> / mi) ;
393411 let svgViewBox = svgFileContent . match ( / \< s v g .* v i e w B o x = [ ' " ] { 1 } ( .* ?) [ ' " ] { 1 } .* \> / mi) ;
394412
395- if ( svgWidth && svgHeight && svgWidth [ 1 ] . indexOf ( '%' ) === 1 && svgHeight [ 1 ] . indexOf ( '%' ) === 1 ) {
413+ if ( svgWidth && svgHeight && svgWidth [ 1 ] . indexOf ( '%' ) === 1 && svgHeight [ 1 ] . indexOf ( '%' ) === 1 ) {
396414 result . height = parseInt ( svgHeight , 10 ) ;
397415 result . width = parseInt ( svgWidth , 10 ) ;
398- } else if ( svgViewBox && svgViewBox [ 1 ] ) {
416+ } else if ( svgViewBox && svgViewBox [ 1 ] ) {
399417 svgViewBox = svgViewBox [ 1 ] . split ( ' ' ) ;
400418
401- if ( svgViewBox . length === 4 ) {
419+ if ( svgViewBox . length === 4 ) {
402420 result . height = svgViewBox [ 3 ] ;
403421 result . width = svgViewBox [ 2 ] ;
404422 }
@@ -420,12 +438,7 @@ class Image extends Model {
420438 * Detect if Jimp should be used
421439 */
422440 shouldUseJimp ( ) {
423- return (
424- process . platform === 'linux' || (
425- this . appInstance . appConfig . resizeEngine &&
426- this . appInstance . appConfig . resizeEngine === 'jimp'
427- )
428- ) ;
441+ return this . appInstance . appConfig . resizeEngine && this . appInstance . appConfig . resizeEngine === 'jimp' ;
429442 }
430443}
431444
0 commit comments