Saving the result
PaperPic never uploads or stores anything. When the user applies the edits, it hands the result to your code, and your app decides where it goes. This page covers what you receive and how to save it.
The apply event
Apply computes the result and fires apply with:
| Field | Type | Description |
|---|---|---|
blob | Blob | The edited image |
type | string | Its format, such as 'image/png' |
width, height | number | Its size in pixels |
pic.on('apply', ({ blob, type }) => upload(blob, type))After Apply, the result is the image on display, the history is empty, and the next edit starts from the result. reload() brings back the original src.
Upload it
Send the blob as a file in a form:
async function upload(blob: Blob, type: string) {
const body = new FormData()
body.append('image', blob, `document.${type.split('/')[1]}`)
const response = await fetch('/api/documents', { method: 'POST', body })
if (!response.ok) {
throw new Error(`The upload failed with status ${response.status}`)
}
}The viewer shows the result at once, whether or not it reaches your server, so tell the user when the upload fails.
Format and quality
Without options, the result keeps the format of the source when it is JPEG, PNG or WebP. PaperPic reads it from the extension of the URL, such as .jpg, or from the type of the File or Blob. A document in black and white is PNG, because JPEG would add artifacts around its sharp edges. Any other source gives PNG too, such as a GIF, or a URL without an extension like /api/documents/42, even when it serves a JPEG.
output chooses the format, and its quality from 0 to 1 for JPEG and WebP, with 0.92 by default:
const pic = new PaperPic(container, {
src: '/receipt.jpg',
output: { type: 'image/webp', quality: 0.8 },
})| Format | Suits |
|---|---|
image/png | Documents in black and white, and text, with no loss |
image/jpeg | Photos and colored documents, in small files |
image/webp | Photos and colored documents, in smaller files than JPEG |
A browser that cannot encode the format you ask for gives PNG, so read type in the event.
Size
The result keeps the full resolution of the source, which is 12 megapixels or more for many phone photos. maxOutputSize limits its longest side, in pixels:
const pic = new PaperPic(container, {
src: '/receipt.jpg',
maxOutputSize: 2000,
})The preview while editing has at most about 2 megapixels, so maxOutputSize changes only the result.
Safari on iOS limits a canvas to about 16 megapixels, and recent iPhones take photos of 24 megapixels or more, so Apply can fail at full resolution. If your users edit photos from an iPhone, set maxOutputSize. 3000 pixels keeps documents readable and stays well below the limit. Apply fails with a large photo shows the error.
Metadata
The result of Apply carries no metadata. PaperPic draws the image on a canvas, so the EXIF data of the photo, such as the GPS position, the camera and the date, is gone. The download button saves the source as it is until the first Apply, with its metadata.
The download button
download: true adds a button at the end of the toolbar that saves the image on display: the result of the last Apply, or else the source as it is. It is hidden while editing, because the edits are not applied yet. download() does the same from code, with or without the button. The browser's context menu does not open over the viewer, so this button is the way to save the image from it.
The file keeps the name of the source and takes the extension of its format, so a receipt.jpg in black and white saves as receipt.png. filename sets another name. Without any name, such as for a Blob, the file is called image. Before the first Apply, a URL without an extension saves without one, because its format is unknown.
With a src from another origin, browsers do not save the file before the first Apply: they open the image instead, and the user leaves your page. Pass the image as a Blob, as Loading images shows, or save it yourself in the download event.
Save it another way
The download event comes before the browser saves the file. Call preventDefault() to save it another way, such as from your server, which can name the file and check who downloads it:
pic.on('download', ({ preventDefault }) => {
preventDefault()
window.location.assign('/api/documents/42/file')
})