Skip to content

Instantly share code, notes, and snippets.

@MobCat
Created July 24, 2025 07:02
Show Gist options
  • Save MobCat/d49472f7b5b8b4a557f2b6ec8bdb711e to your computer and use it in GitHub Desktop.
Save MobCat/d49472f7b5b8b4a557f2b6ec8bdb711e to your computer and use it in GitHub Desktop.
<?php
// vidTitle.php
// Title and time header for html5 video player
// Usage: video-poster.php?title=Your%20text%20here&time=12:34
// <video width="800" height="480" controls poster="vidTitle.php?title=Hello%20World&time=1:00&width=1280&height=720&bar=80">
// <source src="video.mp4" type="video/mp4">
// </video>
// If you need more customization then this, then its recommended to just edit the code directly.
// If you do not set a URI param, then a default will be chosen for you from the following.
// If no time is set, then the time element is not drawn at all. good for a stream or other video with unknown length.
// Get the title and time from URL parameters
$title = isset($_GET['title']) ? $_GET['title'] : 'Untitled Video';
$time = isset($_GET['time']) ? "(".$_GET['time'].")" : null;
// Create a 800x480 "image"
$width = isset($_GET['width']) ? (int)$_GET['width'] : 800;
$height = isset($_GET['height']) ? (int)$_GET['height'] : 480;
$barHeight = isset($_GET['bar']) ? (int)$_GET['bar'] : 40;
// Create image
$image = imagecreatetruecolor($width, $height);
// Enable alpha blending and save alpha
imagealphablending($image, false);
imagesavealpha($image, true);
// Create transparent background
$transparent = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $transparent);
// Create black bar at the top
$black = imagecolorallocatealpha($image, 0, 0, 0, 0); // Solid black
imagefilledrectangle($image, 0, 0, $width, $barHeight, $black);
// Create white text color
$white = imagecolorallocate($image, 255, 255, 255);
// Set font size and calculate text positions
$fontSize = 16;
$font = null; // Use default font, or specify TTF font path
$padding = 10; // Padding from edges
// Add the title text (left aligned)
$titleX = $padding;
$titleY = ($barHeight - 15) / 2 + 15; // Adjust for built-in font height
if ($font) {
$textBox = imagettfbbox($fontSize, 0, $font, $title);
$textHeight = $textBox[1] - $textBox[7];
$titleY = ($barHeight - $textHeight) / 2 + $textHeight;
imagettftext($image, $fontSize, 0, $titleX, $titleY, $white, $font, $title);
} else {
imagestring($image, 5, $titleX, $titleY - 15, $title, $white);
}
// Add the time text (right aligned) if provided
if ($time) {
if ($font) {
$timeBox = imagettfbbox($fontSize, 0, $font, $time);
$timeWidth = $timeBox[4] - $timeBox[0];
$timeX = $width - $timeWidth - $padding;
$timeY = $titleY; // Same Y position as title
imagettftext($image, $fontSize, 0, $timeX, $timeY, $white, $font, $time);
} else {
$timeWidth = strlen($time) * 10; // Rough calculation for built-in font
$timeX = $width - $timeWidth - $padding;
$timeY = $titleY - 15; // Same Y position as title
imagestring($image, 5, $timeX, $timeY, $time, $white);
}
}
// Set proper headers for PNG output
header('Content-Type: image/png');
header('Cache-Control: max-age=3600'); // Cache for 1 hour
// Output image over video
imagepng($image);
imagedestroy($image);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment