@platinumcloud/image-editor
Open the image editor at image.platinumcloud.net from a button on your site and get the edited image back as a File.
- The editor opens in a popup (a new tab on phones). The image travels between the two windows inside the browser and is never uploaded.
- Crop, rotate, color adjustment, mosaic and text, in English, Japanese, Korean and Chinese.
- Every option is optional. You can fix the frame's shape or exact pixel size, the format and the quality, and limit the tools on offer.
- No dependencies, no API key, free.
Install
npm install @platinumcloud/image-editor
Usage
The simplest call takes no arguments. The person chooses an image inside the editor (file picker, drag and drop or paste), edits it, and you get the result:
import { editImage } from '@platinumcloud/image-editor';
button.addEventListener('click', async () => {
// Call editImage() straight from the click handler: browsers only allow popups during a user gesture.
const edited = await editImage();
if (edited) upload(edited); // a File, or null when the person cancelled or closed the editor
});
Everything goes in one options object, and every option is optional. To edit an image you already have, pass it as file:
await editImage({ file });
await editImage({ file, size: '1920x1080', format: ['webp', 'jpg'] });
await editImage({ size: '16:9' }); // the person chooses the image, cropped to 16:9
If the browser blocks the popup, the promise rejects with PopupBlockedError:
import { editImage, PopupBlockedError } from '@platinumcloud/image-editor';
try {
const edited = await editImage();
} catch (error) {
if (error instanceof PopupBlockedError) alert('Allow popups for this site to edit the image.');
else throw error;
}
The module does not touch window until editImage is called, so it can be imported in server-rendered code (Next.js, Nuxt, SvelteKit and so on).
Options
Every option is optional.
| Option |
Type |
Description |
file |
File or Blob |
The image to edit. Default: the person chooses one in the editor. |
lang |
'en' \| 'ja' \| 'ko' \| 'zh-cn' \| 'zh-tw' |
Language of the editor. Default: the browser's language. |
format |
'jpg' \| 'png' \| 'webp' \| 'avif', or an array |
Format of the result. With an array, the person chooses from those formats (the first is preselected; ones the browser cannot write are left out). Default: the input's format. |
quality |
'max' \| 'high' \| 'standard' \| 'small', or 0–1 |
Encoding quality. Default: 'high'. |
size |
'16:9' or '1920x1080' |
Fixed crop frame. '16:9' fixes the shape only; '1920x1080' fixes the exact output size in pixels. Default: free. |
tools |
('crop' \| 'rotate' \| 'adjust' \| 'mosaic' \| 'text')[] |
Tools to offer. Default: all. |
name |
string |
File name shown in the editor and given to the result. |
Button images
The package includes the button images (200×40 px, drawn at 2×), so your bundler can serve them from your own site:
import badge from '@platinumcloud/image-editor/badge/image-editor-black.png';
// or image-editor-white.png for dark backgrounds
<button type="button" onClick={onEdit} style={{ padding: 0, border: 0, background: 'none' }}>
<img src={badge} width={200} height={40} alt="Edit with Online Image Editor" />
</button>
In Next.js the import is an object: use badge.src. Please keep the images' colours and wording as they are.
Requirements for the calling page
- Call
editImage() directly in a click handler. Awaiting something first can make the browser block the popup, which rejects with PopupBlockedError.
- Do not send
Cross-Origin-Opener-Policy: same-origin; it cuts the link to the editor window. same-origin-allow-popups works.
- The editor keeps to your options, but the result arrives through the browser: check its type and size before you trust it.
Without a bundler
The same code is served as a script that defines window.PlatinumImage:
<script src="https://image.platinumcloud.net/embed/sdk.js"></script>
<script>
const edited = await PlatinumImage.edit();
</script>
Full documentation, button images and a demo: https://image.platinumcloud.net/embed/docs/
License
MIT