Skip to content

Create From Image

Starting from an existing image allows you to use the editor for customizing individual assets. This is done by using suspend fun createFromImage(imageUri: URI, dpi: Float = 300F, pixelScaleFactor: Float = 1F): DesignBlock and passing a URI as argument. The dpi argument sets the dots per inch of the scene. The pixelScaleFactor sets the display’s pixel scale factor.

Create a Scene From a Remote URL

In this example, we will show you how to initialize the CreativeEditor SDK with an initial image.

Specify the source to use for the initial image. This can be a relative path or a remote URL.

await engine.scene.createFromImage(

We can retrieve the graphic block id of this initial image using cesdk.engine.block.findByType(type: ObjectType): number[]. Note that that function returns an array. Since there’s only a single graphic block in the scene, the block is at index 0.

// Find the automatically added graphic block in the scene that contains the image fill.
const block = engine.block.findByType('graphic')[0];

We can then manipulate and modify this block. Here we modify its opacity with cesdk.engine.block.setOpacity(id: number, opacity: number): void. See Modifying Scenes for more details.

// Change its opacity.
engine.block.setOpacity(block, 0.5);

When starting with an initial image, the scene’s page dimensions match the given resource and the scene is configured to be in pixel design units.

To later save your scene, see Saving Scenes.

Full Code

Here’s the full code:

import CreativeEngine from 'https://cdn.img.ly/packages/imgly/cesdk-engine/1.51.0/index.js';
const config = {
license: 'mtLT-_GJwMhE7LDnO8KKEma7qSuzWuDxiKuQcxHKmz3fjaXWY2lT3o3Z2VdL5twm',
userId: 'guides-user',
baseURL: 'https://cdn.img.ly/packages/imgly/cesdk-engine/1.51.0/assets',
};
CreativeEngine.init(config).then(async engine => {
await engine.scene.createFromImage(
'https://img.ly/static/ubq_samples/sample_4.jpg',
);
// Find the automatically added graphic block in the scene that contains the image fill.
const block = engine.block.findByType('graphic')[0];
// Change its opacity.
engine.block.setOpacity(block, 0.5);
// Attach engine canvas to DOM
document.getElementById('cesdk_container').append(engine.element);
});

Create a Scene From an HTMLImageElement

In this example, we will show you how to initialize the CreativeEditor SDK with an initial image provided from an image element.

To use the image shown by an image element as the initial image, use the image element’s src attribute as the imageURL.

const element = document.getElementById('image-element');
const imageURL = element.src;

Use the created URL as a source for the initial image.

let scene = await engine.scene.createFromImage(objectURL);

We can retrieve the graphic block id of this initial image using cesdk.engine.block.findByType(type: ObjectType): number[]. Note that that function returns an array. Since there’s only a single graphic block in the scene, the id is at index 0.

// Find the automatically added graphic block with an image fill in the scene.
const block = engine.block.findByType('graphic')[0];

We can then manipulate and modify this block. Here we modify its opacity with cesdk.engine.block.setOpacity(id: number, opacity: number): void. See Modifying Scenes for more details.

// Change its opacity.
engine.block.setOpacity(block, 0.5);

When starting with an initial image, the scenes page dimensions match the given image and the scene is configured to be in pixel design units.

To later save your scene, see Saving Scenes.

Full Code

Here’s the full code:

import CreativeEngine from 'https://cdn.img.ly/packages/imgly/cesdk-engine/1.51.0/index.js';
const element = document.getElementById('image-element');
const imageURL = element.src;
const config = {
license: 'mtLT-_GJwMhE7LDnO8KKEma7qSuzWuDxiKuQcxHKmz3fjaXWY2lT3o3Z2VdL5twm',
userId: 'guides-user',
baseURL: 'https://cdn.img.ly/packages/imgly/cesdk-engine/1.51.0/assets',
};
CreativeEngine.init(config).then(async engine => {
let scene = await engine.scene.createFromImage(imageURL);
// Find the automatically added graphic block with an image fill in the scene.
const block = engine.block.findByType('graphic')[0];
// Change its opacity.
engine.block.setOpacity(block, 0.5);
// Attach engine canvas to DOM
document.getElementById('cesdk_container').append(engine.element);
});

Create a Scene From a Blob

In this example, we will show you how to initialize the CreativeEditor SDK with an initial image provided from a blob.

First, get hold of a blob by fetching an image from the web. This is just for demonstration purposes and your blob object may come from a different source.

const blob = await fetch('https://img.ly/static/ubq_samples/sample_4.jpg').then(
(response) => response.blob()
);

Afterward, create a URL pointing to the blob via URL.createObjectURL.

const objectURL = URL.createObjectURL(blob);

Use the created URL as a source for the initial image.

let scene = await engine.scene.createFromImage(objectURL);

We can retrieve the graphic block id of this initial image using cesdk.engine.block.findByType(type: ObjectType): number[]. Note that that function returns an array. Since there’s only a single graphic block in the scene, the block is at index 0.

// Find the automatically added graphic block in the scene that contains the image fill.
const block = engine.block.findByType('graphic')[0];

We can then manipulate and modify this block. Here we modify its opacity with cesdk.engine.block.setOpacity(id: number, opacity: number): void. See Modifying Scenes for more details.

// Change its opacity.
engine.block.setOpacity(block, 0.5);

When starting with an initial image, the scenes page dimensions match the given image, and the scene is configured to be in pixel design units.

To later save your scene, see Saving Scenes.

Full Code

Here’s the full code:

import CreativeEngine from 'https://cdn.img.ly/packages/imgly/cesdk-engine/1.51.0/index.js';
const blob = await fetch('https://img.ly/static/ubq_samples/sample_4.jpg').then(
response => response.blob(),
);
const objectURL = URL.createObjectURL(blob);
const config = {
license: 'mtLT-_GJwMhE7LDnO8KKEma7qSuzWuDxiKuQcxHKmz3fjaXWY2lT3o3Z2VdL5twm',
userId: 'guides-user',
baseURL: 'https://cdn.img.ly/packages/imgly/cesdk-engine/1.51.0/assets',
};
CreativeEngine.init(config).then(async engine => {
let scene = await engine.scene.createFromImage(objectURL);
// Find the automatically added graphic block in the scene that contains the image fill.
const block = engine.block.findByType('graphic')[0];
// Change its opacity.
engine.block.setOpacity(block, 0.5);
// Attach engine canvas to DOM
document.getElementById('cesdk_container').append(engine.element);
});