• 检出商品详情中的图片并替换url


    原有的批量导入是按照系统本身的功能导入商品,现在需要用接口将图片上传图片服务器

    所以需要将批量导入的商品图片取出来,上传后替换掉原来的url

    (1)检出详情中的图片,用文件名做key

    private function search_content_img($content){
            //匹配img标签src属性中?之前部分的正则
            $img_pattern = "|<img[^>]+src=['" ]?([^ '"?]+)['" >]|U";
            preg_match_all($img_pattern, $content, $img_out, PREG_SET_ORDER);
            $img_change = array();
            foreach ($img_out as $k => $value) {
                $tmp= $value[1];
                $file_info = pathinfo($tmp);
                $key = $file_info['filename'];
                $img_change[$key] = array(
                    'old_img'=> $tmp,
                    'new_img'=> '',
                    );
            }
            return $img_change;
        }
    

    (2)找出图片的实际路径,调用上传接口,将生成的url,保存到对应的key

    private function change_url($arr,$goods_img_path,$url,$goods_img_url){
    
            $count = 0;
            $sum = count($arr);
            foreach ($arr as $key => $value) {
                $old_img = $value['old_img'];
                $filename = "";
                if(!(strpos($old_img,'http://')===false)){
                    $filename = str_ireplace($goods_img_url,$goods_img_path,$old_img);
                }else{
                    $filename = $goods_img_path.'/'.$old_img;
                }
                if(!file_exists($filename)){      
                    continue;
                }
    
                $rs = $this->sendPicToServer($url, $filename);
                if($rs){
                    $data = json_decode($rs,true);
                    if($data['error']==0){
                        $arr[$key]['new_img'] = $data['data']['pic_src'];
                        $count++;
                    }
                }
            }
            return $arr;
        }
    

    (3)用curl上传图片

    function sendPicToServer($url, $filename,$use="avatar",$bizid="102"){
            $finfo    = finfo_open(FILEINFO_MIME);
            $mimetype = finfo_file($finfo, $filename);
            $type = explode(";",$mimetype);
            finfo_close($finfo);
            $s = curl_init();
            curl_setopt($s, CURLOPT_POST,true);
            $picture = null;
    
            if (PHP_VERSION<5.5) {
                $picture = "@$filename;type=$mimetype";
            }else{
                $picture = curl_file_create(@$filename,$type[0]);
            }
            $post_data = array (
                "picture" =>$picture,
            );
            curl_setopt($s, CURLOPT_POSTFIELDS, $post_data);
            curl_setopt($s, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
            curl_setopt($s, CURLOPT_URL,            $url);
            curl_setopt($s, CURLOPT_TIMEOUT,        1000);
            curl_setopt($s, CURLOPT_RETURNTRANSFER, 1);
            $body = curl_exec($s);
            if($body===false){
                $msg = 'Curl error no: ' . curl_errno($s).",";
                $msg.= 'Curl error info: ' . curl_error($s).".";
                $this->write_log($msg,1);
            } 
            curl_close($s);
            return $body;
        }
    

    (4)替换详情里的图片

    private function replace_content_img($content,$arr){
            $error = 1;
            if($arr){
                foreach ($arr as $key => $value) {
                    $content = str_replace($value['old_img'], $value['new_img'], $content);
                }
            }
            if(strpos($content,'/shopfw/')===FALSE){
                $error = 0;
            }
            $rs = array(
                'error'=>$error,
                'content'=>$content,
                );
            return $rs;
        }
    

      

     

      

      

  • 相关阅读:
    关于排序算法的记录
    java获取src下文件
    学习HashMap的笔记
    红黑树删除
    学习红黑树过程中的个人总结
    关于二叉树的记录
    关于自动装箱和自动拆箱
    学习函数的时候问题
    Oracle 实现拆分列数据的split()方法
    福大软工 · 最终作业
  • 原文地址:https://www.cnblogs.com/baby123/p/6477512.html
Copyright © 2020-2023  润新知