| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 | <?phpdeclare(strict_types=1);/** * 文件 service * * @version      0.0.1 * @author      by huwhois * @time        2021/12/28 */namespace app\common\service;use think\Image;use think\facade\Filesystem;use think\Exception;use think\File;class FileService{    /**     * 生成缩略图     * @param string $filename 必须参数, 图片路径名(相对or绝对)(/public/uploads/...)     * @param int $width 缩略图宽值, 默认 384px;     * @param int $height 缩略图高值, 默认 224px;     * @param int $type 缩略图裁剪方式, 默认值 1, 固定尺寸缩放; 其他: 1, 等比例缩放;     * 2, 缩放后填充; 3, 居中裁剪; 4, 左上角裁剪; 5, 右下角裁剪     * @return string $thumbname 缩略图文件名     */    public static function makeThumb($filename, $width = 384, $height = 224, $type = 1, $t_suffix = "thumb")    {        $file = './storage/' . str_replace('\\', '/', $filename);        $ext = pathinfo($file, PATHINFO_EXTENSION);        $thumbname = str_replace('.' . $ext, '', str_replace('\\', '/', $filename)) . $t_suffix . '.' . $ext;        $image = Image::open($file);        $result = $image->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_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 (!$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;    }}
 |