【问题标题】:How to upload image to my thumb folder from youtube image url如何从 youtube 图像 url 将图像上传到我的拇指文件夹
【发布时间】:2015-05-13 14:31:13
【问题描述】:

我使用此函数从 youtube 图像 url 获取二进制数据。但是在将 api v2 更改为 api v3 之后,我过去使用的功能不再起作用。该函数没有返回任何值。请帮我解决这个问题。

<?php

$thumbnail_link = 'https://i.ytimg.com/vi_webp/s4bw0HQotfU/0.jpg';


if ( function_exists('curl_init') ) 
    {

        $ch = curl_init();
        $timeout = 0;
        curl_setopt ($ch, CURLOPT_URL, $thumbnail_link);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

        // Getting binary data
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

        $image = curl_exec($ch);
        curl_close($ch);

        echo $image; //this will return an empty data why is it?



        //  create & save image;
        $img_res = @imagecreatefromstring($image);
        if($img_res === false)
            return FALSE;

        $img_width = imagesx($img_res);
        $img_height = imagesy($img_res);

        $resource = @imagecreatetruecolor($img_width, $img_height);

        if( function_exists('imageantialias'))
        {
            @imageantialias($resource, true); 
        }

        @imagecopyresampled($resource, $img_res, 0, 0, 0, 0, $img_width, $img_height, $img_width, $img_height);
        @imagedestroy($img_res);

        switch($ext)
        {
            case ".gif":
                //GIF
                @imagegif($resource, $upload_path . $thumb_name);
            break;
            case ".jpg":
                //JPG
                @imagejpeg($resource, $upload_path . $thumb_name);
            break;  
            case ".png":
                //PNG
                @imagepng($resource, $upload_path . $thumb_name);
            break;
        }


    }

?>

【问题讨论】:

    标签: api youtube youtube-api


    【解决方案1】:

    您的缩略图链接 i.ytimg.com/vi_webp/s4bw0HQotfU/0.jpg 不存在。

    您可以使用 v3 API 获取正确的缩略图网址: https://developers.google.com/youtube/v3/docs/videos/list

    在上述情况下,它将是 GET https://www.googleapis.com/youtube/v3/videos?part=snippet&id=s4bw0HQotfU&key={YOUR_API_KEY}

    然后你处理结果以获得可用的缩略图:

    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/s4bw0HQotfU/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/s4bw0HQotfU/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/s4bw0HQotfU/hqdefault.jpg",
      "width": 480,
      "height": 360
     },
     "standard": {
      "url": "https://i.ytimg.com/vi/s4bw0HQotfU/sddefault.jpg",
      "width": 640,
      "height": 480
     },
     "maxres": {
      "url": "https://i.ytimg.com/vi/s4bw0HQotfU/maxresdefault.jpg",
      "width": 1280,
      "height": 720
     }
    },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-10
      • 2019-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多