Skip to content

Instantly share code, notes, and snippets.

@bacoords
Last active January 24, 2024 09:40
Show Gist options
  • Star 32 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save bacoords/77ee4d13dfa76db03981cb4eb0df0c6f to your computer and use it in GitHub Desktop.
Save bacoords/77ee4d13dfa76db03981cb4eb0df0c6f to your computer and use it in GitHub Desktop.
Grab a thumbnail of a private (but embeddable) Vimeo video
<?php
/**
* Grab the url of a publicly embeddable video hosted on vimeo
* @param str $video_url The "embed" url of a video
* @return str The url of the thumbnail, or false if there's an error
*/
function grab_vimeo_thumbnail($vimeo_url){
if( !$vimeo_url ) return false;
$data = json_decode( file_get_contents( 'http://vimeo.com/api/oembed.json?url=' . $vimeo_url ) );
if( !$data ) return false;
return $data->thumbnail_url;
}
@taiminenjani
Copy link

Thanks. This was very useful.

@csingewald
Copy link

Cool, thanks.

@shaunkardinal
Copy link

shame this only delivers the low res thumbnail. RIP v2 API getJSON solution

@benonetto
Copy link

Really cool, thanks!

@venuscreats
Copy link

Thanks .. Found a solutions few hours and this is worked!

@norival
Copy link

norival commented Sep 27, 2021

Works perfectly, thanks :)

@Farhan-Haseeb
Copy link

Works perfectly, thanks dude! :) 👍

@fo-rk
Copy link

fo-rk commented Nov 30, 2022

shame this only delivers the low res thumbnail. RIP v2 API getJSON solution

You can replace the resolution at end of URL string for 1920x1080, if that helps @shaunkardinal.

@shaunkardinal
Copy link

You can replace the resolution at end of URL string for 1920x1080, if that helps @shaunkardinal.

@monowales amazing! can you provide an example? something like thumbnail_large or specifically include dimensions?

@fo-rk
Copy link

fo-rk commented Nov 30, 2022

In Javascript I translated @bacoords code above, but returned:

...
return data.thumbnail_url.split('_')[0] + '_1920x1080';

Have a play, I'm fairly sure it will return any dimension you need, (it will crop though, if it's not the original ratio). I found the HD thumbs to be big enough, but still very small file size, 30-50kb. Only caveat is there might be instances where the underscore features elsewhere in the URL, but I don't think so.

@fo-rk
Copy link

fo-rk commented Nov 30, 2022

For completeness, would it be this in PHP?

...
return explode($data->thumbnail_url, '_')[0]."_1920x1080";

@shaunkardinal
Copy link

unfortunately the PHP object only contains the one thumbnail_url, which is generally 640x360.

example https://vimeo.com/api/oembed.json?url=https://vimeo.com/311468941 returns

{"type":"video","version":"1.0","provider_name":"Vimeo","provider_url":"https:\/\/vimeo.com\/","title":"olson kundig | space needle century project | seattle","author_name":"nic lehoux","author_url":"https:\/\/vimeo.com\/niclehoux","is_plus":"1","account_type":"plus","html":"<iframe src=\"https:\/\/player.vimeo.com\/video\/311468941?h=674979c981&amp;app_id=122963\" width=\"640\" height=\"360\" frameborder=\"0\" allow=\"autoplay; fullscreen; picture-in-picture\" allowfullscreen title=\"olson kundig | space needle century project | seattle\"><\/iframe>","width":640,"height":360,"duration":158,"description":"","thumbnail_url":"https:\/\/i.vimeocdn.com\/video\/752300323-c9b7699f9dc14ec1484a39edca5506c5bf745e081cdab9d08fd9fc9c841d1183-d_640","thumbnail_width":640,"thumbnail_height":360,"thumbnail_url_with_play_button":"https:\/\/i.vimeocdn.com\/filter\/overlay?src0=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F752300323-c9b7699f9dc14ec1484a39edca5506c5bf745e081cdab9d08fd9fc9c841d1183-d_640&src1=http%3A%2F%2Ff.vimeocdn.com%2Fp%2Fimages%2Fcrawler_play.png","upload_date":"2019-01-15 10:03:56","video_id":311468941,"uri":"\/videos\/311468941"}

getting the 1080p image is still possible via JS, but delivering via PHP would be ideal! welcome any other takes here...

@fo-rk
Copy link

fo-rk commented Feb 8, 2023

Hey @shaunkardinal, have a look at my last comment: you can use PHP to replace the ..._640 at the end of thumbnail_url with any crop you like, eg _1920x1080. It's undocumented, but works.

@shaunkardinal
Copy link

oh! i see the issue, a classic—the example code you gave had the separator/string mixed up! thanks again @monowales 🙏

return explode('_', $data->thumbnail_url)[0].'_1920x1080';

@sigkappel
Copy link

I just removed the resolution entirely and it give me the highest resolution image
return explode('_', $data->thumbnail_url)[0]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment