PHP code example of yongfook / bannerbear
1. Go to this page and download the library: Download yongfook/bannerbear library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yongfook / bannerbear example snippets
$bb = new Bannerbear\V5\Client("your V5 API key");
$bb = new Bannerbear\V5\Client();
$bb->account();
$bb->list_image_templates(1);
$bb->get_image_template("template uid");
$bb->create_image_template([
"name" => "My Template",
"description" => "Created from the API",
"tags" => ["portrait"],
"width" => 1080,
"height" => 1080,
"config" => ["objects" => [
["id" => "bg", "type" => "rectangle", "left" => 0, "top" => 0,
"width" => 1080, "height" => 1080, "background-color" => "#0f172a"],
["id" => "headline", "type" => "text", "left" => 80, "top" => 400, "width" => 920,
"text" => "Hello World!", "font-size" => 72, "color" => "#ffffff"],
]],
]);
$bb->update_image_template("template uid", [
"name" => "New Name",
"description" => "...",
"tags" => ["portrait"],
]);
$bb->delete_image_template("template uid");
$bb->create_image("template uid", [
"modifications" => [
"template" => ["width" => 1080, "height" => 1080],
"objects" => [
["name" => "headline", "text" => "Hello World!"],
[
"name" => "photo",
"image_url" => "https://images.unsplash.com/photo-1555400038-63f5ba517a47?w=1000&q=80",
],
],
],
]);
$bb->create_image("template uid", ["modifications" => ["objects" => [...]]], true);
$bb->get_image("image uid");
$bb->list_images(1);
$bb->list_animation_templates(1);
$bb->get_animation_template("template uid");
$bb->create_animation_template([
"name" => "My Animation",
"description" => "Created from the API",
"tags" => ["promo"],
"width" => 1080,
"height" => 1080,
"frame_rate" => 30,
]);
$bb->update_animation_template("template uid", ["name" => "New Name", "frame_rate" => 60]);
$bb->delete_animation_template("template uid");
$animation = $bb->create_animation("animation template uid", [
"modifications" => [
"template" => ["width" => 1080, "height" => 1080, "fps" => 30],
"objects" => [
["name" => "headline", "text" => "Hello World!"],
],
],
"formats" => ["mp4"],
]);
$animation = $bb->get_animation($animation["uid"]);
$animation["status"]; // "queued" | "rendering" | "completed" | "failed"
if ($animation["status"] === "completed") {
echo $animation["files"]["mp4"];
}
$bb->list_animations(1);
$bb->list_workflows(1);
$workflow = $bb->get_workflow("workflow uid");
$workflow["inputs"]; // the inputs this workflow declares
$workflow["steps"];
$run = $bb->create_workflow_run("workflow uid", [
"inputs" => [
"headline" => "Hello World!",
"photo" => "https://example.com/photo.jpg",
],
]);
$run = $bb->get_workflow_run($run["uid"]);
$run["status"]; // "queued" | "running" | "completed" | "failed"
if ($run["status"] === "completed") {
print_r($run["outputs"]);
}
$bb->list_workflow_runs(1);
$job = $bb->trim_video([
"video_url" => "https://example.com/clip.mp4",
"start" => 2.5,
"end" => 10.0,
]);
$job = $bb->get_tool_job($job["uid"]);
$job["status"]; // "pending" | "running" | "completed" | "failed"
if ($job["status"] === "completed") {
echo $job["outputs"]["video_url"];
}
$bb->list_tool_jobs(1);
$bb->remove_bg(["image_url" => "https://example.com/product.png"]);
// Corner placement, 40px in from each edge
$bb->overlay_image([
"video_url" => "https://example.com/clip.mp4",
"image_url" => "https://example.com/logo.png",
"position" => "bottom_right",
"margin" => 40,
"opacity" => 0.8,
]);
$bb->subtitle_video([
"video_url" => "https://example.com/talk.mp4",
"font" => "montserrat",
"font_size" => 32,
"color" => "#ffffff",
"background_style" => "outline",
"alignment" => "2",
]);
$bb->generate_voiceover(["text" => "Welcome to Bannerbear.", "voice" => "rachel"]);
$bb->create_video_slideshow([
"image_urls" => ["https://example.com/1.jpg", "https://example.com/2.jpg"],
"slide_duration" => 3,
"transition" => "fade",
"width" => 1280,
"height" => 720,
]);
$bb->create_tool_job("remove_bg", ["image_url" => "https://example.com/product.png"]);
$asset = $bb->upload_asset(file_get_contents("logo.png"), "image/png");
echo $asset["url"];
$bb->get_asset("asset uid");
$bb->list_assets(1);
$data = file_get_contents("logo.png");
$digest = hash("sha256", $data);
$found = $bb->check_assets([$digest]);
if ($found[$digest] === null) {
$bb->upload_asset($data, "image/png");
}
$bb->list_publications(1);
$bb->get_publication("publication uid");
$template = $bb->install_publication("publication uid");
echo $template["uid"];
$bb->create_batch([
"type" => "images",
"items" => [
["template" => "template uid 1", "modifications" => ["objects" => [...]]],
["template" => "template uid 2", "modifications" => ["objects" => [...]]],
],
]);
$bb->get_batch("batch uid");
$bb->list_batches(1);
$hook = $bb->create_webhook([
"name" => "my-webhook",
"url" => "https://example.com/hook",
"resource" => "image",
"event" => "completed",
"status" => "active",
]);
// IMPORTANT: signing_key is ONLY returned in the create response. Store it now —
// subsequent get_webhook calls will not
$bb->get_webhook("webhook uid");
$bb->update_webhook("webhook uid", [
"name" => "renamed",
"url" => "https://example.com/hook",
"resource" => "image",
"event" => "completed",
"status" => "active",
]);
$bb->delete_webhook("webhook uid");
$bb->list_webhooks(1);
$iurl = $bb->create_instant_url([
"name" => "my-instant-url",
"template" => "template uid",
"mode" => "encoded", // or "named_params"
"security" => "signed", // or "open"
"status" => "active",
"scale" => 1, // 1, 2, 3, or 4
]);
// IMPORTANT: signing_key is ONLY returned in the create response. Store it now.
echo $iurl["signing_key"];
echo $iurl["base_url"];
$bb->get_instant_url("uid");
$bb->update_instant_url("uid", ["name" => "...", "template" => "...", /* ... */]);
$bb->delete_instant_url("uid");
$bb->list_instant_urls(1);
// Encoded mode, signed
$bb->build_instant_url($iurl["base_url"], [
"mode" => "encoded",
"signing_key" => $iurl["signing_key"],
"modifications" => [
"template" => ["width" => 1030, "height" => 890],
"objects" => [["name" => "title", "text" => "Hello!", "color" => "#ffffff"]],
],
]);
// Named params mode, signed
$bb->build_instant_url($iurl["base_url"], [
"mode" => "named_params",
"signing_key" => $iurl["signing_key"],
"modifications" => [
"template" => ["width" => 1030, "height" => 890],
"objects" => [["name" => "title", "text" => "Hello!"]],
],
]);
// Open (unsigned): omit signing_key
$bb->build_instant_url($iurl["base_url"], [
"mode" => "encoded",
"modifications" => ["objects" => [["name" => "title", "text" => "Hello!"]]],
]);
$bb = new Bannerbear\BannerbearClient("your api key");
$bb = new Bannerbear\BannerbearClient();
$account = $bb->account();
$bb->create_image("template uid", [
"modifications" => [
[
"name" => "headline",
"text" => "Hello world!",
],
[
"name" => "photo",
"image_url" =>
"https://images.unsplash.com/photo-1555400038-63f5ba517a47?w=1000&q=80",
],
],
]);
$bb->create_image(
"template uid",
[
"modifications" => [
[
"name" => "headline",
"text" => "Hello world!",
],
[
"name" => "photo",
"image_url" =>
"https://images.unsplash.com/photo-1555400038-63f5ba517a47?w=1000&q=80",
],
],
],
TRUE
);
$bb->get_image("image uid");
$bb->list_images();
$bb->list_images(10, 25);
$bb->create_video("video template uid", [
"input_media_url" => "https://www.yourserver.com/videos/awesome_video.mp4",
"modifications" => [
[
"name" => "headline",
"text" => "Hello world",
],
],
]);
$bb->get_video("video uid");
$bb->update_video("video uid", [
"approved" => true,
"transcription" => [
"This is a new transcription",
"It must contain the same number of lines",
"As the previous transcription",
],
]);
$bb->list_videos();
$bb->get_collection("collection uid");
$bb->list_collections(3);
$bb->create_collection(
"template set uid",
[
"modifications" => [
[
"name" => "headline",
"text" => "Hello World!",
],
],
],
true
);
$bb->get_animated_gif("gif uid")
$bb->list_animated_gifs(3)
$bb->create_animated_gif("template uid", [
"frames" => [
[ // frame 1 starts here
[
"name" => "layer1",
"text" => "This is my text"
],
[
"name" => "photo",
"image_url" => "https//www.pathtomyphoto.com/1.jpg"
]
],
[ // frame 2 starts here
[
"name" => "layer1",
"text" => "This is my follow up text"
],
[
"name" => "photo",
"image_url" => "https://www.pathtomyphoto.com/2.jpg"
]
]
]
])
$bb->get_movie("movie uid");
$bb->list_movies(3);
$bb->create_movie([
"width" => 800,
"height" => 800,
"transition" => "pixelize",
"inputs" => [
[
"asset_url" => "https://images.unsplash.com/photo-1635910160061-4b688344bd20?w=500&q=60",
],
[
"asset_url" => "https://i.imgur.com/fH7a5dO.png",
],
],
]);
$bb->get_screenshot("screenshot uid");
$bb->list_screenshots(3);
$bb->create_screenshot(
"https://www.bannerbear.com/",
[
"width" => 1000,
],
true
);
$bb->get_template("template uid");
$bb->update_template("template uid", [
"name" => "New Template Name",
"tags" => ["portrait", "instagram"],
]);
// Page, Limit, Tag, Name
$bb->list_templates(1, 100, 'tag', 'name');
$bb->get_template_set("template set uid");
$bb->list_template_sets(2);
$bb->get_video_template("video template uid");
$bb->list_video_templates(2);
$bb->generate_signed_url("base uid", [ "modifications" => []]);
// example
$bb->generate_signed_url("A89wavQyY3Bebk3djP", [
"modifications" => [
[
"name" => "country",
"text" => "testing!",
],
[
"name" => "photo",
"image_url" => "https://images.unsplash.com/photo-1638356435991-4c79b00ebef3?w=764&q=80",
],
],
]);
// => https://ondemand.bannerbear.com/signedurl/A89wavQyY3Bebk3djP/image.jpg?modifications=W3sibmFtZSI6ImNvdW50cnkiLCJ0ZXh0IjoidGVzdGluZyEifSx7Im5hbWUiOiJwaG90byIsImltYWdlX3VybCI6Imh0dHBzOi8vaW1hZ2VzLnVuc3BsYXNoLmNvbS9waG90by0xNjM4MzU2NDM1OTkxLTRjNzliMDBlYmVmMz93PTc2NCZxPTgwIn1d&s=40e7c9d4902b86ea83e0c400e57d7cc580534fd527e234d40a0c7ace589a16eb