Пример #1
0
 ImageUtil.cropAndResize('some/path', generateArea(10, 0, 200, 200), [generateSize(-10, 10)], function(err, files) {
     assert.equal(err.code, 400);
     assert.ok(!files);
     ImageUtil.cropAndResize('some/path', generateArea(10, 0, 200, 200), [generateSize(10, -10)], function(err, files) {
         assert.equal(err.code, 400);
         assert.ok(!files);
         // Sanity check.
         var path = Path.resolve(__dirname + '/data/right.jpg');
         ImageUtil.cropAndResize(path, generateArea(10, 0, 10, 10), [generateSize(20, 20)], function(err, files) {
             assert.ok(!err);
             assert.ok(files);
             assert.ok(files['20x20']);
             assert.ok(files['20x20']['path']);
             assert.ok(fs.existsSync(files['20x20']['path']));
             assert.ok(files['20x20']['name']);
             assert.ok(files['20x20']['size'] > 0);
             gm(files['20x20']['path']).size(function (err, size) {
                 assert.ok(!err);
                 assert.strictEqual(size.width, 20);
                 assert.strictEqual(size.height, 20);
                 callback();
             });
         });
     });
 });
Пример #2
0
            webshot.getImage(etherpadFileUri, path, options, function (err) {
                if (err) {
                    log().error({'err': err, 'contentId': ctx.contentId}, 'Could not generate an image.');
                    return callback(err);
                }

                ctx.addPreview(path, 'wide');

                // Crop out a thumbnail, manually since we know the image is 1070x500.
                // We'll crop out a top-left box.
                var selectedArea = {
                    'x': 0,
                    'y': 0,
                    'width': PreviewConstants.SIZES.IMAGE.WIDE_HEIGHT,
                    'height': PreviewConstants.SIZES.IMAGE.WIDE_HEIGHT
                };
                ImageUtil.cropAndResize(path, selectedArea, [ {'width': PreviewConstants.SIZES.IMAGE.THUMBNAIL, 'height': PreviewConstants.SIZES.IMAGE.THUMBNAIL} ], function(err, files) {
                    if (err) {
                        log().error({'err': err}, 'Could not crop the image.');
                        return callback(err);
                    }

                    // Move the files to the thumbnail path
                    var key = PreviewConstants.SIZES.IMAGE.THUMBNAIL + 'x' + PreviewConstants.SIZES.IMAGE.THUMBNAIL;
                    var thumbnailPath = ctx.baseDir + '/thumbnail.png';
                    IO.moveFile(files[key].path, thumbnailPath, function(err) {
                        if (err) {
                            return callback(err);
                        }

                        ctx.setThumbnail(thumbnailPath);
                        callback();
                    });
                });
            });
Пример #3
0
 ImageUtil.cropAndResize('some/path', generateArea(0, 0, -10), [generateSize(100, 100)], function(err, files) {
     assert.equal(err.code, 400);
     assert.ok(!files);
     ImageUtil.cropAndResize('some/path', generateArea(-10, 0, 200), [generateSize(100, 100)], function(err, files) {
         assert.equal(err.code, 400);
         assert.ok(!files);
         ImageUtil.cropAndResize('some/path', generateArea(-10, 0, 200), null, function(err, files) {
             assert.equal(err.code, 400);
             assert.ok(!files);
             ImageUtil.cropAndResize('some/path', generateArea(-10, 0, 200), [], function(err, files) {
                 assert.equal(err.code, 400);
                 assert.ok(!files);
                 ImageUtil.cropAndResize('some/path', generateArea(-10, 0, 200), [generateSize(-10, 10)], function(err, files) {
                     assert.equal(err.code, 400);
                     assert.ok(!files);
                     ImageUtil.cropAndResize('some/path', generateArea(-10, 0, 200), [generateSize(10, -10)], function(err, files) {
                         assert.equal(err.code, 400);
                         assert.ok(!files);
                         callback();
                     });
                 });
             });
         });
     });
 });
Пример #4
0
    ContentAPI.getStorageBackend(ctx, principal.picture.largeUri).get(ctx, principal.picture.largeUri, function(err, file) {
        if (err) {
            return callback(err);
        }

        // Get the resized images.
        var selectedArea = {
            'x': parseInt(x, 10),
            'y': parseInt(y, 10),
            'width': parseInt(width, 10),
            'height': parseInt(width, 10)
        };
        var sizes = [
            {'width': PrincipalsConstants.picture.size.SMALL, 'height': PrincipalsConstants.picture.size.SMALL},
            {'width': PrincipalsConstants.picture.size.MEDIUM, 'height': PrincipalsConstants.picture.size.MEDIUM}
        ];
        ImageUtil.cropAndResize(file.path, selectedArea, sizes, function(err, files) {
            // Remove the temp file first.
            file.remove(function(removalError) {
                if (err) {
                    return callback(err);
                } else if (removalError) {
                    return callback(removalError);
                }

                // File removed, store and save the cropped and resized images.
                _storeAndSave(ctx, principal, files, callback);
            });
        });
    });
Пример #5
0
    gm(path).size(function (err, size) {
        if (err) {
            log().error({'err': err}, 'Could not get the image size for the large image.');
            return callback({'code': 500, 'msg': 'Could not get the image size for the large image.'});
        }

        // Ignore if the image is too small.
        if (imageWidth < width || imageHeight < height) {
            return callback(null, null);
        }

        var imageWidth = size.width;
        var imageHeight = size.height;

        // Find the smallest ratio
        var widthRatio = imageWidth / width;
        var heightRatio = imageHeight / height;
        var ratio = (widthRatio < heightRatio) ? widthRatio : heightRatio;

        var cropWidth = Math.floor(width * ratio);
        var cropHeight = Math.floor(height * ratio);

        // In portrait mode we crop out a box the size of the image width at the top of the image.
        // This is to get the top of the page for content items such as PDFs, Office files, ...
        var selectedArea = {
            'x': 0,
            'y': 0,
            'width': cropWidth,
            'height': cropHeight
        };

        // In landscape mode we crop out a box the size of the image height in the (absolute) center of the image.
        if (imageWidth > imageHeight) {
            selectedArea.x = Math.floor((imageWidth - cropWidth) / 2);
            selectedArea.y = Math.floor((imageHeight - cropHeight) / 2);
        }

        // Crop the correct square.
        ImageUtil.cropAndResize(path, selectedArea, [ {'width': width, 'height': height} ], function(err, files) {
            if (err) {
                log().error({'err': err}, 'Could not crop the image.');
                return callback(err);
            }

            // Move the files to the thumbnail path
            var key = width + 'x' + height;
            var croppedPath = ctx.baseDir + '/' + filename;
            IO.moveFile(files[key].path, croppedPath, function(err) {
                if (err) {
                    return callback(err);
                }

                callback(null, croppedPath);
            });
        });
    });
Пример #6
0
      puppeteerHelper.getImage(collabFileUri, imgPath, screenShottingOptions, err => {
        if (err) {
          log().error({ err, contentId: ctx.contentId }, 'Could not generate an image');
          return callback(err);
        }

        ctx.addPreview(imgPath, 'wide');

        // Crop out a thumbnail, manually since we know the image is 1070x500.
        // We'll crop out a top-left box.
        const selectedArea = {
          x: 0,
          y: 0,
          width: PreviewConstants.SIZES.IMAGE.WIDE_HEIGHT,
          height: PreviewConstants.SIZES.IMAGE.WIDE_HEIGHT
        };
        ImageUtil.cropAndResize(
          imgPath,
          selectedArea,
          [
            {
              width: PreviewConstants.SIZES.IMAGE.THUMBNAIL,
              height: PreviewConstants.SIZES.IMAGE.THUMBNAIL
            }
          ],
          (err, files) => {
            if (err) {
              log().error({ err }, 'Could not crop the image');
              return callback(err);
            }

            // Move the files to the thumbnail path
            const key = PreviewConstants.SIZES.IMAGE.THUMBNAIL + 'x' + PreviewConstants.SIZES.IMAGE.THUMBNAIL;
            const thumbnailPath = Path.join(ctx.baseDir, '/thumbnail.png');

            IO.moveFile(files[key].path, thumbnailPath, err => {
              if (err) {
                return callback(err);
              }

              ctx.setThumbnail(thumbnailPath);
              callback();
            });
          }
        );
      });
Пример #7
0
 it('verify parameter validation', function(callback) {
     ImageUtil.cropAndResize(undefined, generateArea(0, 0, 200), [generateSize(100, 100)], function(err, files) {
         assert.equal(err.code, 400);
         assert.ok(!files);
         ImageUtil.cropAndResize('some/path', null, [generateSize(100, 100)], function(err, files) {
             assert.equal(err.code, 400);
             assert.ok(!files);
             ImageUtil.cropAndResize('some/path', generateArea(-10, 0, 200), [generateSize(100, 100)], function(err, files) {
                 assert.equal(err.code, 400);
                 assert.ok(!files);
                 ImageUtil.cropAndResize('some/path', generateArea(0, -10, 200), [generateSize(100, 100)], function(err, files) {
                     assert.equal(err.code, 400);
                     assert.ok(!files);
                     ImageUtil.cropAndResize('some/path', generateArea(0, 0, -10), [generateSize(100, 100)], function(err, files) {
                         assert.equal(err.code, 400);
                         assert.ok(!files);
                         ImageUtil.cropAndResize('some/path', generateArea(-10, 0, 200), [generateSize(100, 100)], function(err, files) {
                             assert.equal(err.code, 400);
                             assert.ok(!files);
                             ImageUtil.cropAndResize('some/path', generateArea(-10, 0, 200), null, function(err, files) {
                                 assert.equal(err.code, 400);
                                 assert.ok(!files);
                                 ImageUtil.cropAndResize('some/path', generateArea(-10, 0, 200), [], function(err, files) {
                                     assert.equal(err.code, 400);
                                     assert.ok(!files);
                                     ImageUtil.cropAndResize('some/path', generateArea(-10, 0, 200), [generateSize(-10, 10)], function(err, files) {
                                         assert.equal(err.code, 400);
                                         assert.ok(!files);
                                         ImageUtil.cropAndResize('some/path', generateArea(-10, 0, 200), [generateSize(10, -10)], function(err, files) {
                                             assert.equal(err.code, 400);
                                             assert.ok(!files);
                                             callback();
                                         });
                                     });
                                 });
                             });
                         });
                     });
                 });
             });
         });
     });
 });