How to gracefully handle YouTube thumbnails with broken images when shooting a video?

Our site contains several lists of YouTube videos with their thumbnails related to the films themselves. We get the thumbnail URLs from the YouTube API and the hotlink for them in place on the YouTube servers. So, our image tags look like this:

<img src="http://i.ytimg.com/vi/o6CHqSN7O-o/2.jpg" alt="" width="133" height="78" />

The problem is that sometimes the video is deleted - as well as the thumbnail. We do not know when this can happen, and our thumbnails simply turn into broken images. How can we handle this?

There are a number of solutions:

  • Download the thumbnails and save them locally - the film will not work if it is deleted, of course, but this is normal, it will explain that it was deleted, and we avoid the broken image.

  • Check the API periodically to see if the thumbnail has changed, if it no longer exists, replace our own snapshot with the movie removed. It's pretty hard to call an API!

  • Use javascript to replace broken images (don't like it a lot)

Our ideal solution would be to point img src to a location on YouTube that displays a friendly “remote” image when the movie is released. However, it does not seem to exist.

Has anyone else dealt with this? Thank!

+3
source share
2 answers

YouTube , , . , , . . PHP-:

<?php
$filename = $this->get_thumbnail_filename_from_url($url); // any way to map an image URL into a filename - e.g. strip everything except [a-zA-Z0-9] from URL
if (!file_exists($cachedir . '/' . $filename)) {
   $filename = $this->get_image_from_web_save_to_cache($url,$cachedir); // makes a HTTP request to the URL, saves the returned image (if any) into cache
}
$filename = basename($filename);
echo "<img src=\"/cache/$filename\">"; // display your cached thumbnail
?>

: , ; YT. , - (, ...) HTTP. :

  • - ?
    • , ​​ ? ( ..)
      • (last-modified, etag ..),
  • " " 404, ""
+1

PHP , HTTP 200. 404 - , - ( ).

0

Source: https://habr.com/ru/post/1744112/


All Articles