Your Name 5 months ago
parent
commit
291e7bd0bd
2 changed files with 116 additions and 2 deletions
  1. 83
    1
      application/api/controller/Ai.php
  2. 33
    1
      application/cms/controller/Index.php

+ 83
- 1
application/api/controller/Ai.php View File

@@ -25,7 +25,7 @@ use think\Db;
25 25
 class Ai extends MemberApi
26 26
 {
27 27
 
28
-    protected $noNeedLogin = ['login', 'register','uploadCsv','uploadCsvFrom','exportCsv','dsTitle','dsTitlelist','dsTitle13'];
28
+    protected $noNeedLogin = ['login', 'register','uploadCsv','uploadCsvFrom','exportCsv','dsTitle','dsTitlelist','dsTitle13','bdPicHandle'];
29 29
     protected $noNeedRight = [];
30 30
 
31 31
     //初始化
@@ -684,4 +684,86 @@ class Ai extends MemberApi
684 684
         ]);
685 685
     }
686 686
 
687
+    /*
688
+     * 处理百度的的ai图片 变成本地的
689
+     * 定时任务处理
690
+     * http://console.zx2049.com/api/ai/bdPicHandle
691
+     */
692
+    public function bdPicHandle(){
693
+
694
+        //查找是否已处理图片 本地化
695
+        $id_list = Db::name('cms_ai')->where([
696
+                'catid'=>13,
697
+                'is_pic' => 0,
698
+                'is_sc' => 1, //已生成文章
699
+            ])->find();
700
+
701
+        if(!empty($id_list['pic_html'])){
702
+
703
+            //提取图片
704
+            //var_dump($id_list['pic_html']);
705
+
706
+            $str = htmlspecialchars_decode($id_list['pic_html']);
707
+            $pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png?]))[\'|\"].*?[\/]?>/";
708
+            preg_match_all($pattern,$str,$match);
709
+            print_r($match[1]);
710
+
711
+            //下载文件到本地服务器
712
+            $imgs = $match[1];
713
+            if(!empty($imgs)){
714
+                $dir_name = '/ai/images/'.date('Y-m-d',time()).'/';
715
+                $dir = '.'.$dir_name;
716
+                if(!is_dir($dir)) {
717
+                    mkdir($dir, 0777, true);
718
+                }
719
+                $new_img = [];
720
+                foreach ($imgs as $k => $v){
721
+                    //去除字符
722
+                    $v = str_replace("?", "", $v);
723
+                    $this->downloadImage($v, $dir);
724
+                    $filename = pathinfo($v, PATHINFO_BASENAME);
725
+                    $new_img[] = $dir_name.$filename;
726
+                }
727
+
728
+                //保存图片信息
729
+                $new_img = json_encode($new_img);
730
+                Db::name('cms_ai')->where([
731
+                        'id' => $id_list['id']
732
+                    ])->update([
733
+                        'is_pic' => 1,
734
+                        'imgs' => $new_img
735
+                    ]);
736
+            }
737
+        }
738
+    }
739
+
740
+    //保存文件方法
741
+    public function saveAsImage($url, $file, $path)
742
+    {
743
+        $filename = pathinfo($url, PATHINFO_BASENAME);
744
+        $resource = fopen($path . $filename, 'a');
745
+        fwrite($resource, $file);
746
+        fclose($resource);
747
+    }
748
+
749
+    //下载文件方法
750
+    public function downloadImage($url, $path)
751
+    {
752
+        $ch = curl_init();
753
+        curl_setopt($ch, CURLOPT_URL, $url);
754
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
755
+        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
756
+        //避免https 的ssl验证
757
+        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
758
+        curl_setopt($ch, CURLOPT_SSLVERSION, false);
759
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
760
+        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
761
+        $file = curl_exec($ch);
762
+        if(curl_exec($ch) === false) echo 'Curl error: ' . curl_error($ch);
763
+        curl_close($ch);
764
+        $this->saveAsImage($url, $file, $path);
765
+    }
766
+
767
+
768
+
687 769
 }

+ 33
- 1
application/cms/controller/Index.php View File

@@ -39,12 +39,22 @@ class Index extends Cmsbase
39 39
             'SEO'  => $seo,
40 40
             'page' => $page,
41 41
         ]);
42
+
43
+
44
+
45
+
46
+        return false;
47
+
48
+
49
+
42 50
         return $this->fetch('/index');
43 51
     }
44 52
 
45 53
     // 列表页
46 54
     public function lists()
47 55
     {
56
+
57
+
48 58
         $cat = $this->request->param('catid/d', 0);
49 59
         if (empty($cat)) {
50 60
             $cat = $this->request->param('catdir/s', '');
@@ -105,6 +115,8 @@ class Index extends Cmsbase
105 115
             'page'         => $page,
106 116
             'modelid'      => $modelid,
107 117
         ]);
118
+
119
+        return false;
108 120
         return $this->fetch('/' . $template);
109 121
 
110 122
     }
@@ -213,7 +225,27 @@ class Index extends Cmsbase
213 225
             'page'                => $page,
214 226
             'modelid'             => $modelid,
215 227
         ]);
216
-        return $this->fetch('/' . $template);
228
+
229
+
230
+
231
+        //查询主次
232
+        $for_one = Db::name('cms_zl')->where('id',$info['lyjlid'])->find();
233
+
234
+        $imgs = json_decode($info['imgs'],true);
235
+
236
+        echo "<div style='margin:0 auto;width:720px;'>";
237
+        echo "来源主词: &nbsp;&nbsp;&nbsp;&nbsp;".$for_one['zc'].'<br/><br/>';
238
+        echo $title.'<br/><hr/>';
239
+        echo '<p>'.$info['content'].'</p>';
240
+
241
+        foreach ($imgs as $k=>$v){
242
+            echo '<hr/><img src="'.$v.'" style="max-width:100%;"/> ';
243
+        }
244
+
245
+        echo "</div>";
246
+
247
+
248
+       // return $this->fetch('/' . $template);
217 249
     }
218 250
 
219 251
     // tags

Loading…
Cancel
Save