How to use the sharp.gravity 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 vseventer / sharp-cli / lib / constants.js View on Github external
'use strict'

// Package modules.
const sharp = require('sharp')

// 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 thatisuday / npm-sharper / index.js View on Github external
function(size, _cb){
							// resize by default
							var $query = master.background(options.background);

							// check resize
							if(options.resize == true){
								$query = $query.resize(size.width, size.height);
							}

							// check crop (on valid crop option)
							if(options.crop != false && _.has(sharp.gravity, options.crop)){
								$query = $query.crop(sharp.gravity[options.crop]);
							}

							// check embed
							if(options.embed == true){
								$query = $query.embed();
							}

							// check max
							if(options.max == true){
								$query = $query.max();
							}

							// check min
							if(options.min == true){
								$query = $query.min();
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 tomalrussell / colouring-london / app / src / tiles / renderers / stitchTile.ts View on Github external
const [topLeft, topRight, bottomLeft, bottomRight] = await Promise.all([
        [nextXY.minX, nextXY.minY],
        [nextXY.maxX, nextXY.minY],
        [nextXY.minX, nextXY.maxY],
        [nextXY.maxX, nextXY.maxY]
    ].map(([x, y]) => renderTile({ tileset, z: nextZ, x, y, scale }, dataParams)));

    const compositedBuffer = await sharp({
        create: {
            width: tileSize * 2,
            height: tileSize * 2,
            channels: 4,
            background: { r: 0, g: 0, b: 0, alpha: 0 }
        }
    }).composite([
        {input: topLeft, gravity: sharp.gravity.northwest},
        {input: topRight, gravity: sharp.gravity.northeast},
        {input: bottomLeft, gravity: sharp.gravity.southwest},
        {input: bottomRight, gravity: sharp.gravity.southeast}
    ]).png().toBuffer();
    
    return sharp(compositedBuffer)
        .resize(tileSize, tileSize, {fit: 'inside'})
        .png()
        .toBuffer();
}
github piobyte / flamingo / src / profiles / examples.js View on Github external
processor: 'sharp', pipe: function (instance) {
          return instance
            .rotate()
            .background('white').flatten()
            .toFormat(format.type, {quality})
            .resize(Math.ceil(width), Math.ceil(height))
            .min()
            .crop(sharp.gravity.center);
        }
      }]
github HarukaFuture / TakingSomethingBot / bot.js View on Github external
async function genPicPNG(avatarStream,filename){//图片套模板
	data = await streamToBuffer(avatarStream)
	return await sharp('template1.png').overlayWith(data,{gravity:sharp.gravity.southeast}).png().toBuffer()
}
function genCircleAvatar(origStream){//圆形头像
github Maddoxkkm / WoTB_SerBot / SerBot.js View on Github external
if (message.attachments.array()[0] === undefined && fullInput[1] === undefined) {
                throw(SerBotDetails.ErrorArray.Missing_Attachment)
            }
            if (message.attachments.array()[0] === undefined) {
                keyartUrl = fullInput[1]
            } else {
                keyartUrl = message.attachments.array()[0].url
            }
            const imageData = await request.Request({url: keyartUrl, encoding: null});
            const image = sharp(imageData);
            await fs.mkdirs(`keyart_gen/temp/${message.id}/Keyart/Data/Gfx/UI/KeyArt`);
            await fs.mkdirs(`keyart_gen/temp/${message.id}/Keyart/Data/Gfx2/UI/KeyArt`);
            await image
                .resize(2728, 1536)
                .min()
                .crop(sharp.gravity.centre)
                .toFormat('webp')
                .toFile(`keyart_gen/temp/${message.id}/Keyart/Data/Gfx2/UI/KeyArt/Mad_Easy_Keyart.webp`)
                .then(async function (info) {
                    await fs.copy(`keyart_gen/tex_source/tex.tex`, `keyart_gen/temp/${message.id}/Keyart/Data/Gfx2/UI/KeyArt/Mad_Easy_Keyart.tex`);
                    await fs.writeFile(`keyart_gen/temp/${message.id}/Keyart/Data/Gfx2/UI/KeyArt/keyart.txt`, `1\nMad_Easy_Keyart.tex\n${info.width} ${info.height}\n1\n0 0 ${info.width} ${info.height} 0 0 0 MadWoTBMods0`);
                    await fs.writeFile(`keyart_gen/temp/${message.id}/Keyart/Data/Gfx2/UI/KeyArt/Mad_Easy_Keyart_size.txt`, `(I${info.width * info.height}\nI${info.width}\nI${info.height}\ntp0\n.`);
                });
            await image
                .resize(1364, 768)
                .min()
                .crop(sharp.gravity.centre)
                .toFormat('webp')
                .toFile(`keyart_gen/temp/${message.id}/Keyart/Data/Gfx/UI/KeyArt/Mad_Easy_Keyart.webp`)
                .then(async function (info) {
                    await fs.copy(`keyart_gen/tex_source/tex.tex`, `keyart_gen/temp/${message.id}/Keyart/Data/Gfx/UI/KeyArt/Mad_Easy_Keyart.tex`);
                    await fs.writeFile(`keyart_gen/temp/${message.id}/Keyart/Data/Gfx/UI/KeyArt/keyart.txt`, `1\nMad_Easy_Keyart.tex\n${info.width} ${info.height}\n1\n0 0 ${info.width} ${info.height} 0 0 0 MadWoTBMods0`);
github vendure-ecommerce / vendure / packages / asset-server-plugin / src / sharp-asset-preview-strategy.ts View on Github external
if (assetType === AssetType.IMAGE) {
            const image = sharp(data);
            const metadata = await image.metadata();
            const width = metadata.width || 0;
            const height = metadata.height || 0;
            if (maxWidth < width || maxHeight < height) {
                return image.resize(maxWidth, maxHeight, { fit: 'inside' }).toBuffer();
            } else {
                return data;
            }
        } else {
            return sharp(path.join(__dirname, 'file-icon.png'))
                .resize(800, 800, { fit: 'outside' })
                .overlayWith(this.generateMimeTypeOverlay(mimeType), {
                    gravity: sharp.gravity.center,
                })
                .toBuffer();
        }
    }

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 4 months ago

Package Health Score

92 / 100
Full package analysis