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

/ / 2016-09-18   阅读:2489
php获取远程图片并保存到本地,这是一个比较通用的功能。 以下函数也写的比较通用,只需要传入一段html代码。 php获取远程图片并保存到本地,这个函数一般应用在编辑器和采集器中。 function auto_save_image($bo...
php获取远程图片并保存到本地,这是一个比较通用的功能。
以下函数也写的比较通用,只需要传入一段html代码。
php获取远程图片并保存到本地,这个函数一般应用在编辑器和采集器中。
function auto_save_image($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 = "uploadFiles/".date("Ym")."/";
    $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;
}

我要评论

昵称:
验证码:

最新评论

共0条 共0页 10条/页 首页 上一页 下一页 尾页
意见反馈