Skip to content

CkMedia

Job: the media a value names — a YouTube or Vimeo video by its link, a video, image or audio file by its address — read once into an object that has everything a page builds from it: the code or the address, the canonical addresses, the preview image, and an element written by the library.

A video reaches an application in a dozen spellings: the watch address, a short link, an embed address, a pasted embed code, a link somewhere in an editor's markup, or just the code. CkMedia::YouTube and CkMedia::Vimeo read all of them — the address through CkWeb::Url, so the host is the real host and never a look-alike — and hand back a CkYouTube or a CkVimeo: the code, the start time if one was named, the canonical addresses, and Iframe(), a player built one choice at a time — YouTube's or Vimeo's — and written from the code alone. A file the browser shows or plays itself reaches an application as an address on the web or a path on the site — alone, in a pasted element, or somewhere in markup or text: CkMedia::Video, CkMedia::Image and CkMedia::Audio read those and hand back a CkVideo, a CkImage or a CkAudio: the address as the library writes it, the file's type, and Player() or Tag(), the browser's own video, audio or img element, built the same way. The object comes back whatever the value was, so a handler asks IsValid() rather than checking for null; Explain() says why when it is false, and Original() keeps the value as it was given.

Only the code — and the start time, and for an unlisted Vimeo video its hash — is taken from the value; for a file, only the address and the start time. Everything the object answers is written from that, so nothing else of what the user typed reaches a page — which is what makes the player safe to place. The objects live in SafeCoder\CommonKit\Media; CkMedia makes them, so you name them only in a type hint.

Nothing throws unless you ask with throwIfInvalid: true (see How it works). A value that is not text — a number, an array — names no video, with "a Int32 is not a YouTube address" as the reason.

Quick look

use SafeCoder\CommonKit\CkMedia;

$video = CkMedia::YouTube('https://youtu.be/dQw4w9WgXcQ?t=90');

$video->IsValid();      // -> true
$video->Code();         // -> 'dQw4w9WgXcQ'
$video->Start();        // -> 90
$video->Url();          // -> 'https://www.youtube.com/watch?v=dQw4w9WgXcQ&t=90'
$video->EmbedUrl();     // -> 'https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ?start=90'
$video->Thumbnail();    // -> 'https://img.youtube.com/vi/dQw4w9WgXcQ/hqdefault.jpg'
$video->Iframe()->Autoplay()->Url();   // -> 'https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ?autoplay=1&mute=1&start=90'

CkMedia::YouTube('dQw4w9WgXcQ')->Url();                      // -> 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
CkMedia::YouTube('<iframe width="560" height="315" src="https://www.youtube.com/embed/dQw4w9WgXcQ?si=x" title="YouTube video player"></iframe>')->Code();   // -> 'dQw4w9WgXcQ'
CkMedia::YouTube('https://example.com/')->IsValid();         // -> false
CkMedia::YouTube('https://example.com/')->Explain();         // -> 'not a YouTube address'
CkMedia::Explain('https://www.youtube.com/playlist?list=x');   // -> 'no video code in the address'

$clip = CkMedia::Vimeo('https://vimeo.com/76979871/a1b2c3d4e5#t=90s');

$clip->Code();          // -> '76979871'
$clip->Hash();          // -> 'a1b2c3d4e5'   (an unlisted video's hash, which a player needs)
$clip->Start();         // -> 90
$clip->EmbedUrl();      // -> 'https://player.vimeo.com/video/76979871?dnt=1&h=a1b2c3d4e5#t=90s'
$clip->Iframe()->Background()->Url();   // -> 'https://player.vimeo.com/video/76979871?background=1&dnt=1&h=a1b2c3d4e5#t=90s'

$file = CkMedia::Video('https://cdn.example.com/clip.mp4#t=30');

$file->IsValid();       // -> true
$file->Url();           // -> 'https://cdn.example.com/clip.mp4'
$file->Extension();     // -> 'mp4'
$file->MediaType();     // -> 'video/mp4'
$file->Start();         // -> 30
$file->Player()->Html();   // -> '<video src="https://cdn.example.com/clip.mp4#t=30" controls></video>'

$image = CkMedia::Image('https://cdn.example.com/cover.jpg');

$image->Url();              // -> 'https://cdn.example.com/cover.jpg'
$image->MediaType();        // -> 'image/jpeg'
$image->Tag()->Html();      // -> '<img src="https://cdn.example.com/cover.jpg" alt="">'

$audio = CkMedia::Audio('https://cdn.example.com/talk.mp3#t=30');

$audio->Start();            // -> 30
$audio->Player()->Html();   // -> '<audio src="https://cdn.example.com/talk.mp3#t=30" controls></audio>'

Methods

YouTube

The YouTube video a value names, as an object.

static function YouTube(
    mixed $input = null,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): CkYouTube
Parameter Type Default What it does
input mixed null The value: an address, a bare code, a pasted embed code, or markup or text with an address in it. Text, or an object that writes itself as text; anything else names no video.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMediaException that says why, instead of handing back an object whose IsValid() is false, when the value names no video.

Returns a CkYouTube, never null.

When Default With throwIfInvalid: true
The value names a video The object, IsValid() true, every method answering for that video The same
It does not: missing, not text, over 100,000 characters (CkSettings::MaxMediaInputLength), no YouTube address in it, or a YouTube address that names no single video — the front page, a channel, a playlist The object, IsValid() false, Explain() the reason, every other method empty or zero Throws CkMediaException — carries Input, Expected and Reason

What is read. An address in any spelling YouTube uses — youtube.com/watch?v=…, youtu.be/…, /embed/…, /shorts/…, /live/…, /v/…, on www., m., music. or the youtube-nocookie.com host, with or without the scheme — and a start time with it, t=90, t=1m30s or start=90, in the query or the fragment. The bare eleven-character code. A pasted embed code, or any markup: the first YouTube address found wins, looked for in an iframe's src, then a link's href, then the text. Plain text with an address somewhere in it, the first again. The address is read through CkWeb::Url, so notyoutube.com, youtube.com.evil.example and an address carried inside another one are not YouTube, and credentials in the address refuse it as they do there. The code is held to its eleven characters of the URL-safe alphabet; the path and the query key are case-sensitive, as YouTube reads them; videoseries and live_stream, which YouTube writes where a code stands for a playlist and a channel's stream, are not codes.

use SafeCoder\CommonKit\CkMedia;

CkMedia::YouTube('https://www.youtube.com/watch?v=dQw4w9WgXcQ')->Code();              // -> 'dQw4w9WgXcQ'
CkMedia::YouTube('youtu.be/dQw4w9WgXcQ')->Code();                                      // -> 'dQw4w9WgXcQ'   (no scheme: https is assumed)
CkMedia::YouTube('https://www.youtube.com/shorts/dQw4w9WgXcQ')->Code();                // -> 'dQw4w9WgXcQ'
CkMedia::YouTube('https://www.youtube.com/watch?v=dQw4w9WgXcQ&t=1m30s')->Start();      // -> 90
CkMedia::YouTube('<p>See <a href="https://youtu.be/dQw4w9WgXcQ">this</a></p>')->Code();   // -> 'dQw4w9WgXcQ'   (a link in markup)
CkMedia::YouTube('Watch this: https://youtu.be/dQw4w9WgXcQ, great!')->Code();          // -> 'dQw4w9WgXcQ'   (an address in text)
CkMedia::YouTube('https://notyoutube.com/watch?v=dQw4w9WgXcQ')->Explain();             // -> 'not a YouTube address'
CkMedia::YouTube('https://www.youtube.com/embed/videoseries?list=PL1')->Explain();     // -> 'no video code in the address'
CkMedia::YouTube(42)->Explain();                                                       // -> 'a Int32 is not a YouTube address'

Vimeo

The Vimeo video a value names, as an object.

static function Vimeo(
    mixed $input = null,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): CkVimeo
Parameter Type Default What it does
input mixed null The value: an address, a bare code, a pasted embed code, or markup or text with an address in it. Text, or an object that writes itself as text; anything else names no video.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMediaException that says why, instead of handing back an object whose IsValid() is false, when the value names no video.

Returns a CkVimeo, never null — IsValid() true with every method answering for the video, or IsValid() false with Explain() the reason: missing, not text, over 100,000 characters, no Vimeo address in it, or a Vimeo address that names no single video — the front page, a user, a channel, a showcase, a live event. With throwIfInvalid: true the second case throws instead.

What is read. vimeo.com/76979871; the unlisted form vimeo.com/76979871/a1b2c3d4e5, whose hash a player cannot do without; player.vimeo.com/video/76979871, with ?h=… for an unlisted video; and the channel, group, album, showcase, on-demand and manage pages that end in the video's code — on vimeo.com, www.vimeo.com or player.vimeo.com, with or without the scheme. A start time comes along from #t=90s, #t=1m30s or t=90. The bare code — digits, no leading zero — a pasted embed code, markup with an address in it (an iframe first, then a link, then the text), and plain text with an address in it are read as for YouTube. A showcase, an album, a group or a channel is numbered as a video is, and that number is never taken for a video: the code is the one after it.

use SafeCoder\CommonKit\CkMedia;

CkMedia::Vimeo('https://vimeo.com/76979871')->Code();                                  // -> '76979871'
CkMedia::Vimeo('vimeo.com/76979871')->Code();                                          // -> '76979871'
CkMedia::Vimeo('https://vimeo.com/76979871/a1b2c3d4e5')->Hash();                       // -> 'a1b2c3d4e5'
CkMedia::Vimeo('https://player.vimeo.com/video/76979871?h=a1b2c3d4e5')->Hash();        // -> 'a1b2c3d4e5'
CkMedia::Vimeo('https://vimeo.com/channels/staffpicks/76979871')->Code();              // -> '76979871'
CkMedia::Vimeo('https://vimeo.com/76979871#t=1m30s')->Start();                         // -> 90
CkMedia::Vimeo('<iframe src="https://player.vimeo.com/video/76979871?badge=0" width="640" height="360"></iframe>')->Code();   // -> '76979871'
CkMedia::Vimeo('https://vimeo.com/showcase/123')->Explain();                           // -> 'no video code in the address'
CkMedia::Vimeo('https://youtu.be/dQw4w9WgXcQ')->Explain();                             // -> 'not a Vimeo address'

Video

The video file a value names, as an object.

static function Video(
    mixed $input = null,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): CkVideo
Parameter Type Default What it does
input mixed null The value: an address on the web, a path on the site, a pasted video element, or markup or text with an address in it. Text, or an object that writes itself as text; anything else names no video.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMediaException that says why, instead of handing back an object whose IsValid() is false, when the value names no video file.

Returns a CkVideo, never null — IsValid() true with every method answering for the file, or IsValid() false with Explain() the reason: missing, not text, over 100,000 characters, no address in the value ("not a video address"), or an address of another kind of file — .mkv, .avi, .mp3, a page, a provider's video ("not a video file"). With throwIfInvalid: true the second case throws instead.

What is read. A video file is one a browser plays in a video element: .mp4, .mov or .webm; mkv and avi are files to download, not to play. The address may be absolute — https://cdn.example.com/clip.mp4, read through CkWeb::Url, so the host is the real host and never a look-alike, and written as it writes it; //cdn.example.com/clip.mp4 is read as https — or a path on the site itself, images/clip.mp4 or /images/clip.mp4, judged as CkFile::RelativePath judges a path once its escapes are decoded, so that a climb spelled %2E%2E is a climb, and written with each segment encoded, the leading slash kept when there was one and a query of safe characters kept as given. A pasted video element, or any markup, is read like a provider's: the first address wins, looked for in a video's or a source's src, then a link's href, then the text. A #t=30, #t=1m30s or #t=30,90 fragment is the start time, and leaves the address.

use SafeCoder\CommonKit\CkMedia;

CkMedia::Video('https://cdn.example.com/clip.mp4')->Url();        // -> 'https://cdn.example.com/clip.mp4'
CkMedia::Video('/images/Clip One.mov')->Url();                    // -> '/images/Clip%20One.mov'   (a path on the site, each segment encoded)
CkMedia::Video('images/clip.mp4')->Player()->Html();              // -> '<video src="/php/classes/ckmedia/images/clip.mp4" controls></video>'
CkMedia::Video('<video controls><source src="/images/clip.mp4" type="video/mp4"></video>')->Url();   // -> '/images/clip.mp4'
CkMedia::Video('https://cdn.example.com/clip.mkv')->Explain();    // -> 'not a video file'
CkMedia::Video('https://youtu.be/dQw4w9WgXcQ')->Explain();        // -> 'not a video file'   (a provider's address: CkMedia::YouTube reads it)
CkMedia::Video('../clip.mp4')->Explain();                         // -> 'not a video address'   (a climb)
CkMedia::Video(42)->Explain();                                    // -> 'a Int32 is not a video address'

Image

The image file a value names, as an object.

static function Image(
    mixed $input = null,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): CkImage
Parameter Type Default What it does
input mixed null The value: an address on the web, a path on the site, a pasted img element, or markup or text with an address in it. Text, or an object that writes itself as text; anything else names no image.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMediaException that says why, instead of handing back an object whose IsValid() is false, when the value names no image file.

Returns a CkImage, never null — IsValid() true with every method answering for the file, or IsValid() false with Explain() the reason: missing, not text, over 100,000 characters, no address in the value ("not an image address"), or an address of another kind of file — .svg, .bmp, a page ("not an image file"). With throwIfInvalid: true the second case throws instead.

What is read. An image file is one a browser shows in an img element: .jpg, .jpeg, .png, .gif, .webp or .avif. SVG is left out, since it may carry script, and a data address is content, not an address. The address is read as Video reads one — absolute through CkWeb::Url, or a path on the site through CkFile::RelativePath once its escapes are decoded — from a pasted img element or any markup (an img's src, then a link's href, then the text), or from text with an address in it; a fragment is dropped.

use SafeCoder\CommonKit\CkMedia;

CkMedia::Image('https://cdn.example.com/cover.jpg')->Extension();   // -> 'jpg'
CkMedia::Image('/images/Cover One.png')->Url();                    // -> '/images/Cover%20One.png'
CkMedia::Image('<img src="/images/cover.jpg" width="640">')->Url();   // -> '/images/cover.jpg'
CkMedia::Image('https://cdn.example.com/cover.svg')->Explain();    // -> 'not an image file'
CkMedia::Image('data:image/png;base64,iVBORw0KGgo=')->Explain();   // -> 'not an image address'
CkMedia::Explain('cover.svg', what: 'Image');                      // -> 'not an image file'

Audio

The audio file a value names, as an object.

static function Audio(
    mixed $input = null,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): CkAudio
Parameter Type Default What it does
input mixed null The value: an address on the web, a path on the site, a pasted audio element, or markup or text with an address in it. Text, or an object that writes itself as text; anything else names no audio.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMediaException that says why, instead of handing back an object whose IsValid() is false, when the value names no audio file.

Returns a CkAudio, never null — IsValid() true with every method answering for the file, or IsValid() false with Explain() the reason: missing, not text, over 100,000 characters, no address in the value ("not an audio address"), or an address of another kind of file — .aac, .mp4, a page ("not an audio file"). With throwIfInvalid: true the second case throws instead.

What is read. An audio file is one a browser plays in an audio element: .mp3, .wav, .ogg, .m4a or .flac. The address is read as Video reads one, from a pasted audio element or any markup (an audio's or a source's src, then a link, then the text), or from text with an address in it; a #t=30, #t=1m30s or #t=30,90 fragment is the start time.

use SafeCoder\CommonKit\CkMedia;

CkMedia::Audio('https://cdn.example.com/talk.mp3')->MediaType();   // -> 'audio/mpeg'
CkMedia::Audio('audio/Talk One.m4a')->Url();                        // -> 'audio/Talk%20One.m4a'
CkMedia::Audio('https://cdn.example.com/talk.aac')->Explain();      // -> 'not an audio file'
CkMedia::Audio(42)->Explain();                                      // -> 'a Int32 is not an audio address'
CkMedia::Explain('audio/talk.mp3', what: 'Audio');                  // -> ''

Explain

Why not — in words, without an exception.

static function Explain(
    mixed $input = null,
    ?string $what = 'YouTube'
): string
Parameter Type Default What it does
input mixed null The value the reader would read.
what ?string 'YouTube' The reader to judge by: 'YouTube', 'Vimeo', 'Video', 'Image' or 'Audio', in any case. Any other name answers "not a CkMedia reader: …".

Returns

When You get
The value names what that reader reads ''
It does not The reason, the same words the object's Explain() and the exception carry — an answer, not a failure; it never throws
use SafeCoder\CommonKit\CkMedia;

CkMedia::Explain('https://youtu.be/dQw4w9WgXcQ');      // -> ''
CkMedia::Explain(null);                                 // -> 'no value'
CkMedia::Explain('https://example.com/');               // -> 'not a YouTube address'
CkMedia::Explain('https://www.youtube.com/');           // -> 'no video code in the address'
CkMedia::Explain('https://vimeo.com/76979871', what: 'Vimeo');   // -> ''
CkMedia::Explain('https://example.com/', what: 'Vimeo');         // -> 'not a Vimeo address'
CkMedia::Explain('images/clip.mp4', what: 'Video');                     // -> ''
CkMedia::Explain('https://cdn.example.com/page.html', what: 'Video');   // -> 'not a video file'
CkMedia::Explain('cover.svg', what: 'Image');                           // -> 'not an image file'
CkMedia::Explain('audio/talk.mp3', what: 'Audio');                      // -> ''

The CkYouTube object

What YouTube hands back — made by it alone, never by an application — with these methods:

Method Answers When the value named no video
IsValid() true when a code was read false
Explain() '' The reason — 'not a YouTube address', 'no video code in the address', 'no value'
Original() The value exactly as it was given, whatever it was The same
Code() The eleven-character video code, 'dQw4w9WgXcQ' ''
Start() The start time named, in seconds — t=90, t=1m30s and start=90 are all 90; a time past a hundred hours is 0 0
Url() The canonical watch address, 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', with &t=90 when a start time was named ''
EmbedUrl() The player's address on the host that sets no cookie until the video plays, 'https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ', with ?start=90 when a start time was named ''
Thumbnail() The preview image, 480 by 360: 'https://img.youtube.com/vi/dQw4w9WgXcQ/hqdefault.jpg' ''
Iframe() The player, to build and place: a CkYouTubeIframe, new on every call — see below An object whose Html() and Url() are ''

The CkYouTubeIframe object

What Iframe() hands back: the player, to build one choice at a time and place. Each setter names one thing about the player and hands the same object back, so the choices read as one chain; Html() then writes the iframe, Url() the address it loads, and the object writes itself as the markup when it is used as text, so echo $video->Iframe() places the player. Only what differs from YouTube's own default is written: a player with nothing set is YouTube's own, with no query on its address.

use SafeCoder\CommonKit\CkMedia;

$video = CkMedia::YouTube('https://youtu.be/dQw4w9WgXcQ');

$video->Iframe()->Html();   // -> '<iframe src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ" width="560" height="315" title="YouTube video player" loading="lazy" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>'
$video->Iframe()
    ->Width(640)
    ->Height(360)
    ->Title('Our "talk"')
    ->Autoplay()
    ->Start(30)
    ->Html();               // -> '<iframe src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ?autoplay=1&amp;mute=1&amp;start=30" width="640" height="360" title="Our &quot;talk&quot;" loading="lazy" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>'
$video->Iframe()->Loop()->Related(false)->Url();   // -> 'https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ?loop=1&playlist=dQw4w9WgXcQ&rel=0'
$video->Iframe()->Fullscreen(false)->Html();       // -> '<iframe src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ?fs=0" width="560" height="315" title="YouTube video player" loading="lazy" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin"></iframe>'
CkMedia::YouTube('https://example.com/')->Iframe()->Html();   // -> ''
Method Sets Written as
Width(int), Height(int) The frame's size in pixels; below 1 is the default, 560 by 315 (CkSettings::DefaultMediaWidth, DefaultMediaHeight) width, height on the frame
Title(?string) The accessible title, cleaned as CkText::String cleans text and encoded; empty is "YouTube video player" title on the frame
Autoplay(bool = true) Plays on load. A browser plays nothing with sound until the viewer has touched the page, so the player is muted with it — unless Muted(false) says otherwise, in either order autoplay=1, and mute=1
Muted(bool = true) Starts silent mute=1
Loop(bool = true) Starts again at the end; a single video loops only with itself as the playlist loop=1&playlist=dQw4w9WgXcQ
Start(int) Where playback starts, in seconds. The player begins with the start time the value named; 0 removes it start=90
End(int) Where playback stops, in seconds from the start of the video; at or before the start, it is dropped end=120
Controls(bool = true) Shows the controls; false for a bare player controls=0
Fullscreen(bool = true) Lets the viewer fill the screen; false removes the button, and allowfullscreen from the frame fs=0
Keyboard(bool = true) Answers the keyboard — space, the arrows, m; false ignores it disablekb=1
Inline(bool = true) Plays inside the page on an iPhone, where it would otherwise open full screen playsinline=1
Captions(bool = true) Shows captions from the start, even for a viewer who turned them off cc_load_policy=1
Language(?string) The interface's and the captions' language, one tag — de, fr-ca, pt-br — lower-cased; anything that is not a tag is none hl=de&cc_lang_pref=de
Related(bool = true) Which related videos the player offers at the end: YouTube's choice, or with false the same channel's only — YouTube no longer lets a player offer none rel=0
Url() The address the frame loads, on the host that sets no cookie until the video plays, the parameters in name order; '' for no video —
Html() The iframe: src on Url(), the size and title, lazy loading, the permissions YouTube's own embed code asks for, no referrer beyond the origin, allowfullscreen unless said otherwise; '' for no video —

A time that is negative or past a hundred hours (CkSettings::MaxMediaSeconds) counts as none. Iframe() makes a new object from the video on every call, so configuring one player never changes another, or the video. Not on the object, on purpose: the progress bar's color, the JavaScript API's enablejsapi, origin and widget_referrer, a playlist's list and listType, and modestbranding, which YouTube retired — an application that drives the player by script adds its own parameters to Url().

The CkVimeo object

What Vimeo hands back — made by it alone — with these methods:

Method Answers When the value named no video
IsValid() true when a code was read false
Explain() '' The reason — 'not a Vimeo address', 'no video code in the address', 'no value'
Original() The value exactly as it was given The same
Code() The video code, digits: '76979871' ''
Hash() The hash of an unlisted video, lower case: 'a1b2c3d4e5'; '' for a video that has none ''
Start() The start time named, in seconds — #t=90s, #t=1m30s and t=90 are all 90 0
Url() The canonical address, 'https://vimeo.com/76979871', the hash after the code for an unlisted video, #t=90s when a start time was named ''
EmbedUrl() The player's address, asking Vimeo not to track the viewer: 'https://player.vimeo.com/video/76979871?dnt=1', with &h=… for an unlisted video and #t=90s for a start time ''
Iframe() The player, to build and place: a CkVimeoIframe, new on every call — see below An object whose Html() and Url() are ''

There is no Thumbnail(): Vimeo gives a video's preview image only through its oEmbed service, which is a call the library does not make.

use SafeCoder\CommonKit\CkMedia;

$clip = CkMedia::Vimeo('https://vimeo.com/76979871');

$clip->Url();       // -> 'https://vimeo.com/76979871'
$clip->EmbedUrl();  // -> 'https://player.vimeo.com/video/76979871?dnt=1'

The CkVimeoIframe object

What Iframe() hands back for a Vimeo video: the player, built one choice at a time as the YouTube one is, with Vimeo's own parameters. Each setter hands the same object back; Html() writes the iframe, Url() the address it loads, and the object writes itself as the markup when it is used as text. Only what differs from Vimeo's own default is written, with two things every address carries: dnt=1, which asks Vimeo not to track the viewer, and the hash of an unlisted video, which a player cannot load it without.

use SafeCoder\CommonKit\CkMedia;

$clip = CkMedia::Vimeo('https://vimeo.com/76979871');

$clip->Iframe()->Html();   // -> '<iframe src="https://player.vimeo.com/video/76979871?dnt=1" width="560" height="315" title="Vimeo video player" loading="lazy" allow="autoplay; fullscreen; picture-in-picture; clipboard-write; encrypted-media" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>'
$clip->Iframe()
    ->Width(640)
    ->Height(360)
    ->Title('Our "talk"')
    ->Autoplay()
    ->Start(30)
    ->Html();               // -> '<iframe src="https://player.vimeo.com/video/76979871?autoplay=1&amp;dnt=1&amp;muted=1#t=30s" width="640" height="360" title="Our &quot;talk&quot;" loading="lazy" allow="autoplay; fullscreen; picture-in-picture; clipboard-write; encrypted-media" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>'
$clip->Iframe()->Background()->Url();   // -> 'https://player.vimeo.com/video/76979871?background=1&dnt=1'
$clip->Iframe()->ShowTitle(false)->ShowByline(false)->ShowPortrait(false)->Color('#EF2929')->Url();   // -> 'https://player.vimeo.com/video/76979871?byline=0&color=ef2929&dnt=1&portrait=0&title=0'
CkMedia::Vimeo('https://vimeo.com/76979871/a1b2c3d4e5#t=90s')->Iframe()->Url();   // -> 'https://player.vimeo.com/video/76979871?dnt=1&h=a1b2c3d4e5#t=90s'
CkMedia::Vimeo('https://example.com/')->Iframe()->Html();   // -> ''
Method Sets Written as
Width(int), Height(int) The frame's size in pixels; below 1 is the default, 560 by 315 (CkSettings::DefaultMediaWidth, DefaultMediaHeight) width, height on the frame
Title(?string) The accessible title, cleaned as CkText::String cleans text and encoded; empty is "Vimeo video player" title on the frame
Autoplay(bool = true) Plays on load, muted with it as on YouTube — unless Muted(false) says otherwise, in either order autoplay=1, and muted=1
Muted(bool = true) Starts silent muted=1
Loop(bool = true) Starts again at the end loop=1
Background(bool = true) Runs as a background — plays on load, loops, silent, no controls, whatever else is set background=1
Start(int) Where playback starts, in seconds. The player begins with the start time the value named; 0 removes it #t=90s, after the query
Controls(bool = true) Shows the controls; false for a bare player controls=0
Keyboard(bool = true) Answers the keyboard — space, the arrows, f; false ignores it keyboard=0
Inline(bool = true) Plays inside the page on a phone, which Vimeo does unless told not to; false lets the device open it full screen playsinline=0
PictureInPicture(bool = true) Offers picture-in-picture, the video in a small window of its own pip=1
Speed(bool = true) Offers the playback speeds in the settings speed=1
ShowTitle(bool = true), ShowByline(bool = true), ShowPortrait(bool = true) What the player shows over the video before it plays — its title, who uploaded it, their portrait; false hides each title=0, byline=0, portrait=0
Color(?string) The color of the controls, a hex color as a stylesheet writes one — ef2929, #EF2929, f00; anything else is none, and so is Vimeo's own blue, 00adef color=ef2929
Captions(?string) The captions shown from the start, by their language — de, en-US; anything that is not a tag is none texttrack=de
Url() The address the frame loads on player.vimeo.com: dnt=1 on every one, h= for an unlisted video, the parameters in name order, the start time after them; '' for no video —
Html() The iframe: src on Url(), the size and title, lazy loading, the permissions Vimeo's own embed code asks for, no referrer beyond the origin, allowfullscreen; '' for no video —

A start that is negative or past a hundred hours (CkSettings::MaxMediaSeconds) counts as none. Iframe() makes a new object from the video on every call. Not on the object, on purpose: autopause, quality, transparent and a stop time — an application that drives the player by script adds its own parameters to Url().

The CkVideo object

What Video hands back — made by it alone — with these methods:

Method Answers When the value named no video file
IsValid() true when an address was read false
Explain() '' The reason — 'not a video address', 'not a video file', 'no value'
Original() The value exactly as it was given The same
Url() The address as the library writes it — 'https://cdn.example.com/clip.mp4', '/images/Clip%20One.mov' — without the start time, which the player writes ''
Extension() The file's extension, lower case: 'mp4', 'mov', 'webm' ''
MediaType() The media type the extension implies, as CkFile::MediaType answers it: 'video/mp4', 'video/quicktime', 'video/webm' ''
Start() The start time named, in seconds — #t=30, #t=1m30s and the start of #t=30,90 0
Player() The player, to build and place: a CkVideoPlayer, new on every call — see below An object whose Html() and Url() are ''

The CkVideoPlayer object

What Player() hands back: the browser's own video element, built one choice at a time as the two iframes are. Each setter hands the same object back; Html() writes the element, Url() the address it loads, and the object writes itself as the markup when it is used as text. Only what is set is written, with one default of the library's own: the controls are shown unless Controls(false) says otherwise, since a video the viewer cannot start is of no use. The size is the file's own unless set.

use SafeCoder\CommonKit\CkMedia;
use SafeCoder\CommonKit\Enums\Preload;

$file = CkMedia::Video('https://cdn.example.com/clip.mp4#t=30');

$file->Player()->Html();   // -> '<video src="https://cdn.example.com/clip.mp4#t=30" controls></video>'
$file->Player()
    ->Poster('https://cdn.example.com/cover.jpg')
    ->Width(640)
    ->Height(360)
    ->Title('Our talk')
    ->Preload(Preload::Metadata)
    ->Html();              // -> '<video src="https://cdn.example.com/clip.mp4#t=30" width="640" height="360" poster="https://cdn.example.com/cover.jpg" title="Our talk" preload="metadata" controls></video>'
$file->Player()->Autoplay()->Loop()->Controls(false)->Inline()->Start(0)->Html();   // -> '<video src="https://cdn.example.com/clip.mp4" autoplay loop muted playsinline></video>'
$file->Player()->End(90)->Url();   // -> 'https://cdn.example.com/clip.mp4#t=30,90'
CkMedia::Video('https://example.com/')->Player()->Html();   // -> ''
Method Sets Written as
Width(int), Height(int) The player's size in pixels; below 1 is none, and the file's own size stands width, height
Title(?string) The title, what a screen reader announces and a pointer shows, cleaned as CkText::String cleans text and encoded; empty is none title
Poster(?string) The image shown until the video plays: an address read as the file's own is — on the web or a path on the site — of a jpg, jpeg, png, gif, webp or avif file; anything else is none poster
Autoplay(bool = true) Plays on load, muted with it as the iframes are — unless Muted(false) says otherwise, in either order autoplay, and muted
Muted(bool = true) Starts silent muted
Loop(bool = true) Starts again at the end loop
Controls(bool = true) Shows the browser's controls; false for a bare player, as a background video wants controls, unless false
Inline(bool = true) Plays inside the page on an iPhone, where it would otherwise open full screen playsinline
Preload(?Preload) How much the browser fetches before play: Preload::None, Metadata or Auto; null leaves the browser to decide preload="none", "metadata", "auto"
Start(int) Where playback starts, in seconds. The player begins with the start time the value named; 0 removes it #t=30 on the address
End(int) Where playback stops, in seconds from the start of the file; at or before the start, it is dropped #t=30,90 on the address
Url() The address the player loads: the file's, with the media fragment for the times when set; '' for no video —
Html() The video element: src on Url(), the size, the poster, the title, the preload hint, then the booleans as they apply; '' for no video —

A time that is negative or past a hundred hours (CkSettings::MaxMediaSeconds) counts as none. Player() makes a new object from the file on every call. The sanitizer's Media policy keeps a video element with its src, poster, controls and size, so a player an author pasted into an editor survives CkSanitizer::Html where a provider's iframe does not.

The CkImage object

What Image hands back — made by it alone — with these methods:

Method Answers When the value named no image file
IsValid() true when an address was read false
Explain() '' The reason — 'not an image address', 'not an image file', 'no value'
Original() The value exactly as it was given The same
Url() The address as the library writes it — 'https://cdn.example.com/cover.jpg', '/images/Cover%20One.png' ''
Extension() The file's extension, lower case: 'jpg', 'png', 'webp' ''
MediaType() As CkFile::MediaType answers it: 'image/jpeg', 'image/png' ''
Tag() The element, to build and place: a CkImageTag, new on every call An object whose Html() and Url() are ''

The CkImageTag object

What Tag() hands back: the img element, built one choice at a time as the players are. Only what is set is written — but for alt, which an img always carries: empty, the default, it marks the image decorative, one a screen reader passes over. The size is the file's own unless set.

use SafeCoder\CommonKit\CkMedia;

$image = CkMedia::Image('https://cdn.example.com/cover.jpg');

$image->Tag()->Html();   // -> '<img src="https://cdn.example.com/cover.jpg" alt="">'
$image->Tag()->Alt('Our team')->Width(640)->Height(360)->Lazy()->Html();   // -> '<img src="https://cdn.example.com/cover.jpg" alt="Our team" width="640" height="360" loading="lazy">'
CkMedia::Image('https://example.com/')->Tag()->Html();   // -> ''
Method Sets Written as
Alt(?string) The words a screen reader says in the image's place, cleaned as CkText::String cleans text and encoded; empty marks the image decorative alt, always
Title(?string) The title a pointer shows; empty is none title
Width(int), Height(int) The size in pixels; below 1 is none, and the file's own size stands width, height
Lazy(bool = true) Fetches the image only as it comes into view, for one below the fold loading="lazy"
Url() The file's address; '' for no image —
Html() The img: src, alt, then the size, the title and the loading hint as set; '' for no image —

The CkAudio object

What Audio hands back — made by it alone — with these methods:

Method Answers When the value named no audio file
IsValid() true when an address was read false
Explain() '' The reason — 'not an audio address', 'not an audio file', 'no value'
Original() The value exactly as it was given The same
Url() The address as the library writes it, without the start time — 'https://cdn.example.com/talk.mp3', 'audio/Talk%20One.m4a' ''
Extension() The file's extension, lower case: 'mp3', 'wav', 'ogg', 'm4a', 'flac' ''
MediaType() As CkFile::MediaType answers it: 'audio/mpeg', 'audio/wav', 'audio/ogg', 'audio/mp4', 'audio/flac' ''
Start() The start time named, in seconds 0
Player() The player, to build and place: a CkAudioPlayer, new on every call An object whose Html() and Url() are ''

The CkAudioPlayer object

What Player() hands back for an audio file: the browser's own audio element, built as the video player is, with the controls shown unless Controls(false) says otherwise. An audio element has no size and no poster; a stylesheet sizes it. Unlike the video players, Autoplay() mutes nothing with it: a silent sound is no use, so the browser holds the sound until the viewer's first touch.

use SafeCoder\CommonKit\CkMedia;
use SafeCoder\CommonKit\Enums\Preload;

$audio = CkMedia::Audio('https://cdn.example.com/talk.mp3#t=30');

$audio->Player()->Html();   // -> '<audio src="https://cdn.example.com/talk.mp3#t=30" controls></audio>'
$audio->Player()->Title('Our talk')->Preload(Preload::Metadata)->Html();   // -> '<audio src="https://cdn.example.com/talk.mp3#t=30" title="Our talk" preload="metadata" controls></audio>'
$audio->Player()->Autoplay()->Loop()->Html();   // -> '<audio src="https://cdn.example.com/talk.mp3#t=30" autoplay controls loop></audio>'
$audio->Player()->End(90)->Url();   // -> 'https://cdn.example.com/talk.mp3#t=30,90'
Method Sets Written as
Title(?string) The title, cleaned as text and encoded; empty is none title
Autoplay(bool = true) Plays on load, as asked; nothing is muted with it autoplay
Muted(bool = true) Starts silent muted
Loop(bool = true) Starts again at the end loop
Controls(bool = true) Shows the browser's controls; false for a player driven by script controls, unless false
Preload(?Preload) How much the browser fetches before play: Preload::None, Metadata or Auto preload="none", "metadata", "auto"
Start(int), End(int) Where playback starts and stops, in seconds; an end at or before the start is dropped #t=30, #t=30,90 on the address
Url() The file's address with the media fragment when set; '' for no audio —
Html() The audio element: src, then the title, the preload hint and the booleans as they apply; '' for no audio —

The player on a page

Store the code — and, for an unlisted Vimeo video, its hash — or the file's address, the one thing the user chose, and build the rest when the page is made. The application decides where the element goes and what stands around it; the library writes it. CkSanitizer::Html removes every iframe, a provider's player included, so a YouTube or Vimeo video is never placed through it: it is placed by the template, beside the sanitized text. A file's element — the video, audio or img the library writes — the sanitizer's Media policy keeps with its source, so one an author pasted into an editor survives.

// no-check
use SafeCoder\CommonKit\CkMedia;

// on the way in: the code, or a refusal with its reason
$video = CkMedia::YouTube(CkRequest::String('video'));

if ($video->IsValid() === false) {
    $errors['video'] = $video->Explain();
}

$row->VideoCode = $video->Code();

// on the way out: the player and its preview, written from the stored code
$video = CkMedia::YouTube($row->VideoCode);

echo $video->Iframe()->Title($row->Title)->Related(false);
echo '<img src="/php/classes/ckmedia/' . $video->Thumbnail() . '" alt="">';

// a file the same way: the address in, the element out
$cover = CkMedia::Image(CkRequest::String('cover'));

if ($cover->IsValid() === false) {
    $errors['cover'] = $cover->Explain();
}

$row->CoverUrl = $cover->Url();

echo CkMedia::Image($row->CoverUrl)->Tag()->Alt($row->Title)->Lazy();

Good to know

  • The object is the answer, valid or not. No reader hands back null, so a handler reads IsValid() and Explain() instead of guarding; every other method is empty or zero until the value named a video or a file.
  • Only the code, the start time and a Vimeo hash come from the value — or, for a file, the address and the start time. The addresses, the thumbnail and the element are written from them, which is why a pasted embed code or element — or markup with one in it — can be read without its attributes ever reaching your page.
  • The real host is what counts. The address is read by CkWeb::Url first, so a look-alike host, a YouTube address carried inside another address, or one with credentials in it is not YouTube; a file's address on the web the same. Nothing is found by pattern alone.
  • A path on the site is judged as a path. images/clip.mp4 and /images/clip.mp4 are read as CkFile::RelativePath reads a path, once its escapes are decoded — so ../clip.mp4 and %2E%2E/clip.mp4 are climbs and name no file.
  • The player asks for as little as it can. A YouTube player loads from youtube-nocookie.com whatever host the value named, so it sets no cookie until the viewer plays the video; a Vimeo player carries dnt=1, which asks Vimeo not to track the viewer; a file's element loads the file and nothing else.
  • Two players, two sets of choices. The YouTube builder has End, Fullscreen, Related and Language; the Vimeo builder has Background, Color, PictureInPicture, Speed and the three Show… toggles, and its Captions takes the language where YouTube's takes a flag. What both have — the size, the title, Autoplay, Muted, Loop, Start, Controls, Keyboard, Inline — is spelled the same.
  • A file's element survives the sanitizer; a provider's player does not. The video, img and audio elements are markup the Media policy keeps, with the source, the poster, the controls and the size; an iframe is an embedding, which the sanitizer removes whatever it holds.
  • The first address wins. In markup, an iframe — or a video, a source, an img, an audio — is looked at before a link, a link before the text; in text, the first address. A value with two videos names the first.
  • A playlist or a channel names no video. videoseries, live_stream, the front page, /channel/… and /playlist?list=… answer "no video code in the address" on YouTube; a user, a channel, a showcase, an album or a live event the same on Vimeo. The page is the provider's, the video is not one.
  • A file is known by its extension. .mp4, .mov and .webm are videos, .mkv and .avi downloads; .jpg, .jpeg, .png, .gif, .webp and .avif are images, .svg is not, since it may carry script; .mp3, .wav, .ogg, .m4a and .flac are audio. Anything else is "not a video file", "not an image file" or "not an audio file" — a provider's address included, which the provider's reader takes.
  • The path is case-sensitive. /WATCH?V=… names no video, as YouTube itself would not serve it; the code keeps its case, since dQw4w9WgXcQ and dqw4w9wgxcq are different videos.

If you want errors instead

Pass throwIfInvalid: true and a value that names no video or file raises a CkMediaException (from SafeCoder\CommonKit\Exceptions) instead of an object whose IsValid() is false. The exception carries the Input, what it was Expected to be — "a YouTube address", "a Vimeo address", "a video address", "an image address", "an audio address" — and a short Reason — "not a YouTube address", "no video code in the address", "not a video file", "not an image address" — flattened into a one-line message.

use SafeCoder\CommonKit\CkMedia;

CkMedia::YouTube(
    input: 'https://example.com/',
    throwIfInvalid: true
);   // throws CkMediaException — the message says why