JSON2Video API allows you to add images to your videos to create overlays, watermarks, backgrounds or slideshows.
In this section we will provide some examples of using images.
Simple image
Let's create a very simple example with nothing else than a background image. For this, we will
create a movie, with just one scene, and with an Image Element inside.
The image is set to a duration of 10 seconds, so the scene and the movie will inherit the
same duration.
require 'vendor/autoload.php';
use JSON2Video\Movie;
use JSON2Video\Scene;
// Create and initialize the movie object
$movie = new Movie;
$movie->setAPIKey(YOUR_API_KEY);
$movie->resolution = 'full-hd';
// Create the scenes of the movie
// Create SCENE 1
$scene1 = new Scene;
$scene1->addElement([
'type' => 'image',
'src' => 'https://assets.json2video.com/assets/images/flower-bee.jpg',
'duration' => 10
]);
$movie->addScene($scene1);
// Finally, render the movie
$movie->render();
// Wait for the movie to be rendered
$movie->waitToFinish();
let movie = new Movie;
movie.setAPIKey(YOUR_API_KEY);
movie.set("resolution", "full-hd");
// Create the scenes of the movie
// Create SCENE 1
let scene1 = new Scene;
scene1.addElement({
"type": "image",
"src": "https://assets.json2video.com/assets/images/flower-bee.jpg",
"duration": 10
});
movie.addScene(scene1);
// Finally, render the movie
movie.render();
// Wait for the movie to be rendered
movie.waitToFinish();
The resulting video is:
Simple watermark over video
In this example, we will add a watermark image on the top-right corner over a background video.
The watermark will appear after 3 seconds and will stay visible for 5 seconds.
require 'vendor/autoload.php';
use JSON2Video\Movie;
use JSON2Video\Scene;
// Create and initialize the movie object
$movie = new Movie;
$movie->setAPIKey(YOUR_API_KEY);
$movie->resolution = 'full-hd';
// Create the scenes of the movie
// Create SCENE 1
$scene1 = new Scene;
$scene1->addElement([
'type' => 'video',
'src' => 'https://assets.json2video.com/assets/videos/beach-01.mp4'
]);
$scene1->addElement([
'type' => 'image',
'src' => 'https://assets.json2video.com/assets/images/sunglasses-emoji-small.png',
'start' => 3,
'duration' => 5,
'x' => 1800,
'y' => 20
]);
$movie->addScene($scene1);
// Finally, render the movie
$movie->render();
// Wait for the movie to be rendered
$movie->waitToFinish();
let movie = new Movie;
movie.setAPIKey(YOUR_API_KEY);
movie.set("resolution", "full-hd");
// Create the scenes of the movie
// Create SCENE 1
let scene1 = new Scene;
scene1.addElement({
"type": "video",
"src": "https://assets.json2video.com/assets/videos/beach-01.mp4"
});
scene1.addElement({
"type": "image",
"src": "https://assets.json2video.com/assets/images/sunglasses-emoji-small.png",
"start": 3,
"duration": 5,
"x": 1800,
"y": 20
});
movie.addScene(scene1);
// Finally, render the movie
movie.render();
// Wait for the movie to be rendered
movie.waitToFinish();
Note that the image is a PNG with transparent background.
Scaled and rotated image
Images can be scaled up and down and can be rotated. The following example
creates a movie with a background image and a rotating sunglasses emoji
scaled up to 250x250 pixels.
require 'vendor/autoload.php';
use JSON2Video\Movie;
use JSON2Video\Scene;
// Create and initialize the movie object
$movie = new Movie;
$movie->setAPIKey(YOUR_API_KEY);
$movie->resolution = 'full-hd';
// Create the scenes of the movie
// Create SCENE 1
$scene1 = new Scene;
$scene1->addElement([
'type' => 'image',
'src' => 'https://assets.json2video.com/assets/images/flower-bee.jpg',
'duration' => 15
]);
$scene1->addElement([
'type' => 'image',
'src' => 'https://assets.json2video.com/assets/images/sunglasses-emoji-small.png',
'x' => 835,
'y' => 575,
'scale' => [
'width' => 250,
'height' => 250
],
'rotate' => [
'angle' => 360,
'speed' => 1
]
]);
$movie->addScene($scene1);
// Finally, render the movie
$movie->render();
// Wait for the movie to be rendered
$movie->waitToFinish();
let movie = new Movie;
movie.setAPIKey(YOUR_API_KEY);
movie.set("resolution", "full-hd");
// Create the scenes of the movie
// Create SCENE 1
let scene1 = new Scene;
scene1.addElement({
"type": "image",
"src": "https://assets.json2video.com/assets/images/flower-bee.jpg",
"duration": 15
});
scene1.addElement({
"type": "image",
"src": "https://assets.json2video.com/assets/images/sunglasses-emoji-small.png",
"x": 835,
"y": 575,
"scale": {
"width": 250,
"height": 250
},
"rotate": {
"angle": 360,
"speed": 1
}
});
movie.addScene(scene1);
// Finally, render the movie
movie.render();
// Wait for the movie to be rendered
movie.waitToFinish();
The rotate property takes an angle and speed parameters to
animate the rotation. In the example above, the settings mean "rotate 360 degrees in 1 second".
The rotation continues during all the scene.
A speed value of zero means there is no animation and the image is statically rotated.
The output of the JSON above is:
Image slideshow
Creating an image slideshow is quite similar to create a video slideshow (Video Elements - Example 3).
We will use scenes (one image per scene) and transitions between scenes.