MENU

采集微信文章图片 下载到本地

March 23, 2018 • Code

//替换文章内的img src 标签
$content = preg_replace("/data-src/i","src", $content);
//替换https
        $content = preg_replace("/https/i","http", $content);
//上面两部是关键 下面就没有技术含量
    
            $rg = "/<img src="[\'|\" is="true" preg="true" match="true" all="true" rg="true" content="true" img="true" array="true" PREG="true" PATTERN="true" ORDER="true" foreach="true" as="true" if="true" qpic="true" cn="true" get="true" domain="true" FILE="true" tmp="true" name="true" function="true" url="true" pattern="true" matches="true" count="true"> 0) {
            return $matches[0];
        }else{
            $rs = parse_url($url);
            $main_url = $rs["host"];
            if(!strcmp(long2ip(sprintf("%u",ip2long($main_url))),$main_url)) {
                return $main_url;
            }else{
                $arr = explode(".",$main_url);
                $count=count($arr);
                $endArr = array("com","net","org","3322");//com.cn  net.cn 等情况
                if (in_array($arr[$count-2],$endArr)){
                    $domain = $arr[$count-3].".".$arr[$count-2].".".$arr[$count-1];
                }else{
                    $domain =  $arr[$count-2].".".$arr[$count-1];
                }
                return $domain;
            }// end if(!strcmp...)
        }// end if(count...)
    }// end function
    
    
    
    function wechat_download_img($img, $path = '../attachment/images/', $method = 'curl') {
        global $_W;
        $path .= $_W['uniacid'].'/';
        $year_mouth = date("Ym", time());
        $path .=  $year_mouth .'/'.date("d", time()) .'/3/';
        if( !file_exists($path) ) mkdir($path,0777,true);
    
        $info = @getimagesize($img);
        $mime = $info['mime'];
    
        $type = substr(strrchr($mime, '/'), 1);
        switch ($type){
            case 'jpeg':
                $image_create_func = 'ImageCreateFromJPEG';
                $image_save_func = 'ImageJPEG';
                $new_image_ext = 'jpg';
    
                // Best Quality: 100
                $quality = 100;
                break;
    
            case 'png':
                $image_create_func = 'ImageCreateFromPNG';
                $image_save_func = 'ImagePNG';
                $new_image_ext = 'png';
    
                // Compression Level: from 0  (no compression) to 9
                $quality = 0;
                break;
    
    
            case 'bmp':
                $image_create_func = 'ImageCreateFromBMP';
                $image_save_func = 'ImageBMP';
                $new_image_ext = 'bmp';
                break;
    
    
    
            case 'gif':
                $image_create_func = 'ImageCreateFromGIF';
                $image_save_func = 'ImageGIF';
                $new_image_ext = 'gif';
                break;
    
            case 'vnd.wap.wbmp':
                $image_create_func = 'ImageCreateFromWBMP';
                $image_save_func = 'ImageWBMP';
                $new_image_ext = 'bmp';
                break;
    
    
            case 'xbm':
                $image_create_func = 'ImageCreateFromXBM';
                $image_save_func = 'ImageXBM';
                $new_image_ext = 'xbm';
                break;
    
    
            default:
                $image_create_func = 'ImageCreateFromJPEG';
                $image_save_func = 'ImageJPEG';
                $new_image_ext = 'jpg';
        }
    
    
        $ext = strrchr($img, ".");
        $strlen = strlen($ext);
        $rm = mt_rand(10000000, 99999999);
        $new_name = time().mt_rand(1,99).basename(substr($img, 0, -$strlen).'_'.base64_encode(md5($rm))).'.'.$new_image_ext;
    
    
        //$save_to = $path."/blog_temp_".time().mt_rand(1,99).".".$new_image_ext;
        $save_to = $path."/".$new_name;
    
        //输出对象 组成跟$_FILE变量一样 得到后自己和平常图片上传处理一样了
        $img_info['name'] = basename($img);
        $img_info['type'] = $mime;
        $img_info['size'] = 1000;
        $img_info['tmp_name'] = tomedia($save_to);
        $img_info['error'] = 0;
    
        if($method == 'curl'){
            $fp = fopen($save_to, "wb");
    
            $refer = '';//这个一定要设置为 空
            $curl = curl_init($img);
            curl_setopt($curl, CURLOPT_REFERER, $refer);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
            curl_setopt($curl, CURLOPT_FILE, $fp);
            curl_exec($curl);
            curl_close($curl);
    
            fclose($fp);
    
        }
        return $img_info;
    }
Tags: None
Archives QR Code
QR Code for this page
Tipping QR Code
Comment Lists