如何在php或cakephp中从youtube保存本地服务器上的图像

时间:2011-06-23 12:04:13

标签: php cakephp cakephp-1.3 cakephp-1.2

$thumbnail_url = 'http://img.youtube.com/vi/JaFfJN_iKdA/default.jpg';


function save_image_local($thumbnail_url)
    {
         //for save image at local server
         $filename = time().'_hk.jpg';
         $fullpath = '../../app/webroot/img/daily_videos/image/'.$filename;
        $fp = fopen($fullpath,'x');
        fwrite($fp, $thumbnail_url);
        fclose($fp);

    }

在此代码中,空白图像存储。不是原始图像存储。

1 个答案:

答案 0 :(得分:4)

您只是编写缩略图网址而非图像内容。您必须从网址获取图像内容并将其写入图像文件。

以下是fopenfwrite

的捷径
$img_content=file_get_contents($thumbnail_url);
file_put_contents($fullpath,$img_content );

OR

$img_content=file_get_contents($thumbnail_url);
$fp = fopen($fullpath,'x');
fwrite($fp, $img_content);
fclose($fp);