微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

thinkphp获取远程图片

后台添加文章时,直接下载远程图片

 

$str = 'http://www.thinkphp.cn/Uploads/info/2013-07-24/51ef32f4490e3_100_100.jpg&http://www.thinkphp.cn/Uploads/ad/2013-05-09/518b1b272d448.jpg&http://www.thinkphp.cn/Uploads/ad/2013-07-02/51d24ce472da0.jpg&http://www.thinkphp.cn/Uploads/ad/2013-07-04/51d4c7c4aa490.jpg';

auto_save_image($str);

function auto_save_image($body){

    $img_array = explode('&',$body);

    /*$img_array = array();

    preg_match_all("/(src)=["|'| ]{0,}(http://(.*).(gif|jpg|jpeg|bmp|png))["|'| ]{0,}/isU",$body,$img_array);

    $img_array = array_unique($img_array[2]);*/ //也可以自动匹配

    set_time_limit(0);

    $imgPath = "uploads/allimg/".date("Ymd")."/";

    $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);

            if($get_file)

            {

                    $fp = @fopen($rndFileName,"w");

                    @fwrite($fp,$get_file);

                    @fclose($fp);

            }

            $body = @ereg_replace($value,$rndFileName,$body);

    }

 

    return $body;

 

 

单图片采集

$str = 'http://www.thinkphp.cn/Uploads/info/2013-07-24/51ef32f4490e3_100_100.jpg';

auto_save_image($str);

 

function auto_save_image($imgurl){

        set_time_limit(0);

        $imgPath = "./Uploads/Picture/".date("Y-m-d");

        if(!is_dir($imgPath)) @mkdir($imgPath,0777);

 

        $get_file = @file_get_contents($imgurl);

        $rndFileName = $imgPath."/".time().".jpg";

        if($get_file)

        {

                $fp = @fopen($rndFileName,"w");

                @fwrite($fp,$get_file);

                @fclose($fp);

        }

        return $rndFileName;

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐