From 50910bb79618de36c2f063d87964ba37ff2a05d7 Mon Sep 17 00:00:00 2001 From: wonknu10 Date: Mon, 27 Mar 2017 10:05:32 +0200 Subject: [PATCH] enhancement: image resize with only width or height specified --- docs/types/abe-image.md | 2 ++ src/cli/cms/media/image.js | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/docs/types/abe-image.md b/docs/types/abe-image.md index fce36b4e..57ae6ce6 100755 --- a/docs/types/abe-image.md +++ b/docs/types/abe-image.md @@ -45,6 +45,8 @@ uploading a image named *my_image.jpg* will create a thumbs by default named my_ if the tag contains ```thumbs='250x250,350x350'``` this will also create *my_image_thumb_250x250.jpg* & *my_image_thumb_350x350.jpg* +if the tag contains ```thumbs='250x'``` or ```thumbs='x250'``` this will create an image that have the with / height specified and the other side no specified will be resized to keep the same ratio as the original image + ###Important thumb are created using https://github.com/jwagner/smartcrop-cli which require to have imagemagick installed on your computer, if you don't a fallback library full JS is used (https://github.com/oliver-moran/jimp) but doesn't crop in a smart way your images diff --git a/src/cli/cms/media/image.js b/src/cli/cms/media/image.js index deaaed64..d5ae4c1c 100644 --- a/src/cli/cms/media/image.js +++ b/src/cli/cms/media/image.js @@ -45,14 +45,24 @@ export function cropAndSaveFiles(images, file, resp) { size: image }) - let newWidth = image.split('x')[0] - let newHeight = image.split('x')[1] + let splitedImage = image.split('x') + let newWidth = null + let newHeight = null + + if(splitedImage[0] != null && splitedImage[0] != '') newWidth = parseInt(image.split('x')[0]) + if(splitedImage[1] != null && splitedImage[1] != '') newHeight = parseInt(image.split('x')[1]) Jimp.read(file).then(function (originalImage) { var originalWidth = originalImage.bitmap.width var originalHeight = originalImage.bitmap.height var ratio = originalWidth*newHeight/newWidth - if(parseInt(ratio - 1) <= parseInt(originalHeight) && parseInt(ratio + 1) >= parseInt(originalHeight)){ + if(newWidth === null || newHeight === null){ + originalImage.resize(newWidth != null ? newWidth : Jimp.AUTO, newHeight != null ? newHeight : Jimp.AUTO).write(newFile) + if(++cropedImage === length) { + resolve(resp) + } + } + else if(parseInt(ratio - 1) <= parseInt(originalHeight) && parseInt(ratio + 1) >= parseInt(originalHeight)){ originalImage.resize(parseInt(image.split('x')[0]), parseInt(image.split('x')[1])).write(newFile) if(++cropedImage === length) { resolve(resp)