Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
merge-images
Advanced tools
Easily compose images together without messing around with canvas
Canvas can be kind of a pain to work with sometimes, especially if you just need a canvas context to do something relatively simple like merge some images together. merge-images
abstracts away all the repetitive tasks into one simple function call.
Images can be overlaid on top of each other and repositioned. The function returns a Promise which resolves to a base64 data URI. Supports both the browser and Node.js.
npm install --save merge-images
or for quick testing:
<script src="https://unpkg.com/merge-images"></script>
With the following images:
/body.png | /eyes.png | /mouth.png |
---|---|---|
You can do:
import mergeImages from 'merge-images';
mergeImages(['/body.png', '/eyes.png', '/mouth.png'])
.then(b64 => document.querySelector('img').src = b64);
// data:image/png;base64,iVBORw0KGgoAA...
And that would update the img
element to show this image:
Those source png images were already the right dimensions to be overlaid on top of each other. You can also supply an array of objects with x/y co-ords to manually position each image:
mergeImages([
{ src: 'body.png', x: 0, y: 0 },
{ src: 'eyes.png', x: 32, y: 0 },
{ src: 'mouth.png', x: 16, y: 0 }
])
.then(b64 => ...);
// data:image/png;base64,iVBORw0KGgoAA...
Using the same source images as above would output this:
The opacity can also be tweaked on each image.
mergeImages([
{ src: 'body.png' },
{ src: 'eyes.png', opacity: 0.7 },
{ src: 'mouth.png', opacity: 0.3 }
])
.then(b64 => ...);
// data:image/png;base64,iVBORw0KGgoAA...
By default the new image dimensions will be set to the width of the widest source image and the height of the tallest source image. You can manually specify your own dimensions in the options object:
mergeImages(['/body.png', '/eyes.png', '/mouth.png'], {
width: 128,
height: 128
})
.then(b64 => ...);
// data:image/png;base64,iVBORw0KGgoAA...
Which will look like this:
Usage in Node.js is the same, however you'll need to also require node-canvas and pass it in via the options object.
const mergeImages = require('merge-images');
const Canvas = require('canvas');
mergeImages(['./body.png', './eyes.png', './mouth.png'], {
Canvas: Canvas
})
.then(b64 => ...);
// data:image/png;base64,iVBORw0KGgoAA...
One thing to note is that you need to provide a valid image source for the node-canvas Image
rather than a DOM Image
. Notice the above example uses a file path, not a relative URL like the other examples. Check the node-canvas docs for more information on valid Image
sources.
Returns a Promise which resolves to a base64 data URI
Type: array
Default: []
Array of valid image sources for new Image()
.
Alternatively an array of objects with x
/y
co-ords and src
property with a valid image source.
Type: object
Type: string
Default: 'image/png'
A DOMString indicating the image format.
Type: number
Default: 0.92
A number between 0 and 1 indicating image quality if the requested format is image/jpeg or image/webp.
Type: number
Default: undefined
The width in pixels the rendered image should be. Defaults to the width of the widest source image.
Type: number
Default: undefined
The height in pixels the rendered image should be. Defaults to the height of the tallest source image.
Type: Canvas
Default: undefined
Canvas implementation to be used to allow usage outside of the browser. e.g Node.js with node-canvas.
MIT © Luke Childs
FAQs
Easily compose images together without messing around with canvas
The npm package merge-images receives a total of 37,426 weekly downloads. As such, merge-images popularity was classified as popular.
We found that merge-images demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.