使用 CURL 保存图像,有时会保存空白图像
Save images with CURL, sometimes it saves blank images
我有这个脚本,我从特定网站获取图像 links,因此我创建了一个函数,我传递图像 link 和将用于放置的网站的源名称相应目录中的图像。
不过有时这个功能不能正常工作,它会随机保存图像,但图像基本上是空的,因此它只会保存一个空文件,其原始文件名来自$img_link,但是无法显示实际图像。
在那种情况下,如果发生这种情况,我会尝试 return 给我一个默认的图像路径。但它没有这样做,并且 return 如上所述是一个空图像。
function saveIMG($img_link, $source){
$name = basename($img_link); // gets basename of the file image.jpg
$name = date("Y-m-d_H_i_s_") . mt_rand(1,999) . "_" .$name;
if (!empty($img_link)){
$ch = curl_init($img_link);
$fp = fopen("images/$source/$name", 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
fclose($fp);
$name ="images/$source/$name";
return $name;
}
else {
$name = "images/news_default.jpg";
return $name;
}
}
你有什么更好的主意,当它无法检索图像时如何处理?
谢谢
使用文件获取内容
$data = file_get_contents($img_link);
//check it return data or not
if ( $data === false )
{
echo "failed";
}
file_get_content
始终是 cURL 的一个很好的替代品。
但是,如果您want/have使用 cURL:
$ch = curl_init("www.path.com/to/image.jpg");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //Return the transfer so it can be saved to a variable
$result = curl_exec($ch); //Save the transfer to a variable
if($result === FALSE){//curl_exec will return false on failure even with returntransfer on
//return? die? redirect? your choice.
}
$fp = fopen("name.jpg", 'w'); //Create the empty image. Extension does matter.
fwrite($fp, $result); //Write said contents to the above created file
fclose($fp); //Properly close the file
就是这样。测试了它并且有效。
我有这个脚本,我从特定网站获取图像 links,因此我创建了一个函数,我传递图像 link 和将用于放置的网站的源名称相应目录中的图像。
不过有时这个功能不能正常工作,它会随机保存图像,但图像基本上是空的,因此它只会保存一个空文件,其原始文件名来自$img_link,但是无法显示实际图像。
在那种情况下,如果发生这种情况,我会尝试 return 给我一个默认的图像路径。但它没有这样做,并且 return 如上所述是一个空图像。
function saveIMG($img_link, $source){
$name = basename($img_link); // gets basename of the file image.jpg
$name = date("Y-m-d_H_i_s_") . mt_rand(1,999) . "_" .$name;
if (!empty($img_link)){
$ch = curl_init($img_link);
$fp = fopen("images/$source/$name", 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
fclose($fp);
$name ="images/$source/$name";
return $name;
}
else {
$name = "images/news_default.jpg";
return $name;
}
}
你有什么更好的主意,当它无法检索图像时如何处理?
谢谢
使用文件获取内容
$data = file_get_contents($img_link);
//check it return data or not
if ( $data === false )
{
echo "failed";
}
file_get_content
始终是 cURL 的一个很好的替代品。
但是,如果您want/have使用 cURL:
$ch = curl_init("www.path.com/to/image.jpg");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //Return the transfer so it can be saved to a variable
$result = curl_exec($ch); //Save the transfer to a variable
if($result === FALSE){//curl_exec will return false on failure even with returntransfer on
//return? die? redirect? your choice.
}
$fp = fopen("name.jpg", 'w'); //Create the empty image. Extension does matter.
fwrite($fp, $result); //Write said contents to the above created file
fclose($fp); //Properly close the file
就是这样。测试了它并且有效。