{"id":847,"date":"2022-06-13T17:20:50","date_gmt":"2022-06-13T09:20:50","guid":{"rendered":"https:\/\/usei.cn\/?p=847"},"modified":"2022-09-09T16:20:18","modified_gmt":"2022-09-09T08:20:18","slug":"nginx-%e8%87%aa%e5%8a%a8%e6%88%aa%e5%9b%be%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/usei.cn\/index.php\/2022\/06\/13\/nginx-%e8%87%aa%e5%8a%a8%e6%88%aa%e5%9b%be%e5%9b%be%e7%89%87\/","title":{"rendered":"nginx \u81ea\u52a8\u622a\u56fe\u56fe\u7247"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\u524d\u7aef\u5730\u5740\u66ff\u6362<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!--\u539f\u56fe\u7247--&gt;\n&lt;img src='\/uploads\/1.jpg'&gt;\n&lt;!--\u538b\u7f29\u5f15\u7528\u56fe\u7247--&gt;\n&lt;img src='\/uploads\/1.jpg!size=130X130'&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">nginx \u914d\u7f6e\u8f6c\u53d1\u89c4\u5219<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>    # --------------------\u81ea\u52a8\u62c6\u56fe----------\u2193-----------------------------\n    # \u7f29\u7565\u56fe\u53c2\u6570\u91cd\u5b9a\u5411  x.jpg!size=s \u5b9a\u4f4d\u5230 s_x.jpg\n    location ~ .*\\.(gif|jpg|jpeg|png|bmp)!size=(.+)$\n    {\n        expires      30d; # \u7f13\u51b2\u65f6\u95f4\n        rewrite ^(.+)\/(&#91;a-zA-Z0-9]+\\.(gif|jpg|jpeg|png|bmp))!size=(.+)$ $1\/$4_$2;\n    }\n    # \u52a8\u6001\u8c03\u7528\u56fe\u7247\u88c1\u56fe\u5de5\u5177 s_x.jpg \u4e0d\u5b58\u5728\u5219\u88c1\u56fe\n    location ~ .*(.+)\/(.+)_(&#91;a-zA-Z0-9]+\\.(gif|jpg|jpeg|png|bmp))$\n    {\n      try_files $uri \/admin\/Resources\/ResizeImage\/?url=$uri;\n    }\n     # --------------------\u81ea\u52a8\u62c6\u56fe---------\u2191------------------------------<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u540e\u7aef\u62c6\u56fe\u4fdd\u5b58\u4ee3\u7801<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nnamespace app\\admin\\controller;\n\nuse think\\Controller;\nuse think\\facade\\Env;\n\n\/**\n * =============== \u6587\u4ef6\u4e0a\u4f20 \u4e8e\u8d44\u6e90\u7ba1\u7406  =========\n * 1.\u56fe\u7247\uff1a\u4e0a\u4f20\u3001\u81ea\u5b9a\u88c1\u56fe\n * \u8d44\u6e90\u6587\u4ef6\u7ba1\u7406  \u4e0a\u4f20\/\u8bbf\u95ee\u65f6\u81ea\u52a8\u88c1\u56fe\u4fdd\u5b58\n * Class Resources\n * @package app\\admin\\controller\n *\/\nclass Resources extends  Controller {\n    \/**\n     * \u56fe\u7247\u8bbf\u95ee\u81ea\u52a8\u5207\u56fe\u65b9\u6cd5\n     * \u7b2c\u4e00\u6b65\uff1a\u539f\u59cb\u8bbf\u95ee\uff1axxxxx.jpg!size=150x150\n     * \u7b2c\u4e8c\u6b65\uff1anginx \u8def\u7531\u8f6c\u5199\u6210150X150_xxxx.jpg\n     * \u7b2c\u4e09\u6b65\uff1anginx \u5bf9\u89c4\u5219\u8def\u5f84\u91cd\u5b9a\u5411\u5230 Resources\/ResizeImage?url=150X150_xxxx.jpg\n     *\/\n    public function ResizeImage($url){\n        \/\/ \u6b63\u5219\u5339\u914d .jpg\n        $pattern = \"\/(.+\\\/)(.+)_(&#91;a-zA-Z0-9]+\\.(gif|jpg|jpeg|png|bmp))\/\";\n        preg_match($pattern,$url,$res);\n\n        if(count($res)&gt;0){\n            $folder = $res&#91;1];\n            $size = $res&#91;2];\n            $filename= $res&#91;3];\n            $filePath=$folder.$filename;\n            \/\/\u67e5\u8be2\u6587\u4ef6\u662f\u5426\u5b58\u5728\n            \/\/\u88c1\u526a\u4e0d\u540c\u5c3a\u5bf8\n            $width =0;\n            $height=0;\n\n            switch ($size){\n                case \"150X150\": $width =150;$height = 150;break;\n                case \"300X300\": $width =300;$height = 300;break;\n                case \"750X750\": $width =750;$height = 750;break;\n                case \"750X469\": $width =750;$height = 469;break;\n                case \"640X320\": $width =640;$height = 320;break;\n                default:$this-&gt;error(\"\u683c\u5f0f\u5316\u53c2\u6570\u9519\u8bef\");\n            }\n            $path = $this-&gt;CutImage($filePath,$width,$height,$size);\n            $fullpath = Env::get('root_path').'public\/'.$path;\n            \/\/\u8fd4\u56de\u6570\u636e\n            $mime = getimagesize($fullpath)&#91;\"mime\"];\n            $img = file_get_contents($fullpath,true);\n            return response($img,200,&#91;\"Content-Length\"=&gt;strlen($img),])-&gt;contentType($mime);\n        }\n        else{\n            return json(&#91;\"code\"=&gt;500,\"msg\"=&gt;\"\u683c\u5f0f\u5316\u53c2\u6570\u9519\u8bef\"]);\n        }\n\n    }\n    private function CutImage($filePath,$width,$height,$Prefix){\n        \/\/ \u5224\u65ad\u539f\u59cb\u6587\u4ef6\u662f\u5426\u5b58\u5728\n        $root=Env::get('root_path').'public';\n\n        if(!file_exists($root.$filePath)){\n            $this-&gt;error('\u539f\u59cb\u6587\u4ef6\u4e0d\u5b58\u5728');\n        }\n        \/\/ \u5224\u65ad\u65b0\u6587\u4ef6\u662f\u5426\u5b58\u5728\n        $FileName= substr(strrchr($filePath, '\/'), 1);\/\/ 221212.jpg\n        $Folder =str_replace($FileName,'',$filePath);\n        $newFileName  = $Prefix.\"_\".$FileName;\/\/ s_22212.jpg\n\n        \/\/\u65b0\u5c3a\u5bf8\u6587\u4ef6\u5b58\u5728\u5219\u76f4\u63a5\u8fd4\u56de\u76f8\u5bf9\u8def\u5f84\n        if(file_exists($root.$Folder.$newFileName)){\n            return $Folder.$newFileName;\n        }\n        \/\/\u8bfb\u53d6\u56fe\u7247\n        $image = \\think\\Image::open($root.$filePath);\n\n        \/\/\u4fdd\u5b58\u88c1\u526a\u540e\u7684\u56fe\u7247\n        $image-&gt;thumb($width, $height,\\think\\Image::THUMB_CENTER)\n            -&gt;save($root.$Folder.$newFileName);\n\n        return $Folder.$newFileName;\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>Java<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> \/**\r\n     * \u6839\u636e\u53c2\u6570\u88c1\u56fe\r\n     * @return\r\n     *\/\r\n    @GetMapping(value =\"\/resizeImage\", produces = MediaType.IMAGE_JPEG_VALUE)\r\n    @Anonymous\r\n    public byte&#91;] resizeImage(@RequestParam(\"url\") String url) throws IOException {\r\n        String pattern = \"(.+\/)(.+)_(&#91;a-zA-Z0-9]+\\\\.(gif|jpg|jpeg|png|bmp))\";\r\n        \/\/ \u521b\u5efa Pattern \u5bf9\u8c61\r\n        Pattern r = Pattern.compile(pattern);\r\n        \/\/ \u521b\u5efa matcher \u5bf9\u8c61\r\n        Matcher m = r.matcher(url);\r\n        if(m.find()){\r\n            String folder = m.group(1);\r\n            String size = m.group(2);\r\n            String filename= m.group(3);\r\n\r\n            String filePath=folder+filename;\r\n            \/\/\u67e5\u8be2\u6587\u4ef6\u662f\u5426\u5b58\u5728\r\n            \/\/\u88c1\u526a\u4e0d\u540c\u5c3a\u5bf8\r\n            Integer width =0,height=0;\r\n            switch (size){\r\n                case \"150X150\": width =150;height = 150;break;\r\n                case \"300X300\": width =300;height = 300;break;\r\n                case \"480X640\": width =480;height = 640;break;\r\n                case \"640X480\": width =640;height = 480;break;\r\n                case \"750X469\": width =750;height = 469;break;\r\n                case \"750X750\": width =750;height = 750;break;\r\n                case \"640X320\": width = 640;height = 320;break;\r\n                default:throw new FileNotFoundException(\"\u683c\u5f0f\u5316SIZE\u53c2\u6570\u9519\u8bef\");\r\n            }\r\n            \/\/\u88c1\u526a\u56fe\u7247\r\n            String path = ImageUtils.cutImage(filePath,width,height,size);\r\n            \/\/\u8fd4\u56de\u5b57\u8282\u6570\u636e\r\n            return  ImageUtils.readFile(path);\r\n        }\r\n        else{\r\n            throw new FileNotFoundException(\"\u683c\u5f0f\u5316\u53c2\u6570\u9519\u8bef\");\r\n        }\r\n    }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\r\n     * \u526a\u5207\u56fe\u7247\r\n     * @param url\r\n     * @param width\r\n     * @param height\r\n     * @param Prefix \u5207\u56fe\u540e\u524d\u7f00\r\n     *\/\r\n    public static String cutImage(String url,int width, int height,String Prefix) throws IOException {\r\n        \/\/ \u5224\u65ad\u539f\u59cb\u6587\u4ef6\u662f\u5426\u5b58\u5728\r\n        \/\/ \u672c\u673a\u5730\u5740\r\n        String localPath = RuoYiConfig.getProfile();\r\n        String downloadPath = StringUtils.substringAfter(url, Constants.RESOURCE_PREFIX);\r\n        Path rootLocation = Paths.get(localPath, downloadPath);\r\n        if(Files.notExists(rootLocation)){\r\n            throw new FileNotFoundException(\"\u539f\u59cb\u6587\u4ef6\u4e0d\u5b58\u5728\");\r\n        }\r\n\r\n        \/\/ \u5224\u65ad\u65b0\u6587\u4ef6\u662f\u5426\u5b58\u5728\r\n        File srcFile = rootLocation.toFile();\r\n        \/\/ \u6587\u4ef6\u540d\u79f0 221212.jpg\r\n        String FileName = srcFile.getName();\r\n        \/\/ \u62d3\u5c55\u540d \u5982:jpg\r\n        String fileSufix = FileName.substring(FileName.lastIndexOf(\".\") + 1);\r\n        \/\/ \u62fc\u63a5\u65b0\u5730\u5740    String newFileName = width+\"X\"+height+\"_\"+FileName;\/\/ 150x150_22212.jpg\r\n        String newFileName = Prefix+\"_\"+FileName;\r\n        File destFile = new File(srcFile.getParent(), newFileName);\r\n\r\n        \/\/ \u65b0\u5c3a\u5bf8\u6587\u4ef6\u5b58\u5728\u5219\u76f4\u63a5\u8fd4\u56de\r\n        if(destFile.exists()){\r\n            return  StringUtils.removeEnd(url,FileName)+newFileName;\r\n        }\r\n\r\n        BufferedImage image = ImageIO.read(srcFile);\r\n        int srcWidth = image.getWidth(null);\r\n        int srcHeight = image.getHeight(null);\r\n        int newWidth = 0, newHeight = 0;\r\n        int x = 0, y = 0;\r\n        double scaleW = (double) width \/ srcWidth;\r\n        double scaleH = (double) height \/ srcHeight;\r\n        \/\/ \u6309\u539f\u6bd4\u4f8b\u7f29\u653e\u56fe\u7247\r\n        if (scaleW &lt; scaleH) {\r\n            newHeight = height;\r\n            newWidth = (int) (srcWidth * scaleH);\r\n            x = (newWidth - width) \/ 2;\r\n        } else {\r\n            newHeight = (int) (srcHeight * scaleW);\r\n            newWidth = width;\r\n            y = (newHeight - height) \/ 2;\r\n        }\r\n        BufferedImage newImage = new BufferedImage(newWidth, newHeight,BufferedImage.TYPE_INT_RGB);\r\n        newImage.getGraphics()\r\n                .drawImage(image.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH),0, 0, null);\r\n        \/\/ \u4fdd\u5b58\u88c1\u526a\u540e\u7684\u56fe\u7247\r\n        ImageIO.write(newImage.getSubimage(x, y, width, height), fileSufix,destFile);\r\n        return  StringUtils.removeEnd(url,FileName)+newFileName;\r\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u7aef\u5730\u5740\u66ff\u6362 nginx \u914d\u7f6e\u8f6c\u53d1\u89c4\u5219 \u540e\u7aef\u62c6\u56fe\u4fdd\u5b58\u4ee3\u7801 Java<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[72],"tags":[],"class_list":["post-847","post","type-post","status-publish","format-standard","hentry","category-nginx"],"_links":{"self":[{"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/posts\/847","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/comments?post=847"}],"version-history":[{"count":4,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/posts\/847\/revisions"}],"predecessor-version":[{"id":1113,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/posts\/847\/revisions\/1113"}],"wp:attachment":[{"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/media?parent=847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/categories?post=847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/tags?post=847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}