The Youzu Magic Background Component enhances product images by adding appropriate backgrounds.
Add the <youzu-magic-background> tag in place of a standard <img> element:
<youzu-magic-background
src ="https://example.com/product.jpg"
alt ="Product Image"
mode ="auto"
style-preset ="modern"
width ="100%" >
</youzu-magic-background >
html
SDK JavaScript Python cURL
<youzu-magic-background
src ="https://example.com/product.jpg"
alt ="Product Image"
mode ="auto"
style-preset ="modern"
width ="100%" >
</youzu-magic-background >
html
const magicBackground = document .querySelector ('youzu-magic-background' );
magicBackground.addEventListener ('backgroundChange' , event => {
console .log ('Background changed:' , event.detail );
});
async function applyBackground () {
try {
await magicBackground.applyBackground ({
stylePreset : 'modern'
});
console .log ('Background applied' );
} catch (error) {
console .error ('Error applying background:' , error);
}
}
javascript
const imageData = {
image_url : 'https://example.com/product.jpg'
};
fetch ('https://platform-api.youzu.ai/v1/generation/magic-background' , {
method : 'POST' ,
headers : {
'Authorization' : 'Bearer YOUR_ACCESS_TOKEN' ,
'x-client-id' : 'YOUR_CLIENT_ID' ,
'Content-Type' : 'application/json'
},
body : JSON .stringify (imageData)
})
.then (response => response.json ())
.then (data => {
console .log ('Magic background generation completed:' , data);
})
.catch (error => {
console .error ('Error:' , error);
});
javascript
import requests
import json
image_data = {
'image_url' : 'https://example.com/product.jpg'
}
headers = {
'Authorization' : 'Bearer YOUR_ACCESS_TOKEN' ,
'x-client-id' : 'YOUR_CLIENT_ID' ,
'Content-Type' : 'application/json'
}
response = requests.post(
'https://platform-api.youzu.ai/v1/generation/magic-background' ,
headers=headers,
data=json.dumps(image_data)
)
if response.status_code == 200 :
generation = response.json()
print ('Magic background generation completed:' , generation)
else :
print ('Error:' , response.status_code, response.text)
python
curl -X POST https://platform-api.youzu.ai/v1/generation/magic-background \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "x-client-id: YOUR_CLIENT_ID" \
-H "Content-Type: application/json" \
-d '{
"image_url": "https://example.com/product.jpg"
}'
bash
Property
Attribute
Description
Type
Default
alt
alt
The alt text for the image
string
''
apiEndpoint
api-endpoint
The API endpoint for background processing
string
'/api/background'
height
height
The height of the image
string
undefined
hideLoading
hide-loading
Whether to disable the loading indicator
boolean
false
mode
mode
The mode to control background application behavior - auto: Ask server if background is needed, add if necessary - always: Always add a background - manual: No automatic requests, use applyBackground() method
`"always"
"auto"
showLoader
show-loader
Manually control the loading state (for demo purposes)
boolean
false
src
src
The source URL of the image
string
undefined
stylePreset
style-preset
The style preset to use for the background Default is 'modern'
string
'modern'
width
width
The width of the image
string
undefined
Event
Description
Type
backgroundChange
Event emitted when a background is applied or removed
CustomEvent<BackgroundChangeEventData>
Method
Parameters
Return Type
Description
applyBackground
options?: BackgroundOptions
Promise<string>
Public method to manually apply a background Can be called in manual mode
<div style ="width: 400px;" >
<youzu-magic-background
id ="manualBackground"
src ="https://example.com/product.jpg"
alt ="Product example"
mode ="manual"
width ="400px" >
</youzu-magic-background >
<div style ="margin-top: 15px; display: flex; gap: 10px; flex-wrap: wrap; justify-content: center;" >
<button id ="applyBtn" style ="padding: 8px 16px; background-color: #4caf50; color: white; border: none; border-radius: 4px; cursor: pointer;" >Apply Background</button >
<button id ="resetBtn" style ="padding: 8px 16px; background-color: #f44336; color: white; border: none; border-radius: 4px; cursor: pointer;" >Reset Image</button >
</div >
</div >
<script >
document .addEventListener ('DOMContentLoaded' , () => {
const background = document .getElementById ('manualBackground' );
const applyBtn = document .getElementById ('applyBtn' );
const resetBtn = document .getElementById ('resetBtn' );
if (background && applyBtn && resetBtn) {
applyBtn.addEventListener ('click' , async () => {
try {
applyBtn.textContent = 'Applying...' ;
applyBtn.disabled = true ;
await background.applyBackground ({
stylePreset : 'modern' ,
demoMode : true ,
demoDelay : 2000 ,
});
applyBtn.textContent = 'Apply Background' ;
applyBtn.disabled = false ;
} catch (error) {
console .error ('Error applying background:' , error);
applyBtn.textContent = 'Apply Background' ;
applyBtn.disabled = false ;
}
});
resetBtn.addEventListener ('click' , async () => {
try {
resetBtn.textContent = 'Resetting...' ;
resetBtn.disabled = true ;
background.setAttribute ('src' , background.getAttribute ('src' ));
resetBtn.textContent = 'Reset Image' ;
resetBtn.disabled = false ;
} catch (error) {
console .error ('Error resetting image:' , error);
resetBtn.textContent = 'Reset Image' ;
resetBtn.disabled = false ;
}
});
}
});
</script >
html