| 337 | |
| 338 | Image.transform = function (file, transform, autoOrientation, fn){ |
| 339 | function _transform(err, img){ |
| 340 | // img -- info object |
| 341 | var |
| 342 | images = {} |
| 343 | , queue = api.queue(function (err){ |
| 344 | fn(err, images); |
| 345 | }) |
| 346 | ; |
| 347 | |
| 348 | if( !err ){ |
| 349 | api.each(transform, function (params, name){ |
| 350 | if( !queue.isFail() ){ |
| 351 | var ImgTrans = new Image(img.nodeType ? img : file), isFn = typeof params == 'function'; |
| 352 | |
| 353 | if( isFn ){ |
| 354 | params(img, ImgTrans); |
| 355 | } |
| 356 | else if( params.width ){ |
| 357 | ImgTrans[params.preview ? 'preview' : 'resize'](params.width, params.height, params.strategy); |
| 358 | } |
| 359 | else { |
| 360 | if( params.maxWidth && (img.width > params.maxWidth || img.height > params.maxHeight) ){ |
| 361 | ImgTrans.resize(params.maxWidth, params.maxHeight, 'max'); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | if( params.crop ){ |
| 366 | var crop = params.crop; |
| 367 | ImgTrans.crop(crop.x|0, crop.y|0, crop.w || crop.width, crop.h || crop.height); |
| 368 | } |
| 369 | |
| 370 | if( params.rotate === undef && autoOrientation ){ |
| 371 | params.rotate = 'auto'; |
| 372 | } |
| 373 | |
| 374 | ImgTrans.set({ type: ImgTrans.matrix.type || params.type || file.type || 'image/png' }); |
| 375 | |
| 376 | if( !isFn ){ |
| 377 | ImgTrans.set({ |
| 378 | deg: params.rotate |
| 379 | , overlay: params.overlay |
| 380 | , filter: params.filter |
| 381 | , quality: params.quality || 1 |
| 382 | }); |
| 383 | } |
| 384 | |
| 385 | queue.inc(); |
| 386 | ImgTrans.toData(function (err, image){ |
| 387 | if( err ){ |
| 388 | queue.fail(); |
| 389 | } |
| 390 | else { |
| 391 | images[name] = image; |
| 392 | queue.next(); |
| 393 | } |
| 394 | }); |
| 395 | } |
| 396 | }); |