• 检出商品详情中的图片并替换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;
        }
    

      

     

      

      

  • 相关阅读:
    Google官方关于Android架构中MVP模式的示例续-DataBinding
    值不值
    [译]Godot系列教程五
    [译]Google官方关于Android架构中MVP模式的示例
    遭遇Web print
    如何寻找有价值的点
    充电时间 Go中的数组、切片、map简单示例
    一段良好的程序永远不应该发生panic异常
    居然是Firefox没有抛弃我们
    macOS 升级到了10.12.1
  • 原文地址:https://www.cnblogs.com/baby123/p/6477512.html
Copyright © 2020-2023  润新知