• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php 保留远程图片到本地

php 搞代码 3年前 (2022-01-24) 14次浏览 已收录 0个评论

php 保存远程图片到本地

    <?php        header('Content-Type:image/jpg');        echo file_get_contents("http://www.baidu.com/img/baidu_logo.gif");        ?> 

第一种: 精确型

<?php                 // 变量说明:         // $url 是远程图片的完整URL地址,<i>*本5文来源gaodai$ma#com搞$$代**码)网@</i><img>搞代码gaodaima</img>不能为空。        // $filename 是可选变量: 如果为空,本地文件名将基于时间和日期         // 自动生成.                 function get_photo($url,$filename='',$savefile='test/')       {             $imgArr = array('gif','bmp','png','ico','jpg','jepg');                if(!$url) return false;                  if(!$filename) {               $ext=strtolower(end(explode('.',$url)));               if(!in_array($ext,$imgArr)) return false;            $filename=date("dMYHis").'.'.$ext;             }                   if(!is_dir($savefile)) mkdir($savefile, 0777);          if(!is_readable($savefile)) chmod($savefile, 0777);                    $filename = $savefile.$filename;                ob_start();             readfile($url);             $img = ob_get_contents();             ob_end_clean();             $size = strlen($img);                     [email protected]($filename, "a");             fwrite($fp2,$img);             fclose($fp2);                     return $filename;          }                       $img=get_photo("http://www.baidu.com/img/baidu_logo.gif");          echo $img ? '<pre class="prettyprint linenums">

‘%20:%20″false”;%20%20

第二种:从文章中提取图片,并保存至本地

%20%20%20%20function%20getImg($str){%20%20%20%20%20%20%20%20%20%20$str%20=%20stripslashes($str);%20%20%20%20%20%20%20%20%20%20$pattern%20=%20"/]*src\=\"(([^>]*)(jpg|gif|png|bmp|jpeg))\"/i";%20%20%20//获取所有图片标签的全部信息%20%20%20%20%20%20%20%20%20%20preg_match_all($pattern,%20$str,%20$matches);%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20$matches[1];%20%20%20//$matches[1]中就是所想匹配的结果,结果为数组%20%20%20%20%20%20}%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20$str%20=%20<<<EOT%20%20%20%20%20%20Money%20has%20been%20moving%20into%20Brazilian%20stocks%20over%20the%20last%20couple%20of%20days,%20despite%20mostly%20flat%20trading%20activity%20in%20the%20U.S.%20equity%20markets.%20During%20Thursday's%20session,%20the%20iShares%20MSCI%20Brazil%20Index%20ETF%20(NYSE:%20EWZ)%20has%20risen%201.12%%20after%20outperforming%20the%20U.S.%20indices%20yesterday%20as%20well.%20%20%20%20%20%20%20%20%20%20%20%20Must%20Read%20%20%20%20%20%20SonySony%20Playstation%20Phone%20Rumors%20Heat%20Up%20%20%20%20%20%20A%20South%20Korean%20Army%20soldier%20walks%20up%20steps%20of%20a%20guard%20post%20near%20the%20demilitarised%20zone%20separating%20the%20two%20Koreas.S.%20Korea%20waves%20olive%20branch%20%20%20%20%20%20      Two giant resource companies make up a big chunk of the Brazilian Bovespa. They are PetroBras (NYSE: PBR), with a market cap of $163.44 billion, and miner Vale (NYSE: VALE), which has a market cap of $178.95 billion. PBR shares have risen 1.47% thus far today and VALE is trading 0.77% higher at $34.03.            This article was originally published on Benzinga, and is republished here with permission.       EOT;                  foreach( getImg($str) as $url)      {          get_photo($url);      }  

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php 保留远程图片到本地
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址