从网址保存图片

时间:2011-05-31 12:42:32

标签: php zend-framework

我从数组中提取所有img url并从外部站点获取图像URL ...

如何保存这些图片?

我尝试使用

$upload = new Zend_File_Transfer_Adapter_Http();
 $upload->setDestination("img/");

 # Returns the file name for 'doc_path' named file element
 $name = $upload->getFileName('http://zendguru.files.wordpress.com/2009/04/ajax-form1.jpg');

但我没有得到任何保存加上它的说法 调用未定义的方法: - Zend_File_Transfer_Adapter_Http::setOption()

2 个答案:

答案 0 :(得分:4)

我认为您无法使用Zend_File_Transfer_Adapter_Http来执行此操作,因为这仅适用于上传的文件。但是,您可以使用Zend_Http_Client。例如:

    $c = new Zend_Http_Client();
    $c->setUri('http://zendguru.files.wordpress.com/2009/04/ajax-form1.jpg');
    $result = $c->request('GET');        
    $img = imagecreatefromstring($result->getBody());
    imagejpeg($img,'img/test.jpg');
    imagedestroy($img);

答案 1 :(得分:1)

你不应该使用Zend。普通的PHP会做。

$ch = curl_init('http://zendguru.files.wordpress.com/2009/04/ajax-form1.jpg');
$fp = fopen('/img/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);