Giter VIP home page Giter VIP logo

sharpie's Introduction

sharpie

Express middleware for resizing images with sharp and short url query parameters.

Supports content negotiation, and http caches.

Usage

import express from 'express';
import { sharpie as createSharpie } from 'sharpie';

const app = express();
const sharpie = createSharpie({
 param: 'url', // or an async function(req) that returns a file path
 q: 90,
 rs: "w:320,h:240,max",
 format: false, // negotiate format
 bg: 'white',
 crop: 'center',
 flatten: true,
 hostnames: false,
 ratio: 'minXMinY',
 sizes: '16,32,48', // sizes for ico format
 signs: {
  assignment: ':', // use ~ for better uri-encoding
  separator: ','  // use ! for better uri-encoding
 },
 https: {
   rejectUnauthorized: false
 }
});

// will get the url through req.params[opts.param] – e.g /param/foo.jpg
app.get('/param:url(*)', sharpie);
// will get the url through req.query[opts.param] – e.g. /query?url=/foo.jpg
app.get('/query', sharpie);

app.listen();

Query Options

It supports a limited subset of sharp options that can be given as parameters or as defaults when initializing the middleware:

  • format force destination format (jpeg, png, webp, avif, raw, svg, ...) If false, defaults to format of the original image, or negotiate best format.
  • q quality, renormalized, default 80
  • rs w:452,h=123,min w:452,h=123,max w:452,enlarge z:55,enlarge
  • bg the background color for flatten and resize defaults to no background color
  • fg fill color for svg (simpler than using style)
  • crop center, north, northeast, ...
  • flatten boolean
  • style appends a style tag with that content to the svg root
  • ratio sets preserveAspectRatio attribute, and if viewBox is missing, add it (provided width and height attributes, and optionally x, y, are present).
  • ex extracts a region of the image, given center x, y, width and height in % of the image. This means ex=x:50,y:50,w:100,h:100 extracts the full image.
  • mean the image has all pixels color set to the image average color. While this is not the "dominant" color, it can be useful as a placeholder.

Constructor options

  • param Key in req.params or req.query Or async req => filepath function.

  • formats list of allowed formats for negotiation. Default to ['svg', 'png', 'jpeg', 'webp'] To test avif encoding, pass formats: ['svg', 'png', 'jpeg', 'webp', 'avif'].

  • hostnames whitelist of other hostnames that can be proxied. True for all, false for none. Or a function, a map, an array - anything taking a hostname and returning a boolean.

  • sizes the sizes of the favicon in ico format, separated by a comma. defaults to 64,32,16.

Formats

  • jpeg: trellisQuantisation, overshootDeringing, and if image is > 10kb, optimizeScans
  • png: palette
  • webp: nearLossless when converting from png
  • avif: lossless when converting from png
  • ico is supported, see sizes and bg options.
  • svg: can be converted to another format, results may vary rs and ex are implemented if the root has viewBox, fg changes svg fill color.

Unrecognized types are proxied as is with a warning. (Future versions might change that behavior).

sharp

import { sharp } from 'sharpie';

Errors

All errors are HttpError instances, passed to next middleware.

sharpie's People

Contributors

bnf avatar kapouer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

bnf

sharpie's Issues

Add preserveAspectRatio attribute to svg when it has a viewBox

When using a svg file through sharpie it means it is embedded in a <img> tag:
the only expected behavior of an image is preserveAspectRatio="xMinYMin", so it should always be added if not present on the svg root node (and if viewBox attribute is defined, of course).

do html rendering using simplification of page ?

systematic definition of "simplifying" a page is tricky, though.
But that would fix the main problem with snapshottting a web page,
and would provide some kind of default thumbnail for pages which don't provide one.

throw 400 for invalid parameters

I like this one ;)

/.uploads/2018-09/plaquette_bague-abf218cb.jpg?q=%28SELECT%20CONCAT%280x7178627871%2C%28SELECT%20%28ELT%283299%3D3299%2C1%29%29%29%2C0x7162786271%29%29

svg rendering can fail sometimes

Error: Expected width > 0.
at Error (native)
at Rsvg.render (.../node_modules/sharpie/node_modules/librsvg/index.js:270:24)
at Rsvg.<anonymous> (../node_modules/sharpie/lib/sharpie.js:83:20)
at emitNone (events.js:72:20)
at Rsvg.emit (events.js:166:7)
at finishMaybe (_stream_writable.js:481:14)
at afterWrite (_stream_writable.js:355:3)
at nextTickCallbackWithManyArgs (node.js:464:18)
at process._tickCallback (node.js:362:17)

with this one:

<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 15.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Calque_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     width="217px" height="32px" viewBox="0 0 217 32" enable-background="new 0 0 217 32" xml:space="preserve">
<path fill="#FFFFFF" d="M143.604,16.082c0-3.582-1.209-6.055-3.637-7.412c-1.814-1.02-4.666-1.526-8.551-1.526h-8.932v17.898h8.932
    c3.91,0,6.76-0.513,8.551-1.542C142.395,22.164,143.604,19.693,143.604,16.082L143.604,16.082z M148.479,16.082
    c0,9.189-5.688,13.785-17.062,13.785h-13.806V2.264h13.806C142.791,2.264,148.479,6.869,148.479,16.082L148.479,16.082z"/>
<polygon fill="#FFFFFF" points="156.611,18.525 156.611,29.867 151.734,29.867 151.734,2.264 179.37,2.264 179.37,7.144 
    156.611,7.144 156.611,13.644 179.37,13.644 179.37,18.525 "/>
<polygon fill="#FFFFFF" points="212.126,29.867 212.126,12.613 202.146,29.867 197.463,29.867 187.48,12.613 187.48,29.867 
    182.607,29.867 182.607,2.264 187.121,2.264 199.805,24.227 212.468,2.264 217,2.264 217,29.867 "/>
<path fill="#FFFFFF" d="M113.322,26.205h-2.924v0.877h2.25c0.014,0.17,0.02,0.42,0.02,0.661c0,0.794-0.279,1.206-1.404,1.206h-1.412
    c-1.222,0-1.399-0.518-1.399-1.318c0-0.334,0.041-1.104,0.066-1.418h-0.707c-0.046,0.15-0.166,0.855-0.166,1.576
    c0,1.021,0.332,2.078,2.208,2.078h1.412c1.773,0,2.213-0.934,2.213-2.057C113.477,27.105,113.383,26.475,113.322,26.205
    L113.322,26.205z"/>
<path fill="#FFFFFF" d="M113.477,20.818l-2.465,1.426c-0.225-0.523-0.611-0.846-1.359-0.846h-0.4c-1.295,0-1.533,0.977-1.533,2.162
    c0,0.494,0.015,0.938,0.034,1.373h5.617v-0.898h-2.167V23.58c0-0.152,0-0.297-0.004-0.43l2.277-1.354V20.818L113.477,20.818z
     M109.657,22.302c0.635,0,0.815,0.378,0.815,1.226c0,0.174,0,0.34-0.009,0.51h-1.969c0-1.203,0.006-1.732,0.765-1.732
    L109.657,22.302L109.657,22.302z"/>
<path fill="#FFFFFF" d="M111.201,16.365h-1.291c-1.586,0-2.266,0.781-2.266,2.021c0,1.245,0.68,2.017,2.266,2.017h1.291
    c1.594,0,2.275-0.764,2.275-2.017C113.477,17.139,112.795,16.365,111.201,16.365L111.201,16.365z M111.201,17.285
    c1.104,0,1.475,0.438,1.475,1.104c0,0.66-0.369,1.098-1.475,1.098h-1.283c-1.097,0-1.468-0.428-1.468-1.098
    c0-0.674,0.371-1.104,1.468-1.104H111.201L111.201,17.285z"/>
<path fill="#FFFFFF" d="M111.363,11.348h-3.612v0.904h3.565c0.988,0,1.365,0.242,1.365,1.02c0,0.787-0.377,1.017-1.365,1.017h-3.565
    v0.913h3.612c1.412,0,2.113-0.679,2.113-1.93C113.477,12.027,112.775,11.348,111.363,11.348L111.363,11.348z"/>
<path fill="#FFFFFF" d="M109.688,6.46h-0.431c-1.308,0.009-1.54,0.96-1.54,2.166c0,0.474,0.008,0.909,0.034,1.38h5.617v-0.9h-2.125
    c0.005-0.191,0.005-0.362,0.005-0.541C111.248,7.42,111.006,6.46,109.688,6.46L109.688,6.46z M109.688,7.367
    c0.647,0,0.787,0.404,0.787,1.21c0,0.179,0,0.356-0.009,0.528h-1.969V8.973c0-1.062,0-1.6,0.76-1.605L109.688,7.367L109.688,7.367z"
    />
<polygon fill="#FFFFFF" points="113.368,2.264 112.609,2.264 112.609,4.465 110.861,4.465 110.861,2.402 110.123,2.402 
    110.123,4.465 108.517,4.465 108.517,2.264 107.751,2.264 107.751,5.391 113.368,5.391 "/>
<path fill="#EC5101" d="M12.532,16.064c0,1.875,1.516,3.393,3.388,3.393c1.87,0,3.387-1.518,3.387-3.393
    c0-1.872-1.517-3.393-3.387-3.393C14.048,12.672,12.532,14.192,12.532,16.064L12.532,16.064z M4.177,16.064h4.177
    c0-4.178,3.39-7.572,7.565-7.572v-4.18C9.445,4.312,4.177,9.582,4.177,16.064L4.177,16.064z M0,16.064
    C0,7.277,7.143,0.131,15.92,0.131c8.778,0,15.918,7.146,15.918,15.934C31.838,24.85,24.698,32,15.92,32C7.143,32,0,24.85,0,16.064
    L0,16.064z"/>
<path fill="#3C6FFF" d="M76.331,27.34c-6.21-6.219-6.208-16.334,0-22.549C80.79,0.33,87.253-0.931,92.843,1.015l-8.77,8.78
    c-0.734,0.734-1.697,1.104-2.741,1.119c-0.875,0.057-1.733,0.424-2.401,1.095c-1.47,1.47-1.47,3.858,0,5.329
    c1.468,1.473,3.852,1.473,5.322,0c0.665-0.67,1.031-1.525,1.108-2.485c0-0.966,0.366-1.93,1.102-2.667l9.609-9.619
    c0.987,0.623,1.922,1.365,2.78,2.225c6.211,6.215,6.211,16.33,0,22.547c-4.456,4.463-10.922,5.722-16.512,3.778l8.771-8.782
    c0.734-0.733,1.697-1.103,2.741-1.117c0.875-0.061,1.734-0.424,2.403-1.094c1.47-1.474,1.47-3.857,0-5.328
    c-1.468-1.475-3.853-1.475-5.324,0c-0.665,0.668-1.03,1.52-1.107,2.483c0,0.966-0.368,1.929-1.103,2.666l-9.608,9.617
    C78.124,28.943,77.189,28.201,76.331,27.34L76.331,27.34z"/>
<path fill="#FF0126" d="M44.288,8.518c0,0.936,0.757,1.691,1.693,1.691h11.635c0.937,0,1.693-0.756,1.693-1.691
    c0-0.932-0.757-1.688-1.693-1.688H45.981C45.045,6.83,44.288,7.586,44.288,8.518L44.288,8.518z M35.837,15.935
    C35.837,7.146,42.999,0,51.802,0c8.23,0,15.022,6.248,15.874,14.243H45.983c-0.937,0-1.695,0.757-1.695,1.688
    c0,0.936,0.759,1.694,1.695,1.694h21.692c-0.151,1.404-0.483,2.759-0.979,4.033H45.981c-0.937,0-1.693,0.757-1.693,1.693
    c0,0.933,0.757,1.688,1.693,1.688h18.91c-2.887,4.122-7.68,6.825-13.09,6.825C42.999,31.867,35.837,24.721,35.837,15.935
    L35.837,15.935z"/>
</svg>

Prohibit bad requests to arbitrary hosts

Currently sharpie may be used to perform requests to arbitrary hosts.
That means the server may be used as proxy for bad/criminal requests.

An array of whitelisted hosts passed as options would offer the possibility to restrict that.

support basic svg transformations

It can be very handy to change SVG root attributes (think width, height, stroke, fill).
Here sharpie wouldn't even have to really use an xml parser.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.