{"id":16886182,"url":"https://github.com/yanue/nginx-lua-graphicsmagick","last_synced_at":"2025-03-17T06:31:36.499Z","repository":{"id":22699406,"uuid":"26043342","full_name":"yanue/nginx-lua-GraphicsMagick","owner":"yanue","description":"类似淘宝图片,用Lua脚本实现的自定义图片尺寸,动态生成缩略图","archived":false,"fork":false,"pushed_at":"2017-05-03T09:47:58.000Z","size":34,"stargazers_count":174,"open_issues_count":0,"forks_count":66,"subscribers_count":23,"default_branch":"master","last_synced_at":"2025-02-27T19:03:40.764Z","etag":null,"topics":["fastdfs","graphicsmagick","lua","nginx","nginx-lua-graphicsmagick"],"latest_commit_sha":null,"homepage":"http://yanue.net/post-166.html","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yanue.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-11-01T06:09:30.000Z","updated_at":"2024-12-23T02:15:34.000Z","dependencies_parsed_at":"2022-07-27T03:17:22.519Z","dependency_job_id":null,"html_url":"https://github.com/yanue/nginx-lua-GraphicsMagick","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yanue%2Fnginx-lua-GraphicsMagick","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yanue%2Fnginx-lua-GraphicsMagick/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yanue%2Fnginx-lua-GraphicsMagick/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yanue%2Fnginx-lua-GraphicsMagick/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yanue","download_url":"https://codeload.github.com/yanue/nginx-lua-GraphicsMagick/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243847062,"owners_count":20357317,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fastdfs","graphicsmagick","lua","nginx","nginx-lua-graphicsmagick"],"created_at":"2024-10-13T16:38:35.781Z","updated_at":"2025-03-17T06:31:36.155Z","avatar_url":"https://github.com/yanue.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nginx-lua-GraphicsMagick\nNginx+Lua+GraphicsMagick，实现自定义图片尺寸功能，支持两种模式[固定高宽模式,定高或定宽模式]，支持FastDFS文件存储\n\n  github地址:[https://github.com/yanue/nginx-lua-GraphicsMagick](https://github.com/yanue/nginx-lua-GraphicsMagick)\n\n## 说明\n- 类似淘宝图片，实现自定义图片尺寸功能，可根据图片加后缀_100x100.jpg(固定高宽),_-100.jpg(定高),_100-.jpg(定宽)形式实现自定义输出图片大小。\n- 主要将自定义尺寸的图片放在完全独立的thumb目录（自定义目录）,并保持原有的图片目录结构。\n\n## 2016-01-14更新说明\n- 新增定高或定宽裁切模式\n  左右结构,用\"-\"号区分未知高或未知宽(\"-\"号不会被浏览器url转义),如\n  如: xx.jpg_100-.jpg 宽100,高自动\n  如: xx.jpg_-100.jpg 高100,宽自动\n- 新增 php 动态获取图片尺寸的类文件\n\n## 2016-04-22更新说明\n- 新增图片含有 query 参数图片裁剪的支持(做了伪静态跳转)\n  列如: \n  xxx.jpg?a=b\u0026c=d_750x750.jpg 或\n  xxx.jpg?params_750x750.jpg\n  最终跳转为:\n  xxx.jpg_750x750.jpg\n  \n#### 文件夹规划\n```bash\nimg.xxx.com(如/var/www/img)\n|-- img1\n|   `-- 001\n|       `-- 001.jpg\n|-- img2\n|   `-- notfound.jpg\n|-- img3\n|   `-- 001\n|       `-- 001.jpg\n```\n#### 自定义尺寸后的路径\n```bash\nthumb（如/tmp/thumb,可在conf文件里面更改）\n    `-- img1\n        `-- 001\n            |-- 001_200x160.jpg 固定高和宽\n            |-- 001_-100.jpg 定高\n            |-- 001_200-.jpg 定宽\n```\n- 其中img.xxx.com为图片站点根目录，img1,img2...目录是原图目录\n- 缩略图目录根据保持原有结构，并单独设置目录，可定时清理。\n\n#### 链接地址对应关系\n* 原图访问地址：```http://img.xxx.com/xx/001/001.jpg```\n* 缩略图访问地址：```http://img.xxx.com/xx/001/001.jpg_100x100.jpg``` 即为宽100,高100\n* 自动宽地址: ```http://img.xxx.com/xx/001/001.jpg_-100.jpg``` 用\"-\"表示自动,即为高100,宽自动\n* 自动高地址: ```http://img.xxx.com/xx/001/001.jpg_100-.jpg``` 用\"-\"表示自动,即为宽100,高自动\n\n#### 访问流程\n* 首先判断缩略图是否存在，如存在则直接显示缩略图；\n* 缩略图不存在,则判断原图是否存在，如原图存在则拼接graphicsmagick(gm)命令,生成并显示缩略图,否则返回404\n\n## 安装\nCentOS6 安装过程见 [nginx+lua+GraphicsMagick安装](nginx-install.md)\n\n## 配置\n\n### 依赖\n* Nginx\n```bash\n./configure --prefix=/usr/local/nginx \\\n--user=www \\\n--group=www \\\n--sbin-path=/usr/sbin/nginx \\\n--conf-path=/etc/nginx/nginx.conf \\\n--pid-path=/var/run/nginx.pid  \\\n--lock-path=/var/run/nginx.lock \\\n--error-log-path=/opt/logs/nginx/error.log \\\n--http-log-path=/opt/logs/nginx/access.log \\\n--with-http_ssl_module \\\n--with-http_realip_module \\\n--with-http_sub_module \\\n--with-http_flv_module \\\n--with-http_dav_module \\\n--with-http_gzip_static_module \\\n--with-http_stub_status_module \\\n--with-http_addition_module \\\n--with-http_spdy_module \\\n--with-pcre \\\n--with-zlib=../zlib-1.2.8 \\\n--add-module=../nginx-http-concat \\\n--add-module=../lua-nginx-module \\\n--add-module=../ngx_devel_kit \\\n```\n* GraphicsMagick(1.3.18)\n  * libjpeg\n  * libpng\n  * inotify(可选)\n\n### 配置文件说明\n##### nginx 配置文件 /etc/nginx\n##### vhost 为站点配置\n-  [demo.conf](vhost/demo.conf) 为 普通站点 配置文件,包含固定高宽和定高,定宽两种模式配置\n-  [fdfs.conf](vhost/fdfs.conf) 为 fastdfs 配置文件,包含固定高宽和定高,定宽两种模式配置\n##### lua 为裁切图片处理目录\n-  [autoSize.lua](lua/autoSize.lua) 定高或定宽模式裁切图片处理lua脚本\n-  [cropSize.lua](lua/cropSize.lua) 固定高宽模式裁切图片处理lua脚本\n  \n#### nginx vhost demo配置\n```bash\nserver{\n    listen  80\n    \n    # set var for thumb pic\n    set $upload_path /opt/uploads;\n    set $img_original_root  $upload_path;# original root;\n    set $img_thumbnail_root $upload_path/cache/thumb;\n    set $img_file $img_thumbnail_root$uri;\n\n    # like：/xx/xx/xx.jpg_100-.jpg or /xx/xx/xx.jpg_-100.jpg\n    location ~* ^(.+\\.(jpg|jpeg|gif|png))_((\\d+\\-)|(\\-\\d+))\\.(jpg|jpeg|gif|png)$ {\n            root $img_thumbnail_root;    # root path for croped img\n            set $img_size $3;\n\n            if (!-f $img_file) {    # if file not exists\n                    add_header X-Powered-By 'Nginx+Lua+GraphicsMagick By Yanue';  #  header for test\n                    add_header file-path $request_filename;    #  header for test\n                    set $request_filepath $img_original_root$1;    # origin_img full path：/document_root/1.gif\n                    set $img_size $3;    # img width or height size depends on uri\n                    set $img_ext $2;    # file ext\n                    content_by_lua_file /etc/nginx/lua/autoSize.lua;    # load lua\n            }\n    }\n\n    # like: /xx/xx/xx.jpg_100x100.jpg\n    location ~* ^(.+\\.(jpg|jpeg|gif|png))_(\\d+)+x(\\d+)+\\.(jpg|jpeg|gif|png)$ {\n            root $img_thumbnail_root;    # root path for croped img\n\n            if (!-f $img_file) {    # if file not exists\n                    add_header X-Powered-By 'Nginx+Lua+GraphicsMagick By Yanue';  #  header for test\n                    add_header file-path $request_filename;    #  header for test\n                    set $request_filepath $img_original_root$1;    # origin_img file path\n                    set $img_width $3;    # img width\n                    set $img_height $4;    # height\n                    set $img_ext $5;    # file ext\n                    content_by_lua_file /etc/nginx/lua/cropSize.lua;    # load lua\n            }\n    }\n    \n    location = /favicon.ico {\n                log_not_found off;\n                access_log off;\n    }\n}\n```\n\n#### nginx fastdfs配置\n```bash\nserver{\n    listen      80;\n    server_name xxx.com;\n    \n    set $img_thumbnail_root /opt/fastdfs/thumb; #set thumb path\n    set $img_file $img_thumbnail_root$uri;   #thumb file\n\n    # like：/pic/M00/xx/xx/xx.jpg_100-.jpg or /pic/M00/xx/xx/xx.jpg_-100.jpg\n    location ~* ^(\\/(\\w+)(\\/M00)(.+\\.(jpg|jpeg|gif|png)))_((\\d+\\-)|(\\-\\d+))\\.(jpg|jpeg|gif|png)$ {\n            root $img_thumbnail_root;    # root path for croped img\n            set $fdfs_group_root /opt/fastdfs/$2/store0/data; #set fastdfs group path $2\n\n            if (!-f $img_file) {    # if thumb file not exists\n                    add_header X-Powered-By 'Nginx+Lua+GraphicsMagick By Yanue';  #  header for test\n                    add_header file-path $request_filename;    #  header for test\n                    set $request_filepath $fdfs_group_root$4;    # origin_img full path：/document_root/1.gif\n                    set $img_size $6;    # img width or height size depends on uri : img size like \"-100\" or \"100-\", \"-\" means auto size\n                    set $img_ext $5;    # file ext\n                    content_by_lua_file /etc/nginx/lua/autoSize.lua;    # load auto width or height crop Lua file\n            }\n    }\n\n    # like：/pic/M00/xx/xx/xx.jpg_200x100.jpg\n    location ~* ^(\\/(\\w+)(\\/M00)(.+\\.(jpg|jpeg|gif|png))_(\\d+)+x(\\d+)+\\.(jpg|jpeg|gif|png))$ {\n            root $img_thumbnail_root;    # root path for croped img\n            set $fdfs_group_root /opt/fastdfs/$2/store0/data; #set fastdfs group path $2\n\n            if (!-f $img_file) {   # if thumb file not exists\n                    add_header X-Powered-By 'Nginx+Lua+GraphicsMagick By Yanue';  #  header for test\n                    add_header file-path $request_filename;    #  header for test\n                    set $request_filepath $fdfs_group_root$4;    # real file path\n                    set $img_width $6;    #  img width\n                    set $img_height $7;    #  img height\n                    set $img_ext $5;     # file ext\n                    content_by_lua_file /etc/nginx/lua/cropSize.lua;    # load crop Lua file\n            }\n    }\n\n    location /pic/M00 {\n            alias /opt/fastdfs/pic/store0/data;\n            ngx_fastdfs_module;\n    }\n\n    location /chat/M00 {\n            alias /opt/fastdfs/chat/store0/data;\n            ngx_fastdfs_module;\n    }\n\n    location = /favicon.ico {\n            log_not_found off;\n            access_log off;\n    }\n}\n```\n### 最后说明\n- lua 脚本处理并未做任何图片尺寸限制,这样很容易被恶意改变宽和高参数而随意生成大量文件,浪费资源和空间,请根据直接情况自行处理\n\n参考:https://github.com/hopesoft/nginx-lua-image-module\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyanue%2Fnginx-lua-graphicsmagick","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyanue%2Fnginx-lua-graphicsmagick","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyanue%2Fnginx-lua-graphicsmagick/lists"}