How to use the sharp.cache 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 chromakode / time / src / renderGIF.js View on Github external
const gl = createGLContext(WIDTH_S, HEIGHT_S)
  if (!gl) {
    console.error('Unable to create GL context')
    process.exit(1)
  }

  const blendData = JSON.parse(result.result[0])
  const timePiece = time(blendData, gl)

  const gif = new GifEncoder(WIDTH, HEIGHT, {highWaterMark: 1024 * 1024})
  gif.pipe(fs.createWriteStream(outPath))
  gif.setRepeat(0)
  gif.writeHeader()

  sharp.cache(false)  // Reduce memory usage

  const totalFrames = 2 * FRAMES - 1
  for (let i = 0; i < totalFrames; i++) {
    // Render frame
    const y = i < FRAMES ? i / FRAMES : 2 - i / FRAMES
    timePiece.renderFrame(.5, y)

    // Get frame data
    let pixels = new Uint8Array(WIDTH_S * HEIGHT_S * 4)
    gl.readPixels(0, 0, WIDTH_S, HEIGHT_S, gl.RGBA, gl.UNSIGNED_BYTE, pixels)

    // Resize and flip (otherwise gifs come out upside-down!?)
    let framePixels = await sharp(Buffer.from(pixels.buffer), {
        raw: {
          width: WIDTH_S,
          height: HEIGHT_S,
github medialab / website / wilson / images.js View on Github external
const assert = require('assert');
const async = require('async');
const sharp = require('sharp');
const path = require('path');
const {sharpToString, imgToProcessedPng} = require('../specs/processing.js');

if (process.env.DISABLE_SHARP_CACHE)
  sharp.cache(false);

// Typical cover resize for people portraits
const COVER_RESIZE = {
  width: 300 * 2,
  height: 225 * 2
};

exports.buildCover = function buildCover(
  inputDir,
  outputDir,
  pathPrefix,
  item,
  options,
  callback
) {
  assert(!!item.cover, `wilson/images.buildCover: cannot build cover if item "${item.id}" has none.`);
github expo / turtle / src / builders / utils / image.ts View on Github external
export async function getImageDimensionsWithSharpAsync(dirname: string, filename: string) {
  const filepath = path.join(dirname, filename);

  // PLEASE DON'T REMOVE THE FOLLOWING LINE (sharp caches files taking path as the cache key)
  sharp.cache(false);

  try {
    const { width, height } = await sharp(filepath).metadata();
    return { width, height };
  } catch (e) {
    return null;
  }
}
github davidmerfield / Blot / app / build / thumbnail / validate.js View on Github external
var sharp = require('sharp');
var config = require('./config');
var FORMATS = config.FORMATS;
var MIN_WIDTH = config.MIN_WIDTH;
var MIN_HEIGHT = config.MIN_HEIGHT;

// Sharp seems to cache files based on their 
// path and not the contents of the file at 
// a particular path. It was returning stale
// versions of a file in the blog's folder. 
// Perhaps it might be smarter to copy the file
// to the temporary directory before operating on it?
// It's also possible that this is a bug in Sharp's
// caching that has been fixed in a more recent version
// or that still needs to be fixed. I should investigate.
sharp.cache(false);

module.exports = function (path, callback) {

  var image = sharp(path);

  image.metadata(function(err, info){

    if (err) return callback(err);

    if (!info) return callback(new Error('Could not read file info'));

    var format = info.format.toLowerCase();
    var width = info.width;
    var height = info.height;

    if (FORMATS.indexOf(format) === -1)
github expo / turtle / src / builders / utils / image.ts View on Github external
export async function resizeIconWithSharpAsync(iconSizePx: number, iconFilename: string, destinationIconPath: string) {
  const filename = path.join(destinationIconPath, iconFilename);

  // PLEASE DON'T REMOVE THE FOLLOWING LINE (sharp caches files taking path as the cache key)
  sharp.cache(false);

  // sharp can't have same input and output filename, so load to buffer then
  // write to disk after resize is complete
  const buffer = await sharp(filename)
    .resize(iconSizePx, iconSizePx)
    .toBuffer();

  await fs.writeFile(filename, buffer);
}
github gjovanov / facer / api / controller / user-controller.js View on Github external
const express = require('express')
const {
  lstatSync,
  readFileSync,
  readdirSync,
  existsSync,
  mkdirSync,
  writeFile,
  unlinkSync
} = require('fs')
const {
  join
} = require('path')
const rimraf = require('rimraf')
const sharp = require('sharp')
sharp.cache(false)
const multer = require("multer")
const userRoutes = express.Router()

//folders
const rootFolder = join(__dirname, '../../')
const dataFolder = join(rootFolder, 'data')
const usersFolder = join(dataFolder, 'users')

const storage = multer.diskStorage({
  destination: function(req, file, callback) {
    callback(null, usersFolder)
  },
  filename: function(req, file, callback) {
    let fileComponents = file.originalname.split(".")
    let fileExtension = fileComponents[fileComponents.length - 1]
    let filename = `${file.originalname}_${Date.now()}.${fileExtension}`
github glacejs / glace-js / lib / image.js View on Github external
"use strict";
/**
 * Contains classes and functions to process images.
 *
 * @module
 */

var sharp = require("sharp");
var temp = require("temp");
var U = require("glacejs-utils");

sharp.cache(false);
/**
 * Creates new instance of Image.
 *
 * @class
 * @arg {string} srcPath - path to processed image
 * @arg {object} [srcOpts] - image options
 * @arg {number} [srcOpts.scaleX] - image current scale value on `X` axis
 * @arg {number} [srcOpts.scaleY] - image current scale value on `Y` axis
 */
var Image = module.exports = function (srcPath, srcOpts) {

    if (!(this instanceof Image))
        return new Image(srcPath, srcOpts);

    srcOpts = U.defVal(srcOpts, {});
    srcOpts.scaleX = U.defVal(srcOpts.scaleX, 1);
github glacejs / glace-js / lib / steps / image.js View on Github external
*
 * @module
 */

var fs = require("fs");
var path = require("path");

var _ = require("lodash");
var imageDiff = require("image-diff");
var sharp = require("sharp");
var uuid = require("uuid/v4");
var U = require("glacejs-utils");

var image = require("../image");

sharp.cache(false);
/**
 * Steps to manage web screenshots and images.
 *
 * @mixin ImageSteps
 */
module.exports = {
    /**
     * Step to make web screenshot in browser.
     *
     * @async
     * @method
     * @instance
     * @arg {object} [opts] - Step options.
     * @arg {string} [opts.imageName] - Screenshot name. File extension
     *  `.png` will be added automatically. Default value is dynamically
     *  generated on each call with algorithm `uuid`.
github ilios / frontend / lib / build-icons / generate-icons.js View on Github external
constructor(inputNode, options) {
    super([inputNode], {
      annotation: 'build-ilios-icons'
    });

    sharp.cache(false);
    this.options = options;
  }
github jrjohnson / ember-cli-image-transformer / lib / generate-icons.js View on Github external
constructor(inputNode, options) {
    super([inputNode], {
      annotation: 'ember-cli-image-transformer-build'
    });

    sharp.cache(false);
    this.options = options;
  }

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