.makeResizedImages(..) mostly works correctly, still missing transforms + UI...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-10-30 19:16:42 +03:00
parent a34591065a
commit 669ee51297

View File

@ -119,7 +119,9 @@ var SharpActions = actions.Actions({
this.previewConstructorWorker && this.previewConstructorWorker.kill() this.previewConstructorWorker && this.previewConstructorWorker.kill()
delete this.previewConstructorWorker }], delete this.previewConstructorWorker }],
// XXX should this resize up??? ...option??? // XXX Q: if skipSmaller and overwrite: 'backup' and image smaller (skiped) do we backup???
// ...I think no...
// XXX make backup name pattern configurable...
// XXX add transform/crop support... // XXX add transform/crop support...
// XXX revise logging... // XXX revise logging...
makeResizedImage: ['- Image/', makeResizedImage: ['- Image/',
@ -141,18 +143,28 @@ var SharpActions = actions.Actions({
// output image name... // output image name...
// //
// Used if processing a single image, ignored otherwise. // Used if processing a single image, ignored otherwise.
name: <str>, name: null | <str>,
// image name pattern and data... // image name pattern and data...
// //
// NOTE: for more info on pattern see: .formatImageName(..) // NOTE: for more info on pattern see: .formatImageName(..)
pattern: <str>, pattern: null | <str>,
data: { .. }, data: null | { .. },
// if true and image is smaller than size enlarge it...
//
// default: null / false
enlarge: null | true,
// overwrite, backup or skip (default) existing images... // overwrite, backup or skip (default) existing images...
// //
// default: null / false // default: null / false
overwrite: true | 'backup' | false, overwrite: null | true | 'backup',
// if true do not write an image if it's smaller than size...
//
// default: null / false
skipSmaller: null | true,
// XXX not implemented... // XXX not implemented...
transform: ..., transform: ...,
@ -162,6 +174,7 @@ var SharpActions = actions.Actions({
, } , }
NOTE: all options are optional.
NOTE: this will not overwrite existing images. NOTE: this will not overwrite existing images.
`, `,
function(images, size, path, options={}){ function(images, size, path, options={}){
@ -200,8 +213,12 @@ var SharpActions = actions.Actions({
name, name,
pattern, pattern,
data, data,
// file handling... // file handling...
enlarge,
skipSmaller,
overwrite, overwrite,
// transformations... // transformations...
// XXX not implemented... // XXX not implemented...
transform, transform,
@ -238,16 +255,37 @@ var SharpActions = actions.Actions({
logger && logger.emit('queued', to) logger && logger.emit('queued', to)
// existing image... var img = sharp(source)
return (skipSmaller ?
// skip source if smaller...
img
.metadata()
.then(function(m){
// skip...
if((fit == 'inside'
&& Math.max(m.width, m.height) < size)
|| (fit == 'outside'
&& Math.min(m.width, m.height) < size)){
logger && logger.emit('skipping', to)
return }
return img })
: Promise.resolve(img))
// write...
.then(function(img){
return img
&& ensureDir(pathlib.dirname(to))
.then(function(){
// handle existing image...
if(fse.existsSync(to)){ if(fse.existsSync(to)){
// rename... // rename...
// XXX make backup name pattern configurable...
if(overwrite == 'backup'){ if(overwrite == 'backup'){
var i = 0 var i = 0
while(fse.existsSync(`${to}.${timestamp}'-bak`+ (i || ''))){ while(fse.existsSync(`${to}.${timestamp}.bak`+ (i || ''))){
i++ } i++ }
fse.renameSync( fse.renameSync(
to, to,
fse.existsSync(`${to}.${timestamp}'-bak`+ (i || ''))) `${to}.${timestamp}.bak`+ (i || ''))
// remove... // remove...
} else if(overwrite){ } else if(overwrite){
fse.removeSync(to) fse.removeSync(to)
@ -256,9 +294,8 @@ var SharpActions = actions.Actions({
logger && logger.emit('skipping', to) logger && logger.emit('skipping', to)
return } } return } }
return ensureDir(pathlib.dirname(to)) // write...
.then(function(){ return img
return sharp(source)
.clone() .clone()
// handle transform (.orientation / .flip) and .crop... // handle transform (.orientation / .flip) and .crop...
.run(function(){ .run(function(){
@ -275,20 +312,23 @@ var SharpActions = actions.Actions({
// and in which coordinates we // and in which coordinates we
// crop (i.e. pre/post transform)... // crop (i.e. pre/post transform)...
if(transform){ if(transform){
// XXX
} }
if(crop){ if(crop){
// XXX
} }
}) })
.resize({ .resize({
width: size, width: size,
height: size, height: size,
fit: fit, fit: fit,
withoutEnlargement: !enlarge,
}) })
.withMetadata() .withMetadata()
.toFile(to) .toFile(to)
.then(function(){ .then(function(){
logger logger
&& logger.emit('done', to) })}) })) }], && logger.emit('done', to) }) }) }) })) }],
// XXX use .makeResizedImage(..) // XXX use .makeResizedImage(..)
// XXX should this account for non-jpeg images??? // XXX should this account for non-jpeg images???