How to use the sharp.strategy function in sharp

To help you get started, we’ve selected a few sharp examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mapbox / appropriate-images / lib / generate.js View on Github external
function getCropper(name) {
  // See http://sharp.dimens.io/en/stable/api-resize/#crop
  //
  // Possible attributes of sharp.gravity are north, northeast, east, southeast, south,
  // southwest, west, northwest, center and centre.
  if (sharp.gravity[name] !== undefined) {
    return sharp.gravity[name];
  }
  // The experimental strategy-based approach resizes so one dimension is at its target
  // length then repeatedly ranks edge regions, discarding the edge with the lowest
  // score based on the selected strategy.
  // - entropy: focus on the region with the highest Shannon entropy.
  // - attention: focus on the region with the highest luminance frequency,
  //   colour saturation and presence of skin tones.
  if (sharp.strategy[name] !== undefined) {
    return sharp.strategy[name];
  }

  throw new UsageError(
    `"${name}" is not a valid crop value. Consult http://sharp.dimens.io/en/stable/api-resize/#crop`
  );
}
github vseventer / sharp-cli / lib / constants.js View on Github external
// Exports.
module.exports = {
  BAND: ['red', 'green', 'blue'],
  BLEND: Object.keys(sharp.blend),
  BOOL: Object.keys(sharp.bool),
  COLOURSPACE: Object.keys(sharp.colourspace),
  CONTAINER: ['fs', 'zip'],
  DEPTH: ['onepixel', 'onetile', 'one'],
  FIT: Object.keys(sharp.fit),
  FORMAT: ['heif', 'jpeg', 'jpg', 'png', 'raw', 'tiff', 'webp'],
  GRAVITY: Object.keys(sharp.gravity),
  HEIF_COMPRESSION: ['hevc', 'avc', 'jpeg', 'av1'],
  KERNEL: Object.keys(sharp.kernel),
  LAYOUT: ['dz', 'google', 'zoomify'],
  POSITION: Object.keys(sharp.position),
  STRATEGY: Object.keys(sharp.strategy),
  TIFF_COMPRESSION: ['ccittfax4', 'deflate', 'jpeg', 'lzw', 'none'],
  TIFF_PREDICTOR: ['float', 'horizontal', 'none']
}
github mapbox / appropriate-images / lib / generate.js View on Github external
function getCropper(name) {
  // See http://sharp.dimens.io/en/stable/api-resize/#crop
  //
  // Possible attributes of sharp.gravity are north, northeast, east, southeast, south,
  // southwest, west, northwest, center and centre.
  if (sharp.gravity[name] !== undefined) {
    return sharp.gravity[name];
  }
  // The experimental strategy-based approach resizes so one dimension is at its target
  // length then repeatedly ranks edge regions, discarding the edge with the lowest
  // score based on the selected strategy.
  // - entropy: focus on the region with the highest Shannon entropy.
  // - attention: focus on the region with the highest luminance frequency,
  //   colour saturation and presence of skin tones.
  if (sharp.strategy[name] !== undefined) {
    return sharp.strategy[name];
  }

  throw new UsageError(
    `"${name}" is not a valid crop value. Consult http://sharp.dimens.io/en/stable/api-resize/#crop`
  );
}
github soomtong / blititor / module / gallery / lib / gallery.js View on Github external
function processImageWithSharp(file, callback) {
    const name = file.filename;

    const resizeOpt = {
        width: 1600,
        height: 1800,
        type: 'jpeg',
        quality: 95
    };
    const thumbnailOpt = {
        width: 80,
        height: 60,
        entropy: sharp.strategy.entropy
    };

    const image1 = sharp(file.path);

    image1.metadata(function (error, data) {
        // calculate image aspect and resize if image size is over
        if (data.width > resizeOpt.width) {
            if (image1.height > resizeOpt.height) {
                image1.resize(null, resizeOpt.height);
            } else {
                image1.resize(resizeOpt.width, null);
            }
        } else if (data.height > resizeOpt.height * 2) {
            image1.resize(null, resizeOpt.height);
        }
github jayway / vue-js-workshop / api / routes / products.js View on Github external
productsApi.put('/upload', upload.single('product_image'), (req, res) => {
  const productId = req.body.product_id;
  const filename = productId + Date.now();

  sharp(req.file.buffer)
    .resize(200, 200)
    .crop(sharp.strategy.entropy)
    .toFile(`${__dirname}/../public/uploads/${filename}`, (err) => {
      if (err) {
        console.error('woops', err);
      }

      inMemoryProducts[productId].imageUrl = `http://localhost:3000/uploads/${filename}`;
      inMemoryProducts[productId].imageName = req.file.originalname;

      res.status(201);
      res.send({
        data: req.file.originalname,
      });
    });
});
github vendure-ecommerce / vendure / packages / asset-server-plugin / src / transform-image.ts View on Github external
presets: ImageTransformPreset[],
): Promise {
    let width = +queryParams.w || undefined;
    let height = +queryParams.h || undefined;
    let mode = queryParams.mode || 'crop';
    if (queryParams.preset) {
        const matchingPreset = presets.find(p => p.name === queryParams.preset);
        if (matchingPreset) {
            width = matchingPreset.width;
            height = matchingPreset.height;
            mode = matchingPreset.mode;
        }
    }
    const options: ResizeOptions = {};
    if (mode === 'crop') {
        options.position = sharp.strategy.entropy;
    } else {
        options.fit = 'inside';
    }
    return sharp(originalImage).resize(width, height, options);
}
github vendure-ecommerce / vendure / server / src / plugin / default-asset-server-plugin / transform-image.ts View on Github external
presets: ImageTransformPreset[],
): Promise {
    let width = +queryParams.w || undefined;
    let height = +queryParams.h || undefined;
    let mode = queryParams.mode || 'crop';
    if (queryParams.preset) {
        const matchingPreset = presets.find(p => p.name === queryParams.preset);
        if (matchingPreset) {
            width = matchingPreset.width;
            height = matchingPreset.height;
            mode = matchingPreset.mode;
        }
    }
    const options: ResizeOptions = {};
    if (mode === 'crop') {
        options.position = sharp.strategy.entropy;
    } else {
        options.fit = 'inside';
    }
    return sharp(originalImage).resize(width, height, options);
}
github CrucifixArnaud / rapworldmap / app / modules / artists / artists.controller.js View on Github external
return res.status(400).json({
        error: {
          status: res.status,
          title: 'An error occured during file upload',
          detail: [{
              msg: errorMsg,
              param: err.field
            }
          ],
          meta: req.body
        }
      });
    }

    if(req.file) {
      sharp(req.file.path).resize(300, 300).crop(sharp.strategy.entropy).toFile('uploads/medium-' + req.file.filename, function (err) {
        if (err) {
          return next(err);
        }
      });
    }

    next();

  });
}
github Rocketseat / rocketpwa / lib / splash.js View on Github external
sizes.map(async size => {
      let image = sharp(fromPath).resize(size.w, size.h);

      if (fill) {
        image = image.background(fill).embed();
      } else {
        image = image.crop(sharp.strategy.entropy);
      }

      await image
        .png()
        .toFile(path.resolve(destPath, `splash-${size.w}x${size.h}.png`));

      htmlSplash.push(``);
    })
  );

sharp

High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, GIF, AVIF and TIFF images

Apache-2.0
Latest version published 1 month ago

Package Health Score

94 / 100
Full package analysis