|
@@ -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
|
}
|