How to use the sharp.fit 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 venveo / serverless-sharp / src / image-ops / size.js View on Github external
fpxLeft = newWidth - width
  } else if (fpxLeft < 0) {
    fpxLeft = 0
  }

  // adjust focal point y
  if (fpyTop + height > newHeight) {
    fpyTop = newHeight - height
  } else if (fpyTop < 0) {
    fpyTop = 0
  }
  image.resize({
    width: newWidth,
    height: newHeight,
    withoutEnlargement: false,
    fit: sharp.fit.fill
  }).extract({
    left: fpxLeft,
    top: fpyTop,
    width,
    height
  })
}
github reactioncommerce / reaction / src / core-services / files / setUpFileCollections.js View on Github external
async transformWrite(fileRecord) {
        if (!transform) return null;

        const { size, fit, format, type } = transform;

        // Need to update the content type and extension of the file info, too.
        // The new size gets set correctly automatically by FileCollections package.
        fileRecord.type(type, { store: name });
        fileRecord.extension(format, { store: name });

        // resizing image, adding fit, setting output format
        return sharp()
          .resize({ width: size, height: size, fit: sharp.fit[fit], withoutEnlargement: true })
          .toFormat(format)
          .on("error", (err) => {
            throw new ReactionError("error-sharp-resize-internal", err);
          });
      }
    })
github venveo / serverless-sharp / src / image-ops / size.js View on Github external
exports.fill = async (image, mode, width = null, height = null, color = null) => {
  const resizeParams = {
    withoutEnlargement: false,
    fit: sharp.fit.contain
  }
  if (width) {
    resizeParams.width = width
  }
  if (height) {
    resizeParams.height = height
  }
  // TODO: Validate color more explicitly
  if (color) {
    resizeParams.background = color
  }
  image.resize(resizeParams)
}
github DivanteLtd / storefront-api / src / lib / image.js View on Github external
export async function resize (buffer, width, height) {
  try {
    const transformer = sharp(buffer);

    if (width || height) {
      const options = {
        withoutEnlargement: true,
        fit: sharp.fit.inside
      }
      transformer.resize(width, height, options)
    }

    return transformer.toBuffer();
  } catch (err) {
    console.log(err);
  }
}
github gridsome / gridsome / gridsome / lib / workers / image-processor.js View on Github external
height: parseInt(options.height, 10) || null,
      jpegProgressive: true
    }

    const plugins = []
    let pipeline = sharp(buffer)

    if (
      (config.width && config.width <= width) ||
      (config.height && config.height <= height)
    ) {
      const resizeOptions = {}

      if (config.height) resizeOptions.height = config.height
      if (config.width) resizeOptions.width = config.width
      if (options.fit) resizeOptions.fit = sharp.fit[options.fit]
      if (options.position) resizeOptions.position = sharp.position[options.position]
      if (options.background && colorString.get(options.background)) {
        resizeOptions.background = options.background
      } else if (backgroundColor) {
        resizeOptions.background = backgroundColor
      }

      pipeline = pipeline.resize(resizeOptions)
    }

    if (/\.png$/.test(ext)) {
      const quality = config.quality / 100

      pipeline = pipeline.png({
        compressionLevel: config.pngCompressionLevel,
        adaptiveFiltering: false
github gfaraj / super-bot / src / plugins / sticker.js View on Github external
if (!message.attachment || !message.attachment.data) {
        bot.error('No image provided.');
        return;
    }

    let processed = message;

    if (removeBg) {
        processed = await bot.receive(bot.copy(message).text('rembg'));
        if (!processed || !processed.attachment || !processed.attachment.data) {
            processed = message;
        }
    }

    let fit = message.text === "fill" ? sharp.fit.fill : sharp.fit.cover;
    let position = fit === sharp.fit.cover ? sharp.gravity.north : sharp.gravity.center;

    let data = processed.attachment.data.split(',')[1];
    let sharpInstance = sharp(Buffer.from(data, 'base64')).ensureAlpha();

    if (trim) {
        sharpInstance = sharpInstance.trim(5);
    }

    let stickerData = await sharpInstance
        .resize(512, 512, {
            fit,
            position,
            background: { r: 255, g: 255, b: 255, alpha: 0 }
        })
        .webp()
        .toBuffer();
github itgalaxy / favicons / src / helpers.js View on Github external
.then(svgBuffer =>
              sharp(svgBuffer)
                .resize({
                  background,
                  width,
                  height,
                  fit: sharp.fit.contain
                })
                .toBuffer()
            )
github oddbit / tanam / functions / src / triggers / storage.ts View on Github external
const resizeAndConvertImage = (size: number) =>
    sharp(originalFileBuffer)
      .resize(size, size, {
        withoutEnlargement: true,
        fit: sharp.fit.inside,
      })
      .toFormat(sharp.format.webp)
      .toBuffer();
github przemyslawpluta / removd / lib / image.js View on Github external
function upscale(detail, ext) {

    if (ext === 'png') {
        return sharp().resize({
            width: detail.width,
            height: detail.height,
            fit: sharp.fit.inside
        }).png().withMetadata();
    }

    return sharp().resize({
        width: detail.width,
        height: detail.height,
        fit: sharp.fit.inside
    }).jpeg({
        quality: 93,
        chromaSubsampling: '4:4:4',
    }).withMetadata();

}
github venveo / serverless-sharp / src / image-ops / size.js View on Github external
exports.scaleMax = (image, width = null, height = null) => {
  image.resize({
    width,
    height,
    withoutEnlargement: true,
    fit: sharp.fit.inside
  })
}

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