Image

Images

Add, edit, and generate images.

Pictures come from the user's computer, from stock photo services, or from an AI model. On the page, the user can crop, fit, and filter a picture, and remove its background.

Setup

Upload, crop, fit modes, and filters work with no setup. The other options need a key, a server, or an AI provider, which you pass as props.

import { ImageEditor } from '@layermetry/media-editor/react';

export default function App() {
  return (
    <div style={{ position: 'fixed', inset: 0 }}>
      <ImageEditor
        licenseKey="YOUR_LICENSE_KEY"
        onClose={() => console.log('The editor was closed')}
        onExport={(file) => console.log(`Exported ${file.name}`)}
        // Background removal: your media server.
        mediaApiUrl="https://media.example.com"
        // Stock photos: your Pexels and Unsplash keys.
        stockMediaApis={{
          pexels: { apiKey: 'YOUR_PEXELS_KEY' },
          unsplash: { apiKey: 'YOUR_UNSPLASH_ACCESS_KEY', showAttribution: true },
        }}
        // Image generation and AI image editing: your AI proxy.
        ai={{ endpointUrl: 'https://api.example.com/ai' }}
      />
    </div>
  );
}
OptionPropConfiguration page
Stock photosstockMediaApisStock media
Background removalmediaApiUrlAI providers
Image generation, AI image editingaiAI providers

Upload

The Upload Images button in the upload panel opens the file picker. The user can choose several files at once.

The panel lists JPG, PNG, GIF, and WebP. A file that is not an image is refused with the message "Invalid file type".

A new picture is placed with the Fit mode, so the whole picture shows on the page.

The image upload panel with upload and generation controls beside the example posterView full size

Stock photos

Requires a Pexels key, an Unsplash key, or both. See Stock media.

With stockMediaApis set, the upload panel shows a Stock Images tab, with one tab for each service. A search asks for pictures in the orientation of the page: landscape, portrait, or square.

stockMediaApis={{
  pexels: { apiKey: 'YOUR_PEXELS_KEY' },
  unsplash: { apiKey: 'YOUR_UNSPLASH_ACCESS_KEY', showAttribution: true },
}}

Unsplash requires showAttribution: true, so the photographer's name is shown with each picture.

An apiKey is visible to anyone who opens the page. In production, pass endpointUrl instead, so that the key stays on your server. See Stock media.

Crop

Crop shows part of a picture. Select a picture and press Crop in the toolbar above the page, or choose Crop in the right-click menu. In the compact layout, Crop is a chip under the picture. A frame opens over the picture. Drag the frame or its handles to choose the part that shows.

Press Done, press Enter, or click outside the frame to apply the crop. Press Cancel or Esc to leave the picture as it was. Clear crop shows the whole picture again. A crop is one step to undo, and the exported file shows exactly the same part of the picture.

A saved design keeps the crop in a crop field on the picture, { x, y, width, height }, counted in the picture's own pixels, not in page pixels. Store it with the rest of the record.

Fit modes

A fit mode decides how a picture fills the page. Select a picture and press Image Fitting Options in the toolbar above the page.

ModeWhat it does
FitShows the whole picture. Borders appear if the shapes differ. This is the default.
FillFills the page and cuts off what does not fit.
StretchFills the page and changes the picture's proportions if needed.
CenterKeeps the picture at its own size, in the center. Borders can appear.
Four actual canvas views comparing Fit, Fill, Stretch and Center for the same pictureView full size

Filters and adjustments

Select a picture and open the Effects panel. Four filters switch on and off: Grayscale, Sepia, Invert, and Emboss.

Eleven adjustments take a number:

AdjustmentRange
Blur0 to 10
Brightness0 to 200
Noise0 to 100
Pixel Size1 to 10
Contrast0 to 2
Corner Radius0 to 100
Shadow0 to 50
Offset X, Offset Y-50 to 50
Shadow Offset X, Shadow Offset Y-50 to 50

Filters and adjustments appear in the exported file.

The Effects panel opened for the selected campaign photographView full size

Resize handles

A selected picture, GIF, or sticker has eight resize handles: four at the corners and four at the sides. All eight keep the shape of the item. A cropped picture keeps the shape of its crop. A turned item grows along its own edges.

When the user drags a side handle, the item grows equally on both sides of the dragged edge. The opposite edge stays where it is. When the user holds Alt (Option on a Mac), the item grows around its middle.

To stretch an item on purpose, hold Shift while dragging a side handle or a corner handle. The user can press or release Shift in the middle of a drag. The change takes effect at once, and releasing Shift brings back the shape that the item had when the drag started.

Text, shapes, lines, and a selection that mixes these with pictures keep their earlier behavior. A very wide or very tall item reaches the smallest size sooner, because its short side shrinks with it. For example, a banner of 1000 × 20 cannot be made narrower than 250 from a side handle without Shift.

On a phone there is no Shift key. To change the shape of a picture there, use Stretch in the Fit options, which fits the picture to the shape of the page, or crop the picture. See Fit modes.

Background removal

Connect your own media server with mediaApiUrl. ai={{ hosted: true }} uses Layermetry’s service. Hosted AI is coming soon. Available only with an enterprise licence from Layermetry. See AI providers.

Select a picture and press Remove Background in the toolbar, or choose Remove background in the right-click menu. The editor replaces the picture with the one your server returns, at the same place and size.

Image generation

Requires an AI provider, passed as ai. See AI providers.

With ai set, the upload panel shows Generate image. The user describes the picture and presses Generate.

The results appear in the box. Choose a picture and select Add to canvas to place it. Generate more appends results; Try again replaces the current choices. Every picture made in the session stays in the Generated tab.

ai={{ endpointUrl: 'https://api.example.com/ai' }}

AI image editing

Requires an AI provider, passed as ai. See AI providers.

Select a picture and press Edit with AI in the toolbar, or choose Edit with AI… in the right-click menu. The user describes the change, for example "put two passengers in the boat", and presses Generate.

The box offers starts such as Change the background and Add something. The first suggestion previews on the picture. Click a card to compare it; choose Original to restore the source. Closing the box keeps the selected result as one editing action, with one Undo step.

The request has "operation": "imageEdit" and carries the picture in input.source. The answer has the same { items: [{ url }] } format as image generation.

When you pass onUploadAsset, the editor gives it the new picture before the picture is replaced, so the design keeps your own address. Without onUploadAsset, the picture's data is saved inside the design. See Asset storage.

Upscaling

Coming soon

Upscaling will increase the resolution of a picture.

Masks

Coming soon

Masks will hide part of a picture behind a shape.

Configuration

PropTypeDefaultDescription
stockMediaApisobjectnonePexels and Unsplash settings. Without it, the Stock Images tab does not appear.
mediaApiUrlstringnoneThe address of the media server that removes backgrounds. ai={{ hosted: true }} uses the Layermetry address. Hosted AI is coming soon. Available only with an enterprise licence from Layermetry.
aiobjectnoneThe AI provider. Without it, no AI control appears and no AI request is sent.
onUploadAssetfunctionnoneStores pictures that were changed with AI in your own storage.

Events

Images have no events of their own. The editor calls onUploadAsset when the user keeps a picture changed with AI. The finished design reaches you through onExport. See Events.

Troubleshooting

Additional resources

Watch: open and close crop

Select a picture and choose Crop. Drag the frame to choose what shows, then choose Done.

Crop a picture and compare the result.

On this page