thumb($width, $height, $type)->save('./storage/' . $thumbname); return $result ? $thumbname : ''; } public function urlImg($url) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_NOBODY, 0); // 只取body头 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_NOSIGNAL, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $package = curl_exec($ch); $httpinfo = curl_getinfo($ch); curl_close($ch); $imageAll = array_merge(array( 'imgBody' => $package ), $httpinfo); if ($httpinfo['download_content_length'] > 4 * 1024 * 1024) { throw new Exception("文件太大", 1); } $type = null; switch ($imageAll['content_type']) { case 'image/gif': $type = "gif"; break; case 'image/webp': $type = "webp"; break; case 'image/jpeg': $type = "jpg"; break; case 'image/png': $type = "png"; break; default: $type = null; break; } // 腾讯公众号图片 if(strpos($url,'qpic.cn') !== false){ $urls = parse_url($url); if (isset($urls['query'])) { $query_arr = []; parse_str($urls['query'], $query_arr); $type = isset($query_arr['wx_fmt']) ? $query_arr['wx_fmt'] : null; $type = $type == 'jpeg' ? 'jpg' : $type; } } if (!$type) { throw new Exception("不支持的文件后缀", 1); } $temp = app()->getRuntimePath() . 'temp'; if (!file_exists($temp)) { mkdir($temp, 0755); } $tempname = $temp . '/php.' . $type; file_put_contents($tempname, $imageAll["imgBody"]); return new File($tempname); } /** * 遍历获取目录下的指定类型的文件 * @param $path * @param $allowFiles png|jpg|jpeg|gif|bmp|webp * @param array $files * @return array */ public static function getFiles($path, $allowFiles = 'png|jpg|jpeg|gif|bmp|webp', &$files = array()) { if (!is_dir($path)) return null; if (substr($path, strlen($path) - 1) != '/') $path .= '/'; $handle = opendir($path); while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') { $path2 = $path . $file; if (is_dir($path2)) { self::getFiles($path2, $allowFiles, $files); } else { if (preg_match("/\.(" . $allowFiles . ")$/i", $file)) { $files[] = array( 'url' => substr($path2, strlen(app()->getRootPath().'/public') - 1), 'mtime' => filemtime($path2) ); } } } } return $files; } }