Home > php教程 > php手册 > php获取远程图片保存到本地

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 19:53:14
Original
946 people have browsed it

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 分享一例php获取远程图片的代码,php获取远程图片文件并保存到本地,不错的一段代码,有需要的朋友参考下。 本函数主要功能: 获取远程图片并把它保存到本地,确定有把文件写入本地服务器的权限。 变

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

  分享一例php获取远程图片的代码,php获取远程图片文件并保存到本地,不错的一段代码,有需要的朋友参考下。

  本函数主要功能:

  获取远程图片并把它保存到本地,确定有把文件写入本地服务器的权限。

  变量说明: $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

  if($img):echo '

<img  src="'.%24img.'" alt="php获取远程图片保存到本地" >
Copy after login
';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);

  }

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

source:php.cn
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template