mirror of
https://github.com/yanue/nginx-lua-GraphicsMagick.git
synced 2025-11-26 23:35:33 +08:00
add auto height or auto width mode and imgSize.php
This commit is contained in:
200
ImgSize.php
Normal file
200
ImgSize.php
Normal file
@@ -0,0 +1,200 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: mi
|
||||
* Date: 2014/10/30
|
||||
* Time: 14:56
|
||||
*/
|
||||
|
||||
namespace Plugin;
|
||||
|
||||
/**
|
||||
* 动态获取图片尺寸
|
||||
*
|
||||
* 原始图片: http://img.xx.com/chat/M00/00/51/cHxqplZWoMKANLnyAAC290JKUck397.jpg
|
||||
* 按固定宽和高: http://img.xx.com/chat/M00/00/51/cHxqplZWoMKANLnyAAC290JKUck397.jpg_100x200.jpg
|
||||
* 按固定宽: http://img.xx.com/chat/M00/00/51/cHxqplZWoMKANLnyAAC290JKUck397.jpg_200-.jpg
|
||||
* 按固定高: http://img.xx.com/chat/M00/00/51/cHxqplZWoMKANLnyAAC290JKUck397.jpg_-200.jpg
|
||||
*
|
||||
* Class ImgSize
|
||||
* @package Plugin
|
||||
*/
|
||||
class ImgSize
|
||||
{
|
||||
private static $trust_hosts = array(
|
||||
null, // 保留null
|
||||
STATIC_DOMAIN,
|
||||
MAIN_DOMAIN,
|
||||
"fdfs.estt.com.cn"
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* 获取原始图片
|
||||
*
|
||||
* @param $img_url
|
||||
* @return string
|
||||
*/
|
||||
public static function getOriginal($img_url)
|
||||
{
|
||||
$img_ext = pathinfo($img_url, PATHINFO_EXTENSION);
|
||||
$img_path = self::getOriginalWithoutExt($img_url);
|
||||
if ($img_url) {
|
||||
return $img_path . '.' . $img_ext;
|
||||
}
|
||||
return $img_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换内容图片为自动高
|
||||
*
|
||||
* @param $detail
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getWapDetailPicReplace($detail)
|
||||
{
|
||||
preg_match_all('/<[img|IMG].*?src=[\'|\"](.*?(?:))[\'|\"].*?[\/]?>/', $detail, $matches);
|
||||
if (isset($matches[1])) {
|
||||
$matches[1] = array_unique($matches[1]);
|
||||
foreach ($matches[1] as $item) {
|
||||
$detail = str_replace($item, self::getAutoHeight($item), $detail);
|
||||
}
|
||||
}
|
||||
return $detail;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取自动高
|
||||
*
|
||||
* @param $img_url
|
||||
* @param int $width
|
||||
* @return string
|
||||
*/
|
||||
public static function getAutoHeight($img_url, $width = 800)
|
||||
{
|
||||
$width = intval($width * 1.5);
|
||||
|
||||
if ($img_url) {
|
||||
// 允许的域名
|
||||
$img_host = parse_url($img_url, PHP_URL_HOST);
|
||||
if (!($img_host && in_array($img_host, self::$trust_hosts))) {
|
||||
return $img_url;
|
||||
}
|
||||
|
||||
// 获取原始图片未带后缀
|
||||
$originalImgPath = self::getOriginalWithoutExt($img_url);
|
||||
if (!$originalImgPath) {
|
||||
return $img_url;
|
||||
}
|
||||
$img_ext = pathinfo($img_url, PATHINFO_EXTENSION);
|
||||
|
||||
if ($width) {
|
||||
return $originalImgPath . '.' . $img_ext . '_' . $width . '-.' . $img_ext;
|
||||
}
|
||||
}
|
||||
|
||||
return $img_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取自动宽
|
||||
*
|
||||
* @param $img_url
|
||||
* @param int $height
|
||||
* @return string
|
||||
*/
|
||||
public static function getAutoWidth($img_url, $height = 200)
|
||||
{
|
||||
$height = intval($height * 1.5);
|
||||
|
||||
if ($img_url) {
|
||||
|
||||
// 允许的域名
|
||||
$img_host = parse_url($img_url, PHP_URL_HOST);
|
||||
if (!($img_host && in_array($img_host, self::$trust_hosts))) {
|
||||
return $img_url;
|
||||
}
|
||||
|
||||
// 获取原始图片未带后缀
|
||||
$originalImgPath = self::getOriginalWithoutExt($img_url);
|
||||
if (!$originalImgPath) {
|
||||
return $img_url;
|
||||
}
|
||||
$img_ext = pathinfo($img_url, PATHINFO_EXTENSION);
|
||||
|
||||
if ($height) {
|
||||
return $originalImgPath . '.' . $img_ext . '_' . '-' . $height . '.' . $img_ext;
|
||||
}
|
||||
}
|
||||
|
||||
return $img_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按固定尺寸获取图片
|
||||
*
|
||||
* @param $img_url
|
||||
* @param int $w
|
||||
* @param null $h
|
||||
* @return string
|
||||
*/
|
||||
public static function getSize($img_url, $w = 200, $h = null)
|
||||
{
|
||||
if ($w < 800) {
|
||||
$w = intval($w * 1.5);
|
||||
$h = intval($h * 1.5);
|
||||
}
|
||||
|
||||
if ($img_url) {
|
||||
// 允许的域名
|
||||
|
||||
$img_host = parse_url($img_url, PHP_URL_HOST);
|
||||
if (!(in_array($img_host, self::$trust_hosts))) {
|
||||
return $img_url;
|
||||
}
|
||||
// 获取原始图片未带后缀
|
||||
$originalImgPath = self::getOriginalWithoutExt($img_url);
|
||||
if (!$originalImgPath) {
|
||||
return $img_url;
|
||||
}
|
||||
$img_ext = pathinfo($img_url, PATHINFO_EXTENSION);
|
||||
// 长宽检测
|
||||
if ($w && $h) {
|
||||
return $originalImgPath . '.' . $img_ext . '_' . $w . 'x' . $h . '.' . $img_ext;
|
||||
}
|
||||
|
||||
if ($w && !$h) {
|
||||
return $originalImgPath . '.' . $img_ext . '_' . $w . 'x' . $w . '.' . $img_ext;
|
||||
}
|
||||
|
||||
if (!$w && $h) {
|
||||
return $originalImgPath . '.' . $img_ext . '_' . $h . 'x' . $h . '.' . $img_ext;
|
||||
}
|
||||
|
||||
if (!$w && !$h) {
|
||||
return $originalImgPath . '.' . $img_ext;
|
||||
}
|
||||
}
|
||||
|
||||
return $img_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取原始图片未带后缀
|
||||
*
|
||||
* @param $img_url
|
||||
* @return string
|
||||
*/
|
||||
private static function getOriginalWithoutExt($img_url)
|
||||
{
|
||||
if ($img_url) {
|
||||
$img_ext = pathinfo($img_url, PATHINFO_EXTENSION);
|
||||
// like /avatar/M00/00/01/wKgBqFJyG2iAfHD0AAAxCAGuAII166.jpg_430x430.jpg
|
||||
if (preg_match('/^(.*)\.' . $img_ext . '(_([0-9]+)x([0-9]+)\.' . $img_ext . ')?$/is', $img_url, $match)) {
|
||||
return $match[1];
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
258
README.md
258
README.md
@@ -2,8 +2,15 @@
|
||||
Nginx+Lua+GraphicsMagick,实现自定义图片尺寸功能,支持FastDFS文件存储
|
||||
|
||||
## 说明
|
||||
类似淘宝图片,实现自定义图片尺寸功能,可根据图片加后缀_100x100.jpg形式实现自定义输出图片大小。
|
||||
主要将自定义尺寸的图片放在完全独立的thumb目录(自定义目录),并保持原有的图片目录结构。
|
||||
- 类似淘宝图片,实现自定义图片尺寸功能,可根据图片加后缀_100x100.jpg形式实现自定义输出图片大小。
|
||||
- 主要将自定义尺寸的图片放在完全独立的thumb目录(自定义目录),并保持原有的图片目录结构。
|
||||
|
||||
## 2016-01-14更新说明
|
||||
- 新增定高或定宽裁切模式
|
||||
左右结构,用"-"号区分未知高或未知宽("-"号不会被浏览器url转义),如
|
||||
如: xx.jpg_100-.jpg 宽100,高自动
|
||||
如: xx.jpg_-100.jpg 高100,宽自动
|
||||
- 新增 php 动态获取图片尺寸的类文件
|
||||
|
||||
#### 文件夹规划
|
||||
```bash
|
||||
@@ -22,70 +29,113 @@ img.xxx.com(如/var/www/img)
|
||||
thumb(如/tmp/thumb,可在conf文件里面更改)
|
||||
`-- img1
|
||||
`-- 001
|
||||
|-- 001_100x100.jpg
|
||||
|-- 001_200x160.jpg
|
||||
|-- 001_200x160.jpg 固定高和宽
|
||||
|-- 001_-100.jpg 定高
|
||||
|-- 001_200-.jpg 定宽
|
||||
```
|
||||
其中img.xxx.com为图片站点根目录,img1,img2...目录是原图目录,可根据目录设置不同的缩略图尺寸,thumb文件夹用来存放缩略图,可定时清理。
|
||||
- 其中img.xxx.com为图片站点根目录,img1,img2...目录是原图目录,可根据目录设置不同的缩略图尺寸,
|
||||
- thumb文件夹用来存放缩略图,可定时清理。
|
||||
|
||||
#### 链接地址对应关系
|
||||
* 原图访问地址:```http://img.xxx.com/xx/001/001.jpg```
|
||||
* 缩略图访问地址:```http://img.xxx.com/xx/001/001.jpg_100x100.jpg```
|
||||
* 缩略图访问地址:```http://img.xxx.com/xx/001/001.jpg_100x100.jpg``` 即为宽100,高100
|
||||
* 自动宽地址: ```http://img.xxx.com/xx/001/001.jpg_-100.jpg``` 用-表示自动,即为高100,宽自动
|
||||
* 自动高地址: ```http://img.xxx.com/xx/001/001.jpg_100-.jpg``` 用-表示自动,即为宽100,高自动
|
||||
|
||||
#### 访问流程
|
||||
* 首先判断缩略图是否存在,如存在则直接显示缩略图;
|
||||
* 缩略图不存在,则判断原图是否存在,如原图存在则拼接graphicsmagick命令,生成并显示缩略图,否则返回404
|
||||
|
||||
## 安装
|
||||
CentOS6 安装过程见 [nginx+lua+GraphicsMagick安装](nginx-install.md)
|
||||
|
||||
## 配置
|
||||
|
||||
|
||||
## 依赖
|
||||
### 依赖
|
||||
* Nginx
|
||||
```bash
|
||||
./configure --prefix=/usr/local/nginx --user=www --group=www --pid-path=/opt/logs/nginx/nginx.pid --lock-path=/opt/logs/nginx/nginx.lock --error-log-path=/opt/logs/nginx/error.log --http-log-path=/opt/logs/nginx/access.log --with-http_ssl_module --with-http_realip_module --with-http_sub_module --with-http_flv_module --with-http_dav_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_addition_module --with-zlib=../zlib-1.2.8 --with-pcre=../pcre-8.36 --add-module=../nginx-http-concat --add-module=../lua-nginx-module/ --add-module=../echo-nginx-module/ --add-module=../ngx_devel_kit/
|
||||
./configure --prefix=/usr/local/nginx \
|
||||
--user=www \
|
||||
--group=www \
|
||||
--sbin-path=/usr/sbin/nginx \
|
||||
--conf-path=/etc/nginx/nginx.conf \
|
||||
--pid-path=/var/run/nginx.pid \
|
||||
--lock-path=/var/run/nginx.lock \
|
||||
--error-log-path=/opt/logs/nginx/error.log \
|
||||
--http-log-path=/opt/logs/nginx/access.log \
|
||||
--with-http_ssl_module \
|
||||
--with-http_realip_module \
|
||||
--with-http_sub_module \
|
||||
--with-http_flv_module \
|
||||
--with-http_dav_module \
|
||||
--with-http_gzip_static_module \
|
||||
--with-http_stub_status_module \
|
||||
--with-http_addition_module \
|
||||
--with-http_spdy_module \
|
||||
--with-pcre \
|
||||
--with-zlib=../zlib-1.2.8 \
|
||||
--add-module=../nginx-http-concat \
|
||||
--add-module=../lua-nginx-module \
|
||||
--add-module=../ngx_devel_kit \
|
||||
```
|
||||
* GraphicsMagick(1.3.18)
|
||||
* libjpeg
|
||||
* libpng
|
||||
* inotify(可选)
|
||||
* inotify(可选)
|
||||
|
||||
## 安装
|
||||
|
||||
#### nginx vhost default配置
|
||||
### 配置文件说明
|
||||
* nginx 配置文件 /etc/nginx
|
||||
* vhost 为站点配置
|
||||
- [demo.conf](vhost/demo.conf) 为 普通站点 配置文件,包含固定高宽和定高,定宽两种模式配置
|
||||
- [fdfs.conf](vhost/fdfs.conf) 为 fastdfs 配置文件,包含固定高宽和定高,定宽两种模式配置
|
||||
* lua 为裁切图片处理目录
|
||||
- [autoSize.lua](lua/autoSize.lua) 定高或定宽模式裁切图片处理lua脚本
|
||||
- [cropSize.lua](lua/cropSize.lua) 固定高宽模式裁切图片处理lua脚本
|
||||
|
||||
#### nginx vhost demo配置
|
||||
```bash
|
||||
server {
|
||||
listen 80;
|
||||
index index.php index.html index.htm;
|
||||
server{
|
||||
listen 80
|
||||
|
||||
# set var for thumb pic
|
||||
set $upload_path /opt/uploads;
|
||||
set $img_original_root $upload_path;# original root;
|
||||
set $img_thumbnail_root $upload_path/cache/thumb;
|
||||
set $img_file $img_thumbnail_root$uri;
|
||||
|
||||
set $root_path '/var/www';
|
||||
root $root_path;
|
||||
# like:/xx/xx/xx.jpg_100-.jpg or /xx/xx/xx.jpg_-100.jpg
|
||||
location ~* ^(.+\.(jpg|jpeg|gif|png))_((\d+\-)|(\-\d+))\.(jpg|jpeg|gif|png)$ {
|
||||
root $img_thumbnail_root; # root path for croped img
|
||||
set $img_size $3;
|
||||
|
||||
|
||||
location / {
|
||||
index index.html index.htm;
|
||||
}
|
||||
|
||||
location /lua {
|
||||
default_type 'text/plain';
|
||||
content_by_lua 'ngx.say("hello, ttlsa lua")';
|
||||
if (!-f $img_file) { # if file not exists
|
||||
add_header X-Powered-By 'Nginx+Lua+GraphicsMagick By Yanue'; # header for test
|
||||
add_header file-path $request_filename; # header for test
|
||||
set $request_filepath $img_original_root$1; # origin_img full path:/document_root/1.gif
|
||||
set $img_size $3; # img width or height size depends on uri
|
||||
set $img_ext $2; # file ext
|
||||
content_by_lua_file /etc/nginx/lua/autoSize.lua; # load lua
|
||||
}
|
||||
}
|
||||
|
||||
# like: /xx/xx/xx.jpg_100x100.jpg
|
||||
location ~* ^(.+\.(jpg|jpeg|gif|png))_(\d+)+x(\d+)+\.(jpg|jpeg|gif|png)$ {
|
||||
root /tmp/thumb; # 这里必须设置,否则根目录,即 $document_root 会是 Nginx 默认的 Nginx Root/html,在 Lua 中会得不到期望的值
|
||||
set $thumbnail_root /tmp/thumb;
|
||||
set $img_original_root $root_path;
|
||||
set $file $thumbnail_root$uri; #如果缩略图文件存在,直接返回
|
||||
root $img_thumbnail_root; # root path for croped img
|
||||
|
||||
if (!-f $file) { # 如果文件不存在时才需要裁剪
|
||||
set $request_filepath $img_original_root$1; # 设置原始图片路径,如:/document_root/1.gif
|
||||
set $img_width $3; # 设置裁剪/缩放的宽度
|
||||
set $img_height $4; # 设置裁剪/缩放的高度
|
||||
set $img_ext $2; # 图片文件格式后缀
|
||||
content_by_lua_file /etc/nginx/lua/img.lua; # 加载外部 Lua 文件
|
||||
}
|
||||
if (!-f $img_file) { # if file not exists
|
||||
add_header X-Powered-By 'Nginx+Lua+GraphicsMagick By Yanue'; # header for test
|
||||
add_header file-path $request_filename; # header for test
|
||||
set $request_filepath $img_original_root$1; # origin_img file path
|
||||
set $img_width $3; # img width
|
||||
set $img_height $4; # height
|
||||
set $img_ext $5; # file ext
|
||||
content_by_lua_file /etc/nginx/lua/cropSize.lua; # load lua
|
||||
}
|
||||
}
|
||||
|
||||
location ~ /\.ht {
|
||||
deny all;
|
||||
|
||||
location = /favicon.ico {
|
||||
log_not_found off;
|
||||
access_log off;
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -94,111 +144,59 @@ server {
|
||||
```bash
|
||||
server{
|
||||
listen 80;
|
||||
server_name static.saleasy.net static.isaleasy.com static.estt.com.cn;
|
||||
server_name xxx.com;
|
||||
|
||||
set $img_thumbnail_root /opt/fastdfs/thumb; #set thumb path
|
||||
set $img_file $img_thumbnail_root$uri; #thumb file
|
||||
|
||||
# 缩放图片链接
|
||||
location ~* ^(\/pic\/M00(.+\.(jpg|jpeg|gif|png))_(\d+)+x(\d+)+\.(jpg|jpeg|gif|png))$ {
|
||||
root /opt/fastdfs/thumb; # 这里必须设置,否则根目录,即 $document_root 会是 Nginx 默认的 Nginx Root/html,在 Lua 中会得不到期望的值
|
||||
set $thumbnail_root /opt/fastdfs/thumb;
|
||||
set $fdfs_group_root /opt/fastdfs/pic/store0/data;
|
||||
set $file $thumbnail_root$uri;
|
||||
# like:/pic/M00/xx/xx/xx.jpg_100-.jpg or /pic/M00/xx/xx/xx.jpg_-100.jpg
|
||||
location ~* ^(\/(\w+)(\/M00)(.+\.(jpg|jpeg|gif|png)))_((\d+\-)|(\-\d+))\.(jpg|jpeg|gif|png)$ {
|
||||
root $img_thumbnail_root; # root path for croped img
|
||||
set $fdfs_group_root /opt/fastdfs/$2/store0/data; #set fastdfs group path $2
|
||||
|
||||
if (!-f $file) { # 如果文件不存在时才需要裁剪
|
||||
set $request_filepath $fdfs_group_root$2; # 设置原始图片路径,如:/document_root/1.gif
|
||||
set $img_width $4; # 设置裁剪/缩放的宽度
|
||||
set $img_height $5; # 设置裁剪/缩放的高度
|
||||
set $img_ext $3; # 图片文件格式后缀
|
||||
content_by_lua_file /etc/nginx/lua/img.lua; # 加载外部 Lua 文件
|
||||
if (!-f $img_file) { # if thumb file not exists
|
||||
add_header X-Powered-By 'Nginx+Lua+GraphicsMagick By Yanue'; # header for test
|
||||
add_header file-path $request_filename; # header for test
|
||||
set $request_filepath $fdfs_group_root$4; # origin_img full path:/document_root/1.gif
|
||||
set $img_size $6; # img width or height size depends on uri : img size like "-100" or "100-", "-" means auto size
|
||||
set $img_ext $5; # file ext
|
||||
content_by_lua_file /etc/nginx/lua/autoSize.lua; # load auto width or height crop Lua file
|
||||
}
|
||||
}
|
||||
|
||||
# like:/pic/M00/xx/xx/xx.jpg_200x100.jpg
|
||||
location ~* ^(\/(\w+)(\/M00)(.+\.(jpg|jpeg|gif|png))_(\d+)+x(\d+)+\.(jpg|jpeg|gif|png))$ {
|
||||
root $img_thumbnail_root; # root path for croped img
|
||||
set $fdfs_group_root /opt/fastdfs/$2/store0/data; #set fastdfs group path $2
|
||||
|
||||
if (!-f $img_file) { # if thumb file not exists
|
||||
add_header X-Powered-By 'Nginx+Lua+GraphicsMagick By Yanue'; # header for test
|
||||
add_header file-path $request_filename; # header for test
|
||||
set $request_filepath $fdfs_group_root$4; # real file path
|
||||
set $img_width $6; # img width
|
||||
set $img_height $7; # img height
|
||||
set $img_ext $5; # file ext
|
||||
content_by_lua_file /etc/nginx/lua/cropSize.lua; # load crop Lua file
|
||||
}
|
||||
}
|
||||
|
||||
# 默认图片
|
||||
location /pic/M00 {
|
||||
alias /opt/fastdfs/pic/store0/data;
|
||||
ngx_fastdfs_module;
|
||||
}
|
||||
|
||||
location /chat/M00 {
|
||||
alias /opt/fastdfs/chat/store0/data;
|
||||
ngx_fastdfs_module;
|
||||
}
|
||||
|
||||
location = /favicon.ico {
|
||||
log_not_found off;
|
||||
access_log off;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### img.lua文件
|
||||
```bash
|
||||
-- nginx lua thumbnail module
|
||||
-- created by yanue
|
||||
-- last update : 2014/11/3
|
||||
-- version : 0.5.1
|
||||
|
||||
-- 检测路径是否目录
|
||||
local function is_dir(sPath)
|
||||
if type(sPath) ~= "string" then return false end
|
||||
|
||||
local response = os.execute("cd " .. sPath)
|
||||
if response == 0 then
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
-- 文件是否存在
|
||||
function file_exists(name)
|
||||
local f = io.open(name, "r")
|
||||
if f ~= nil then io.close(f) return true else return false end
|
||||
end
|
||||
|
||||
-- 获取文件路径
|
||||
function getFileDir(filename)
|
||||
return string.match(filename, "(.+)/[^/]*%.%w+$") --*nix system
|
||||
end
|
||||
|
||||
-- 获取文件名
|
||||
function strippath(filename)
|
||||
return string.match(filename, ".+/([^/]*%.%w+)$") -- *nix system
|
||||
end
|
||||
|
||||
--去除扩展名
|
||||
function stripextension(filename)
|
||||
local idx = filename:match(".+()%.%w+$")
|
||||
if (idx) then
|
||||
return filename:sub(1, idx - 1)
|
||||
else
|
||||
return filename
|
||||
end
|
||||
end
|
||||
|
||||
--获取扩展名
|
||||
function getExtension(filename)
|
||||
return filename:match(".+%.(%w+)$")
|
||||
end
|
||||
|
||||
-- 开始执行
|
||||
-- ngx.log(ngx.ERR, getFileDir(ngx.var.file));
|
||||
-- ngx.log(ngx.ERR,ngx.var.file);
|
||||
-- ngx.log(ngx.ERR,ngx.var.request_filepath);
|
||||
|
||||
local gm_path = 'gm'
|
||||
|
||||
-- check image dir
|
||||
if not is_dir(getFileDir(ngx.var.file)) then
|
||||
os.execute("mkdir -p " .. getFileDir(ngx.var.file))
|
||||
end
|
||||
|
||||
-- 裁剪后保证等比缩图 (缺点:裁剪了图片的一部分)
|
||||
-- 命令:gm convert input.jpg -thumbnail "100x100^" -gravity center -extent 100x100 output_3.jpg
|
||||
if (file_exists(ngx.var.request_filepath)) then
|
||||
local cmd = gm_path .. ' convert ' .. ngx.var.request_filepath
|
||||
cmd = cmd .. " -thumbnail " .. ngx.var.img_width .. "x" .. ngx.var.img_height .. "^"
|
||||
cmd = cmd .. " -gravity center -extent " .. ngx.var.img_width .. "x" .. ngx.var.img_height
|
||||
cmd = cmd .. " +profile \"*\" " .. ngx.var.file;
|
||||
ngx.log(ngx.ERR, cmd);
|
||||
os.execute(cmd);
|
||||
ngx.exec(ngx.var.uri);
|
||||
else
|
||||
ngx.exit(ngx.HTTP_NOT_FOUND);
|
||||
end
|
||||
```
|
||||
### 最后说明
|
||||
lua 脚本处理并未做任何图片尺寸限制,这样很容易被恶意改变宽和高参数而随意生成大量文件,浪费资源和空间,请根据直接情况自行处理
|
||||
|
||||
参考:https://github.com/hopesoft/nginx-lua-image-module
|
||||
93
lua/autoSize.lua
Normal file
93
lua/autoSize.lua
Normal file
@@ -0,0 +1,93 @@
|
||||
-- 根据输入长或宽的尺寸自动裁切图片大小
|
||||
|
||||
-- 检测路径是否目录
|
||||
local function is_dir(sPath)
|
||||
if type(sPath) ~= "string" then return false end
|
||||
|
||||
local response = os.execute("cd " .. sPath)
|
||||
if response == 0 then
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
-- 文件是否存在
|
||||
function file_exists(name)
|
||||
local f = io.open(name, "r")
|
||||
if f ~= nil then io.close(f) return true else return false end
|
||||
end
|
||||
|
||||
-- 获取文件路径
|
||||
function getFileDir(filename)
|
||||
return string.match(filename, "(.+)/[^/]*%.%w+$") --*nix system
|
||||
end
|
||||
|
||||
-- 获取文件名
|
||||
function strippath(filename)
|
||||
return string.match(filename, ".+/([^/]*%.%w+)$") -- *nix system
|
||||
end
|
||||
|
||||
--去除扩展名
|
||||
function stripextension(filename)
|
||||
local idx = filename:match(".+()%.%w+$")
|
||||
if (idx) then
|
||||
return filename:sub(1, idx - 1)
|
||||
else
|
||||
return filename
|
||||
end
|
||||
end
|
||||
|
||||
--获取扩展名
|
||||
function getExtension(filename)
|
||||
return filename:match(".+%.(%w+)$")
|
||||
end
|
||||
|
||||
function getImgSize(img)
|
||||
|
||||
end
|
||||
|
||||
-- 开始执行
|
||||
-- ngx.log(ngx.ERR, getFileDir(ngx.var.img_file));
|
||||
|
||||
local gm_path = 'gm'
|
||||
|
||||
-- check image dir
|
||||
if not is_dir(getFileDir(ngx.var.img_file)) then
|
||||
os.execute("mkdir -p " .. getFileDir(ngx.var.img_file))
|
||||
end
|
||||
|
||||
-- 获取高宽 100!或!100模式
|
||||
local uri = ngx.var.img_size
|
||||
local width = string.sub(uri,1,1)
|
||||
local height = 0
|
||||
|
||||
if width == "-" then
|
||||
width = 0
|
||||
height = string.sub(uri,2,string.len(uri))
|
||||
else
|
||||
width = string.sub(uri,1,string.len(uri)-1)
|
||||
height = 0
|
||||
end
|
||||
ngx.log(ngx.ERR,uri)
|
||||
ngx.log(ngx.ERR,width)
|
||||
ngx.log(ngx.ERR,height)
|
||||
ngx.log(ngx.ERR,ngx.var.img_file);
|
||||
ngx.log(ngx.ERR,ngx.var.request_filepath);
|
||||
-- 裁剪后保证等比缩图 (缺点:裁剪了图片的一部分)
|
||||
-- gm convert input.jpg -thumbnail "100x100^" -gravity center -extent 100x100 output_3.jpg
|
||||
if (file_exists(ngx.var.request_filepath)) then
|
||||
local cmd = gm_path .. ' convert ' .. ngx.var.request_filepath
|
||||
if height == 0 then
|
||||
cmd = cmd .. " -resize " .. width .. "x" .. ""
|
||||
else
|
||||
cmd = cmd .. " -resize " .. "x" .. height .. ""
|
||||
end
|
||||
|
||||
cmd = cmd .. " +profile \"*\" " .. ngx.var.img_file;
|
||||
ngx.log(ngx.ERR, cmd);
|
||||
os.execute(cmd);
|
||||
ngx.exec(ngx.var.uri);
|
||||
else
|
||||
ngx.exit(ngx.HTTP_NOT_FOUND);
|
||||
end
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
-- nginx lua thumbnail module
|
||||
-- created by yanue
|
||||
-- last update : 2014/11/3
|
||||
-- version : 0.5.1
|
||||
-- 根据输入长和宽的尺寸裁切图片
|
||||
|
||||
-- 检测路径是否目录
|
||||
local function is_dir(sPath)
|
||||
@@ -46,25 +43,26 @@ function getExtension(filename)
|
||||
end
|
||||
|
||||
-- 开始执行
|
||||
-- ngx.log(ngx.ERR, getFileDir(ngx.var.file));
|
||||
-- ngx.log(ngx.ERR,ngx.var.file);
|
||||
-- ngx.log(ngx.ERR,ngx.var.request_filepath);
|
||||
-- ngx.log(ngx.ERR, getFileDir(ngx.var.img_file));
|
||||
|
||||
local gm_path = 'gm'
|
||||
|
||||
-- check image dir
|
||||
if not is_dir(getFileDir(ngx.var.file)) then
|
||||
os.execute("mkdir -p " .. getFileDir(ngx.var.file))
|
||||
if not is_dir(getFileDir(ngx.var.img_file)) then
|
||||
os.execute("mkdir -p " .. getFileDir(ngx.var.img_file))
|
||||
end
|
||||
|
||||
ngx.log(ngx.ERR,ngx.var.img_file);
|
||||
ngx.log(ngx.ERR,ngx.var.request_filepath);
|
||||
-- 裁剪后保证等比缩图 (缺点:裁剪了图片的一部分)
|
||||
-- 命令:gm convert input.jpg -thumbnail "100x100^" -gravity center -extent 100x100 output_3.jpg
|
||||
-- gm convert input.jpg -thumbnail "100x100^" -gravity center -extent 100x100 output_3.jpg
|
||||
if (file_exists(ngx.var.request_filepath)) then
|
||||
local cmd = gm_path .. ' convert ' .. ngx.var.request_filepath
|
||||
cmd = cmd .. " -thumbnail " .. ngx.var.img_width .. "x" .. ngx.var.img_height .. "^"
|
||||
cmd = cmd .. " -gravity center -extent " .. ngx.var.img_width .. "x" .. ngx.var.img_height
|
||||
cmd = cmd .. " +profile \"*\" " .. ngx.var.file;
|
||||
ngx.log(ngx.ERR, cmd);
|
||||
-- cmd = cmd .. " -quality 100"
|
||||
cmd = cmd .. " +profile \"*\" " .. ngx.var.img_file;
|
||||
-- ngx.log(ngx.ERR, cmd);
|
||||
os.execute(cmd);
|
||||
ngx.exec(ngx.var.uri);
|
||||
else
|
||||
86
nginx-install.md
Normal file
86
nginx-install.md
Normal file
@@ -0,0 +1,86 @@
|
||||
nginx install
|
||||
============
|
||||
0. before ready
|
||||
---------------
|
||||
groupadd www
|
||||
useradd -g www www -s /bin/false
|
||||
|
||||
yum install -y gcc gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel
|
||||
yum install -y libpng libjpeg libpng-devel libjpeg-devel ghostscript libtiff libtiff-devel freetype freetype-devel
|
||||
|
||||
1. download software
|
||||
--------------------
|
||||
/usr/local/src
|
||||
### base download
|
||||
wget http://nginx.org/download/nginx-1.8.0.tar.gz
|
||||
wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz ### LuaJIT
|
||||
wget ftp://ftp.graphicsmagick.org/pub/GraphicsMagick/1.3/GraphicsMagick-1.3.21.tar.gz ### GraphicsMagick
|
||||
wget http://zlib.net/zlib-1.2.8.tar.gz
|
||||
|
||||
### nginx module
|
||||
git clone https://github.com/alibaba/nginx-http-concat.git
|
||||
git clone https://github.com/simpl/ngx_devel_kit.git
|
||||
git clone https://github.com/openresty/echo-nginx-module.git
|
||||
git clone https://github.com/openresty/lua-nginx-module.git
|
||||
git clone https://github.com/happyfish100/fastdfs-nginx-module.git
|
||||
|
||||
2. unzip and install depends
|
||||
------------------------------
|
||||
|
||||
#### 2.0 unzip
|
||||
tar -zxf nginx-1.8.0.tar.gz
|
||||
tar -zxf LuaJIT-2.0.4.tar.gz
|
||||
tar -zxf GraphicsMagick-1.3.21.tar.gz
|
||||
tar -zxf zlib-1.2.8.tar.gz
|
||||
|
||||
#### 2.1 install LuaJIT
|
||||
cd LuaJIT-2.0.4
|
||||
./configure --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB"
|
||||
make -j8
|
||||
make install
|
||||
export LUAJIT_LIB=/usr/local/lib
|
||||
export LUAJIT_INC=/usr/local/include/luajit-2.0
|
||||
ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
|
||||
cd ..
|
||||
|
||||
#### 2.2 install GraphicsMagick
|
||||
cd GraphicsMagick-1.3.21
|
||||
./configure --enable-shared --with-jpeg=yes --with-png=yes
|
||||
make -j8
|
||||
make install
|
||||
cd ..
|
||||
|
||||
#### 2.3 install nginx
|
||||
cd nginx-1.8.0
|
||||
./configure --prefix=/usr/local/nginx \
|
||||
--user=www \
|
||||
--group=www \
|
||||
--sbin-path=/usr/sbin/nginx \
|
||||
--conf-path=/etc/nginx/nginx.conf \
|
||||
--pid-path=/var/run/nginx.pid \
|
||||
--lock-path=/var/run/nginx.lock \
|
||||
--error-log-path=/opt/logs/nginx/error.log \
|
||||
--http-log-path=/opt/logs/nginx/access.log \
|
||||
--with-http_ssl_module \
|
||||
--with-http_realip_module \
|
||||
--with-http_sub_module \
|
||||
--with-http_flv_module \
|
||||
--with-http_dav_module \
|
||||
--with-http_gzip_static_module \
|
||||
--with-http_stub_status_module \
|
||||
--with-http_addition_module \
|
||||
--with-http_spdy_module \
|
||||
--with-pcre \
|
||||
--with-zlib=../zlib-1.2.8 \
|
||||
--add-module=../nginx-http-concat \
|
||||
--add-module=../lua-nginx-module \
|
||||
--add-module=../ngx_devel_kit \
|
||||
|
||||
如果安装了 fastdfs
|
||||
--add-module=../fastdfs-nginx-module/src
|
||||
|
||||
make -j8
|
||||
make install
|
||||
|
||||
|
||||
|
||||
@@ -15,21 +15,48 @@ server {
|
||||
content_by_lua 'ngx.say("hello, ttlsa lua")';
|
||||
}
|
||||
|
||||
location ~* ^(.+\.(jpg|jpeg|gif|png))_(\d+)+x(\d+)+\.(jpg|jpeg|gif|png)$ {
|
||||
root /tmp/thumb; # 这里必须设置,否则根目录,即 $document_root 会是 Nginx 默认的 Nginx Root/html,在 Lua 中会得不到期望的值
|
||||
set $thumbnail_root /tmp/thumb;
|
||||
set $img_original_root $root_path;
|
||||
set $file $thumbnail_root$uri; #如果缩略图文件存在,直接返回
|
||||
# set var for thumb pic
|
||||
set $upload_path /opt/uploads;
|
||||
set $img_original_root $upload_path;# original root;
|
||||
set $img_thumbnail_root $upload_path/cache/thumb;
|
||||
set $img_file $img_thumbnail_root$uri;
|
||||
|
||||
if (!-f $file) { # 如果文件不存在时才需要裁剪
|
||||
set $request_filepath $img_original_root$1; # 设置原始图片路径,如:/document_root/1.gif
|
||||
set $img_width $3; # 设置裁剪/缩放的宽度
|
||||
set $img_height $4; # 设置裁剪/缩放的高度
|
||||
set $img_ext $2; # 图片文件格式后缀
|
||||
content_by_lua_file /etc/nginx/lua/img.lua; # 加载外部 Lua 文件
|
||||
}
|
||||
# like:/xx/xx/xx.jpg_100-.jpg or /xx/xx/xx.jpg_-100.jpg
|
||||
location ~* ^(.+\.(jpg|jpeg|gif|png))_((\d+\-)|(\-\d+))\.(jpg|jpeg|gif|png)$ {
|
||||
root $img_thumbnail_root; # root path for croped img
|
||||
set $img_size $3;
|
||||
|
||||
if (!-f $img_file) { # if file not exists
|
||||
add_header X-Powered-By 'Nginx+Lua+GraphicsMagick By Yanue'; # header for test
|
||||
add_header file-path $request_filename; # header for test
|
||||
set $request_filepath $img_original_root$1; # origin_img full path:/document_root/1.gif
|
||||
set $img_size $3; # img width or height size depends on uri
|
||||
set $img_ext $2; # file ext
|
||||
content_by_lua_file /etc/nginx/lua/autoSize.lua; # load lua
|
||||
}
|
||||
}
|
||||
|
||||
# like: /xx/xx/xx.jpg_100x100.jpg
|
||||
location ~* ^(.+\.(jpg|jpeg|gif|png))_(\d+)+x(\d+)+\.(jpg|jpeg|gif|png)$ {
|
||||
root $img_thumbnail_root; # root path for croped img
|
||||
|
||||
if (!-f $img_file) { # if file not exists
|
||||
add_header X-Powered-By 'Nginx+Lua+GraphicsMagick By Yanue'; # header for test
|
||||
add_header file-path $request_filename; # header for test
|
||||
set $request_filepath $img_original_root$1; # origin_img file path
|
||||
set $img_width $3; # img width
|
||||
set $img_height $4; # height
|
||||
set $img_ext $5; # file ext
|
||||
content_by_lua_file /etc/nginx/lua/cropSize.lua; # load lua
|
||||
}
|
||||
}
|
||||
|
||||
# if need (all go there)
|
||||
location ^~ /uploads {
|
||||
root $img_original_root;
|
||||
}
|
||||
|
||||
|
||||
location ~ /\.ht {
|
||||
deny all;
|
||||
}
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
server{
|
||||
listen 80;
|
||||
server_name static.saleasy.net static.isaleasy.com static.estt.com.cn;
|
||||
|
||||
# 缩放图片链接
|
||||
location ~* ^(\/pic\/M00(.+\.(jpg|jpeg|gif|png))_(\d+)+x(\d+)+\.(jpg|jpeg|gif|png))$ {
|
||||
root /opt/fastdfs/thumb; # 这里必须设置,否则根目录,即 $document_root 会是 Nginx 默认的 Nginx Root/html,在 Lua 中会得不到期望的值
|
||||
set $thumbnail_root /opt/fastdfs/thumb;
|
||||
set $fdfs_group_root /opt/fastdfs/pic/store0/data;
|
||||
set $file $thumbnail_root$uri;
|
||||
|
||||
if (!-f $file) { # 如果文件不存在时才需要裁剪
|
||||
set $request_filepath $fdfs_group_root$2; # 设置原始图片路径,如:/document_root/1.gif
|
||||
set $img_width $4; # 设置裁剪/缩放的宽度
|
||||
set $img_height $5; # 设置裁剪/缩放的高度
|
||||
set $img_ext $3; # 图片文件格式后缀
|
||||
content_by_lua_file /etc/nginx/lua/img.lua; # 加载外部 Lua 文件
|
||||
}
|
||||
}
|
||||
|
||||
# 默认图片
|
||||
location /pic/M00 {
|
||||
alias /opt/fastdfs/pic/store0/data;
|
||||
ngx_fastdfs_module;
|
||||
}
|
||||
|
||||
location = /favicon.ico {
|
||||
log_not_found off;
|
||||
access_log off;
|
||||
}
|
||||
}
|
||||
|
||||
63
vhost/fdfs.conf
Normal file
63
vhost/fdfs.conf
Normal file
@@ -0,0 +1,63 @@
|
||||
server{
|
||||
listen 80;
|
||||
server_name fdfs.xxx.com;
|
||||
|
||||
set $img_thumbnail_root /opt/fastdfs/thumb; #set thumb path
|
||||
set $img_file $img_thumbnail_root$uri; #thumb file
|
||||
|
||||
# like:/pic/M00/xx/xx/xx.jpg_100-.jpg or /pic/M00/xx/xx/xx.jpg_-100.jpg
|
||||
location ~* ^(\/(\w+)(\/M00)(.+\.(jpg|jpeg|gif|png)))_((\d+\-)|(\-\d+))\.(jpg|jpeg|gif|png)$ {
|
||||
root $img_thumbnail_root; # root path for croped img
|
||||
set $fdfs_group_root /opt/fastdfs/$2/store0/data; #set fastdfs group path $2
|
||||
|
||||
if (!-f $img_file) { # if thumb file not exists
|
||||
add_header X-Powered-By 'Nginx+Lua+GraphicsMagick By Yanue'; # header for test
|
||||
add_header file-path $request_filename; # header for test
|
||||
set $request_filepath $fdfs_group_root$4; # origin_img full path:/document_root/1.gif
|
||||
set $img_size $6; # img width or height size depends on uri : img size like "-100" or "100-", "-" means auto size
|
||||
set $img_ext $5; # file ext
|
||||
content_by_lua_file /etc/nginx/lua/autoSize.lua; # load auto width or height crop Lua file
|
||||
}
|
||||
}
|
||||
|
||||
# like:/pic/M00/xx/xx/xx.jpg_200x100.jpg
|
||||
location ~* ^(\/(\w+)(\/M00)(.+\.(jpg|jpeg|gif|png))_(\d+)+x(\d+)+\.(jpg|jpeg|gif|png))$ {
|
||||
root $img_thumbnail_root; # root path for croped img
|
||||
set $fdfs_group_root /opt/fastdfs/$2/store0/data; #set fastdfs group path $2
|
||||
|
||||
if (!-f $img_file) { # if thumb file not exists
|
||||
add_header X-Powered-By 'Nginx+Lua+GraphicsMagick By Yanue'; # header for test
|
||||
add_header file-path $request_filename; # header for test
|
||||
set $request_filepath $fdfs_group_root$4; # real file path
|
||||
set $img_width $6; # img width
|
||||
set $img_height $7; # img height
|
||||
set $img_ext $5; # file ext
|
||||
content_by_lua_file /etc/nginx/lua/cropSize.lua; # load crop Lua file
|
||||
}
|
||||
}
|
||||
|
||||
location /pic/M00 {
|
||||
alias /opt/fastdfs/pic/store0/data;
|
||||
ngx_fastdfs_module;
|
||||
}
|
||||
|
||||
location /chat/M00 {
|
||||
alias /opt/fastdfs/chat/store0/data;
|
||||
ngx_fastdfs_module;
|
||||
}
|
||||
|
||||
location /avatar/M00 {
|
||||
alias /opt/fastdfs/avatar/store0/data;
|
||||
ngx_fastdfs_module;
|
||||
}
|
||||
|
||||
location /media/M00 {
|
||||
alias /opt/fastdfs/media/store0/data;
|
||||
ngx_fastdfs_module;
|
||||
}
|
||||
|
||||
location = /favicon.ico {
|
||||
log_not_found off;
|
||||
access_log off;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user