如何在本地服务器保存图片从youtube在php或cakephp


how to save image at local server from youtube in php or cakephp

$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);
    }

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

您只是在写缩略图url,而不是图像的内容。您必须从url中获取图像的内容,并将其写入图像文件。

以下是fopenfwrite的快捷方式。

$img_content=file_get_contents($thumbnail_url);
file_put_contents($fullpath,$img_content );

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