php 保留远程图片到本地

php中文网
发布: 2016-06-13 10:50:22
原创
1013人浏览过

php 保存远程图片到本地

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

第一种: 精确型

   
<?php                 // 变量说明:         // $url 是远程图片的完整URL地址,不能为空。        // $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&#160;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="brush:php;toolbar:false;"><img  src="'.$img.'" alt="php 保留远程图片到本地" >
登录后复制
' : "false";

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

    function getImg($str){          $str = stripslashes($str);          $pattern = "/<img [^ alt="php 保留远程图片到本地" >]*src\=\"(([^>]*)(jpg|gif|png|bmp|jpeg))\"/i";   //获取所有图片标签的全部信息          preg_match_all($pattern, $str, $matches);                      return $matches[1];   //$matches[1]中就是所想匹配的结果,结果为数组      }                $str = <<<EOT      Money has been moving into Brazilian stocks over the last couple of days, despite mostly flat trading activity in the U.S. equity markets. During Thursday's session, the iShares MSCI Brazil Index ETF (NYSE: EWZ) has risen 1.12% after outperforming the U.S. indices yesterday as well.            Must Read      SonySony Playstation Phone Rumors Heat Up      A South Korean Army soldier walks up steps of a guard post near the demilitarised zone separating the two Koreas.S. Korea waves olive branch      <img    style="max-width:90%"  style="max-width:90%" alt="" src="http://img.ibtimes.com/www/site/us/images/1px.gif" sized="yes">      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.      <img alt="Sony" src="http://img.ibtimes.com/www/thumb/mainpage/13463-12079-sony.jpg" sized="yes">      This article was originally published on Benzinga, and is republished here with permission.       EOT;                  foreach( getImg($str) as $url)      {          get_photo($url);      }  
登录后复制

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号