I’m building a site on which users can share YouTube videos. Therefore I need to fetch a thumbnail from the youtube servers by using the imagecreatefromjpeg function of php. Unfortunately, this does not work with every video. Some videos only result in this error message:
Warning:
imagecreatefromjpeg(http://img.youtube.com/vi/GY0wbp38REk/0.jpg)
[function.imagecreatefromjpeg]: failed to open stream: HTTP request
failed! HTTP/1.0 404 Not Found in ….
My script checks from 0.jpg up to 4.jpg whether a thumbnail is available, but there seems to be no thumbnail for certain videos.
How can I get the thumbnail of these videos?
Instead of guessing the address to the video, you have to use the oEmbed data they provide. Query
http://www.youtube.com/oembed?url=with the path to the url (watch?v=...) and parse the response, they provide thumbnail_url