Browse Source

保存图片到本地

xialei 1 year ago
parent
commit
07515e22e2
1 changed files with 81 additions and 2 deletions
  1. 81 2
      app/controller/api/douhuomall/Take.php

+ 81 - 2
app/controller/api/douhuomall/Take.php

@@ -13,6 +13,7 @@ class Take
13 13
 
14 14
     public function get_goods_list(){
15 15
         try {
16
+            set_time_limit(0);
16 17
             $allData = [];
17 18
             $page = 1;
18 19
             $limit = 100;
@@ -40,12 +41,17 @@ class Take
40 41
                 $goodsInfo = GongApiRequest::get_goods_info($value['goods_id']);
41 42
                 if(!$goodsInfo) continue;
42 43
 
44
+                //图片存放路径
45
+                $targetDir = public_path() . 'images/gong/'.$value['goods_id'].'/';
46
+
47
+                $this->deleteFilesInDirectory($targetDir);
48
+
43 49
                 //spuId_info商品详细信息处理
44
-                $goodsInfo['cover_url'] = $goodsInfo['main_img'];
50
+                $goodsInfo['cover_url'] = $this->seveImg($value['goods_id'], $goodsInfo['main_img'], $targetDir);
45 51
                 $goodsInfo['spu_id'] = $goodsInfo['goods_id'];
46 52
 
47 53
                 $goodsInfo['detail'] = "";
48
-                $detail_img = $goodsInfo['detail_img'];
54
+                $detail_img = $this->seveImg($value['goods_id'], $goodsInfo['detail_img'], $targetDir);
49 55
                 foreach($detail_img as $img_url){
50 56
                     $img_url = "<img src='".$img_url."' />";
51 57
                     $goodsInfo['detail'] = $goodsInfo['detail'].$img_url;
@@ -92,6 +98,79 @@ class Take
92 98
         }
93 99
     }
94 100
 
101
+    public function seveImg($goods_id, $img_url, $targetDir)
102
+    {
103
+        if (is_array($img_url)){
104
+            $new_img_url = [];
105
+            foreach ($img_url as $value) {
106
+                $new_img_url[] = $this->seveImgUrl($goods_id, $value, $targetDir);
107
+            }
108
+        }else{  
109
+            $new_img_url = $this->seveImgUrl($goods_id, $img_url, $targetDir);
110
+        }
111
+        return $new_img_url;
112
+    }
113
+
114
+    public function seveImgUrl($goods_id, $remoteImageUrl, $targetDir)
115
+    {
116
+        // 目标路径(确保目录存在)
117
+        $targetDirTow = rand(1000, 10000). $this->upImgName(basename($remoteImageUrl));
118
+        $targetPath = $targetDir . $targetDirTow; // 使用basename获取文件名
119
+        
120
+        // 检查目标目录是否存在,如果不存在则创建
121
+        if (!is_dir($targetDir)) {
122
+            mkdir($targetDir, 0777, true); // 第二个参数是权限,第三个参数是递归创建
123
+        }
124
+        
125
+        // 获取远程图片内容
126
+        $imageContent = file_get_contents($remoteImageUrl);
127
+        
128
+        // 检查是否成功获取内容
129
+        if ($imageContent !== false) {
130
+            // 保存图片到指定路径
131
+            if (file_put_contents($targetPath, $imageContent)) {
132
+                $new_img_url = env('APP_URL').'/images/gong/'.$goods_id.'/'. $targetDirTow;
133
+                return $new_img_url;
134
+            } else {
135
+                echo "保存图片时发生错误。";
136
+                return $remoteImageUrl;
137
+            }
138
+        } else {
139
+            echo "无法获取远程图片内容。";
140
+            return $remoteImageUrl;
141
+        }
142
+    }
143
+
144
+
145
+    public function upImgName($filename)
146
+    {
147
+        $position = strpos($filename, '?');
148
+        if ($position !== false) {
149
+            $filename = substr($filename, 0, $position);
150
+        }
151
+        return $filename;
152
+    }
153
+
154
+    public function deleteFilesInDirectory($dir) {
155
+        if (!is_dir($dir)) return true;
156
+
157
+        // 打开目录
158
+        $files = scandir($dir);
159
+     
160
+        // 遍历目录中的所有文件
161
+        foreach ($files as $file) {
162
+            // 如果是文件(排除 . 和 ..)
163
+            if ($file !== '.' && $file !== '..') {
164
+                $filePath = $dir . '/' . $file;
165
+                // 删除文件
166
+                if (is_file($filePath)) {
167
+                    unlink($filePath);
168
+                }
169
+            }
170
+        }
171
+        return true;
172
+    }
173
+
95 174
 
96 175
     public function add($data)
97 176
     {