Fetch YouTube highest thumbnail resolution

前端 未结 4 1014
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-01 20:36

I want to get youtube highest thumbnail \"maxresdefault.jpg\"

Like this one

http://i.ytimg.com/vi/Cj6ho1-G6tw/maxresdefault.jpg

I\'m using this simple ph

4条回答
  •  渐次进展
    2021-02-01 20:50

    You can use getimagesize() and check if the image exists (there's file_exists() too, but it may not work very well, in this case).

    You can use this function to fetch the greatest resolution screenshot of a particular video.

    Code:

    function fetch_highest_res ($videoid) {
        $resolutions = array('maxresdefault', 'hqdefault', 'mqdefault');     
        foreach($resolutions as $res) {
            $imgUrl = "http://i.ytimg.com/vi/$videoid/$res.jpg";
            if(@getimagesize(($imgUrl))) 
                return $imgUrl;
        }
    }
    

    Usage:

    echo fetch_highest_res('Cj6ho1-G6tw').'
    '; echo fetch_highest_res('VGazSZUYyf4');

    Output:

    http://i.ytimg.com/vi/Cj6ho1-G6tw/maxresdefault.jpg
    http://i.ytimg.com/vi/VGazSZUYyf4/hqdefault.jpg
    

    Note: This may not be the best solution, and it's a work-around if you don't want to use the API.

提交回复
热议问题