search
Homephp教程php手册php获取远程图片并把它保存到本地

php获取远程图片并把它保存到本地

Jun 13, 2016 am 11:27 AM
phpusekeepWriteFunctionpictureexistituslocalofSimpleObtainRemotelycollection

在php中我们经常使用写一些简单的采集功能,这样可以自动把远程服务器的图片或资源直接采集保存到本地服务器中,下面我来给大家详细介绍远程图片并把它保存到本地几种方法。

例1

 代码如下 复制代码

/*
*功能: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;
}

例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="/static/imghwm/default1.png" data-src="'.$img.'" class="lazy" . alt="php获取远程图片并把它保存到本地" >
';else:echo "false";endif;  

?>


dedecms中的:

 

 代码如下 复制代码
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);
         }
?>

例4

 代码如下 复制代码

//
// 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=".%24img." alt="php获取远程图片并把它保存到本地" >
';}else{echo 'false';}
?>

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.