In PHP, we often write some simple collection functions, which can automatically collect and save images or resources from remote servers directly to the local server. Now I will introduce to you in detail several methods of remote images and saving them locally.
Example 1
The code is as follows | Copy code |
代码如下 | 复制代码 | /* *功能:php多种方式完美实现下载远程图片保存到本地 *参数:文件url,保存文件名称,使用的下载方式 *当保存文件名称为空时则使用远程文件原来的名称 */ function getImage($url,$filename='',$type=0){ if($url==''){return false;} if($filename==''){ $ext=strrchr($url,'.'); if($ext!='.gif' && $ext!='.jpg'){return false;} $filename=time().$ext; } //文件保存路径 if($type){ $ch=curl_init(); $timeout=5; curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $img=curl_exec($ch); curl_close($ch); }else{ ob_start(); readfile($url); $img=ob_get_contents(); ob_end_clean(); } $size=strlen($img); //文件大小 $fp2=@fopen($filename,'a'); fwrite($fp2,$img); fclose($fp2); return $filename; } | /* *Function: PHP has multiple ways to perfectly download remote images and save them locally *Parameters: file url, save file name, Download method used *When the saved file name is empty, the original name of the remote file is used */ function getImage($url,$filename='',$type=0) { if($url==''){return false;} if($filename==''){ $ext=strrchr($url,'.'); if($ext!='.gif' && $ext!='.jpg'){return false;} /> //File saving path if($type){ $ch=curl_init(); $timeout=5; curl_setopt($ch,CURLOPT_URL,$url ); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $img=curl_exec($ch); curl_close( $ch); }else{ ob_start(); readfile($url); $img=ob_get_contents(); ob_end_clean(); /> } $size=strlen($img); //File size $fp2=@fopen($filename,'a'); fwrite($fp2 ,$img); fclose($fp2); return $filename; }
|
Example 2
代码如下 | 复制代码 |
// // Function: 获取远程图片并把它保存到本地 // // // 确定您有把文件写入本地服务器的权限 // // // 变量说明: // $url 是远程图片的完整URL地址,不能为空。 // $filename 是可选变量: 如果为空,本地文件名将基于时间和日期 // 自动生成. function GrabImage($url,$filename="") { if($url==""):return false;endif; if($filename=="") { $ext=strrchr($url,"."); if($ext!=".gif" && $ext!=".jpg"):return false;endif; $filename=date("dMYHis").$ext; } ob_start(); readfile($url); $img = ob_get_contents(); ob_end_clean(); $size = strlen($img); $fp2=@fopen($filename, "a"); fwrite($fp2,$img); fclose($fp2); return $filename; } $img=GrabImage("http://www.bkjia.com",""); if($img):echo '
<img src="'.$img.'"> ';else:echo "false";endif;
?>
|
The code is as follows | Copy code |
<🎜>// // Function: Get the remote image and save it locally // //< br />// Make sure you have the permission to write the file to the local server // // // Variable description: // $url is the complete version of the remote image URL address, cannot be empty. // $filename is an optional variable: if empty, the local file name will be automatically generated based on time and date // Automatically generated.<🎜><🎜>function GrabImage($url,$filename="" ) { if($url==""):return false;endif;<🎜><🎜> if($filename=="") { $ext=strrchr($url," ."); if($ext!=".gif" && $ext!=".jpg"):return false;endif; $filename=date("dMYHis").$ext ; }<🎜><🎜> ob_start(); readfile($url); $img = ob_get_contents(); ob_end_clean(); $ size = strlen($img);<🎜><🎜> $fp2=@fopen($filename, "a"); fwrite($fp2,$img); fclose($fp2) ;<🎜><🎜> return $filename; }<🎜><🎜> $img=GrabImage("http://www.bkjia.com",""); if($img):echo '<img src="'.$img.'"> ';else:echo "false";endif; ?> |
dedecms中的:
代码如下 | 复制代码 |
if(!empty($saveremoteimg)) 代码如下 | 复制代码 | if(!empty($saveremoteimg)) { $body = stripslashes($body); $img_array = array(); preg_match_all("/(src|SRC)=["|'| ]{0,}(http://(.*).(gif|jpg|jpeg|bmp|png))/isU",$body,$img_array); $img_array = array_unique($img_array[2]); set_time_limit(0); $imgUrl = $img_dir."/".strftime("%Y%m%d",time()); $imgPath = $base_dir.$imgUrl; $milliSecond = strftime("%H%M%S",time()); if(!is_dir($imgPath)) @mkdir($imgPath,0777); foreach($img_array as $key =>$value) { $value = trim($value); $get_file = @file_get_contents($value); $rndFileName = $imgPath."/".$milliSecond.$key.".".substr($value,-3,3); $fileurl = $imgUrl."/".$milliSecond.$key.".".substr($value,-3,3); if($get_file) { $fp = @fopen($rndFileName,"w"); @fwrite($fp,$get_file); @fclose($fp); } $body = ereg_replace($value,$fileurl,$body); } $body = addslashes($body); } ?> | { $body = stripslashes($body); $img_array = array(); preg_match_all("/(src|SRC)=["|'| ]{0,}(http://(.*).(gif|jpg|jpeg|bmp|png))/isU",$body,$img_array); $img_array = array_unique($img_array[2]); set_time_limit(0); $imgUrl = $img_dir."/".strftime("%Y%m%d",time()); $imgPath = $base_dir.$imgUrl; $milliSecond = strftime("%H%M%S",time()); if(!is_dir($imgPath)) @mkdir($imgPath,0777); foreach($img_array as $key =>$value) { $value = trim($value); $get_file = @file_get_contents($value); $rndFileName = $imgPath."/".$milliSecond.$key.".".substr($value,-3,3); $fileurl = $imgUrl."/".$milliSecond.$key.".".substr($value,-3,3); if($get_file) { $fp = @fopen($rndFileName,"w"); @fwrite($fp,$get_file); @fclose($fp); } $body = ereg_replace($value,$fileurl,$body); } $body = addslashes($body); }?> |
Example 4
The code is as follows 代码如下 | 复制代码 | // // Function: 获取远程图片并把它保存到本地 // // // 确定您有把文件写入本地服务器的权限 // // // 变量说明: // $url 是远程图片的完整URL地址,不能为空。 // $filename 是可选变量: 如果为空,本地文件名将基于时间和日期// 自动生成. function GrabImage($url,$filename='') { if($url==''):return false;endif; if($filename=='') { $ext=strrchr($url,'.'); if($ext!='.gif' && $ext!='.jpg'):return false;endif;$filename=date('dMYHis').$ext; } ob_start(); readfile($url); $img = ob_get_contents(); ob_end_clean(); $size = strlen($img); $fp2=@fopen($filename, 'a'); fwrite($fp2,$img); fclose($fp2); return $filename; } $img=GrabImage('http://www.ccc.cc/static/image/common/logo.png',''); if($img){echo ' <img src='.$img.'> ';}else{echo 'false';} ?> | |
Copy code |
|
//
// Function: Get the remote image and save it locally
//
/ /
// Make sure you have permission to write files to the local server
//
//
// Variable description:
// $url is a remote image The complete URL address cannot be empty.
// $filename is an optional variable: if empty, the local filename will be automatically generated based on time and date //.
function GrabImage($url,$filename='') {
if($url==''):return false;endif;
if($filename=='') {
$ext=strrchr($url,'.');
if($ext!='.gif' && $ext!='.jpg'):return false;endif;$filename=date('dMYHis').$ext;
}
ob_start( );
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($img);
$fp2 =@fopen($filename, 'a');
fwrite($fp2,$img);
fclose($fp2);
return $filename;
}
$img=GrabImage('http://www.ccc.cc/static/image/common/logo.png','');
if($img){echo '
< ;img src='.$img.'>
';}else{echo 'false';}
?>
http://www.bkjia.com/PHPjc/444614.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/444614.htmlIn php, we often use to write some simple collection functions, which can automatically collect pictures or resources from remote servers directly The collection is saved to the local server. Let me give you the details below...