|
@@ -106,6 +106,136 @@ class Images |
|
@@ -106,6 +106,136 @@ class Images |
106
|
return $domain;
|
106
|
return $domain;
|
107
|
}
|
107
|
}
|
108
|
|
108
|
|
|
|
109
|
+
|
|
|
110
|
+ /**
|
|
|
111
|
+ * 图片上传
|
|
|
112
|
+ * @param string $name 文件表单name, 即用于$_FILES[$name]
|
|
|
113
|
+ */
|
|
|
114
|
+ public static function saveImage($name)
|
|
|
115
|
+ {
|
|
|
116
|
+ if (empty($_FILES[$name]))
|
|
|
117
|
+ {
|
|
|
118
|
+ return array();
|
|
|
119
|
+ }
|
|
|
120
|
+ $files = $_FILES[$name];
|
|
|
121
|
+ $images = array();
|
|
|
122
|
+ if (is_array($files['tmp_name']))
|
|
|
123
|
+ {
|
|
|
124
|
+ foreach ($files['tmp_name'] as $k => $tmp_name)
|
|
|
125
|
+ {
|
|
|
126
|
+ if(!empty($tmp_name))
|
|
|
127
|
+ {
|
|
|
128
|
+ $images[$files['name'][$k]] = $tmp_name;
|
|
|
129
|
+ }
|
|
|
130
|
+ }
|
|
|
131
|
+ }
|
|
|
132
|
+ else
|
|
|
133
|
+ {
|
|
|
134
|
+ $images[$files['name']] = $files['tmp_name'];
|
|
|
135
|
+ }
|
|
|
136
|
+ if($_SERVER['HTTP_HOST'] != 'test.service.api.yohobuy.com') //代理转接
|
|
|
137
|
+ {
|
|
|
138
|
+ return self::agentCurlImage($images);
|
|
|
139
|
+ }
|
|
|
140
|
+ else
|
|
|
141
|
+ {
|
|
|
142
|
+ return self::uploadStreamImage($images);
|
|
|
143
|
+ }
|
|
|
144
|
+ }
|
|
|
145
|
+
|
|
|
146
|
+ /**
|
|
|
147
|
+ * 上传图片[图片上传域名限制于http://test.service.api.yohobuy.com]
|
|
|
148
|
+ *
|
|
|
149
|
+ * @param string | array(filename => absolute file path) $file
|
|
|
150
|
+ * url:http://upload.static.yohobuy.com?project=sns&fileData=xxx
|
|
|
151
|
+ * @return mixed
|
|
|
152
|
+ */
|
|
|
153
|
+ public static function uploadStreamImage($file)
|
|
|
154
|
+ {
|
|
|
155
|
+ $end ="\r\n";
|
|
|
156
|
+ $twoHyphens ="--";
|
|
|
157
|
+ $boundary = "*****";
|
|
|
158
|
+ $stream = '';
|
|
|
159
|
+ $files = is_array($file) ? $file : array($file);
|
|
|
160
|
+ foreach($files as $name => $filename)
|
|
|
161
|
+ {
|
|
|
162
|
+ if(!file_exists($filename))
|
|
|
163
|
+ {
|
|
|
164
|
+ continue;
|
|
|
165
|
+ }
|
|
|
166
|
+ $name = is_numeric($name) ? name.'.jpg' : $name;
|
|
|
167
|
+ $stream .= $twoHyphens.$boundary.$end;
|
|
|
168
|
+ $stream .="Content-Disposition: form-data; "."name=\"fileData\";filename=\"".$name ."\"".$end; // form file element name :fileData
|
|
|
169
|
+ $stream .= $end;
|
|
|
170
|
+ $stream .= file_get_contents($filename);
|
|
|
171
|
+ $stream .= $end;
|
|
|
172
|
+ }
|
|
|
173
|
+ if(empty($stream))
|
|
|
174
|
+ {
|
|
|
175
|
+ return false;
|
|
|
176
|
+ }
|
|
|
177
|
+ $stream .= $twoHyphens.$boundary.$end;
|
|
|
178
|
+ $stream .="Content-Disposition: form-data; "."name=\"project\"".$end;
|
|
|
179
|
+ $stream .= $end;
|
|
|
180
|
+ $stream .= "sns";//project sns
|
|
|
181
|
+ $stream .= $end;
|
|
|
182
|
+ $stream .= $twoHyphens .$boundary .$twoHyphens .$end;
|
|
|
183
|
+ $opts = array(
|
|
|
184
|
+ 'http' => array(
|
|
|
185
|
+ 'method' => 'POST',
|
|
|
186
|
+ 'header' => 'content-type:multipart/form-data;boundary='.$boundary,
|
|
|
187
|
+ 'content' => $stream
|
|
|
188
|
+ )
|
|
|
189
|
+ );
|
|
|
190
|
+ $context = stream_context_create($opts);
|
|
|
191
|
+ $result = json_decode(file_get_contents('http://upload.static.yohobuy.com', false, $context), true);
|
|
|
192
|
+ if(!empty($result['data']['imagesList']))
|
|
|
193
|
+ {
|
|
|
194
|
+ return count($file) == 1 || !is_array($file) ? current($result['data']['imagesList']) : $result['data']['imagesList'] ;
|
|
|
195
|
+ }
|
|
|
196
|
+ else
|
|
|
197
|
+ {
|
|
|
198
|
+ return false;
|
|
|
199
|
+ }
|
|
|
200
|
+ }
|
|
|
201
|
+
|
|
|
202
|
+ /**
|
|
|
203
|
+ * 代理上传图片
|
|
|
204
|
+ *
|
|
|
205
|
+ * @param array|string $files
|
|
|
206
|
+ * @return array
|
|
|
207
|
+ */
|
|
|
208
|
+ private static function agentCurlImage($file)
|
|
|
209
|
+ {
|
|
|
210
|
+ $ch = curl_init();
|
|
|
211
|
+ curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
212
|
+ curl_setopt($ch, CURLOPT_VERBOSE, 0);
|
|
|
213
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
214
|
+ curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
|
|
|
215
|
+ curl_setopt($ch, CURLOPT_URL, 'http://test.service.api.yohobuy.com/sns/ajax/uploadimg');
|
|
|
216
|
+ curl_setopt($ch, CURLOPT_POST, true);
|
|
|
217
|
+ $params = array();
|
|
|
218
|
+ $files = is_array($file) ? $file : array($file);
|
|
|
219
|
+ foreach($files as $key => $name)
|
|
|
220
|
+ {
|
|
|
221
|
+ $key = is_numeric($key) ? $key.'.jpg' : $key;
|
|
|
222
|
+ $filename = dirname($name).'/'.$key;
|
|
|
223
|
+ rename($name, $filename);
|
|
|
224
|
+ if (@class_exists('\CURLFile'))
|
|
|
225
|
+ {
|
|
|
226
|
+ $params["images[$key]"] = new \CURLFile(realpath($filename));
|
|
|
227
|
+ }
|
|
|
228
|
+ else
|
|
|
229
|
+ {
|
|
|
230
|
+ $params["images[$key]"] = '@' . realpath($filename);
|
|
|
231
|
+ }
|
|
|
232
|
+ }
|
|
|
233
|
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
|
|
|
234
|
+ $response = json_decode(curl_exec($ch), true);
|
|
|
235
|
+ return $response['data'];
|
|
|
236
|
+
|
|
|
237
|
+ }
|
|
|
238
|
+
|
109
|
/**
|
239
|
/**
|
110
|
* 获取模板的图片地址
|
240
|
* 获取模板的图片地址
|
111
|
* @param $fileName
|
241
|
* @param $fileName
|