软件准备

  • Tengine

官网:http://tengine.taobao.org/

$ wget http://tengine.taobao.org/download/tengine-2.2.0.tar.gz
  • Lua

官网:http://www.lua.org/

$ wget http://www.lua.org/ftp/lua-5.3.4.tar.gz
  • LuaJIT

官网:http://luajit.org/

$ wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
  • GraphicsMagick

官网:http://www.graphicsmagick.org/

$ wget https://sourceforge.net/projects/graphicsmagick/files/graphicsmagick/1.3.26/GraphicsMagick-1.3.26.tar.gz

软件安装

1.安装 Lua 依赖工具(Lua安装所需)

$ sudo yum -y install readline readline-devel

如果不安装readline和readline-devel,在安装Lua时会报如下错误
致命错误:readline/readline.h:没有那个文件或目录

2. 安装 Lua

$ tar -zxvf lua-5.3.4.tar.gz
$ cd lua-5.3.4
$ make linux && make install

执行命令后如果可以看到lua的版本信息,使用print来打印内容

$ lua
Lua 5.3.4  Copyright (C) 1994-2017 Lua.org, PUC-Rio
> print('Hello Lua')
Hello Lua

3. 安装LuaJIT

$ tar -zxvf LuaJIT-2.0.5.tar.gz
$ cd LuaJIT-2.0.5
$ sudo make && make install

配置环境变量(笔者配置在用户变量,你也可以配置在profile中)

$ vim ~/.bashrc
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.0
$ source ~/.bashrc

4. 安装Tengine

$ tar -zxvf tengine-2.2.0.tar.gz
$ cd tengine-2.2.0
$ ./configure --prefix=/usr/local/Tengine --dso-path=/usr/local/Tengine/modules --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_concat_module --with-http_lua_module --http-proxy-temp-path=/var/tmp/Tengine/proxy_temp --http-fastcgi-temp-path=/var/tmp/Tengine/fastcgi_temp --http-uwsgi-temp-path=/var/tmp/Tengine/uwsgi_temp --http-scgi-temp-path=/var/tmp/Tengine/cgi_temp --http-client-body-temp-path=/var/tmp/Tengine/client_body_temp --http-log-path=/var/log/Tengine/access.log --error-log-path=/var/log/Tengine/error.log

执行上面的命令后,打印的信息中笔者看到有Lua的库找不到(如果Lua库找不到在启动Tengine时会报错)

解决方法:配置LuaJIT的环境变量或执行如下命令

$ ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

编译安装

$ sudo make && make install

测试

$ /usr/local/Tengine/sbin

执行命令测试,出现如下类似信息,表示安装成功

$ ./nginx -t
nginx: the configuration file /usr/local/Tengine/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/Tengine/conf/nginx.conf test is successful

执行 ./nginx -t 后,笔者遇到两个问题,错误及解放方法如下

  • 错误1:

./nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory

解决方法:执行如下命令

ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
  • 错误2:

错误2:nginx: [emerg] mkdir() “/var/tmp/Tengine/client_body_temp” failed (2: No such file or directory)

解决方法:执行命令创建文件mkdir -p /var/tmp/Tengine/client_body_temp

$ mkdir -p /var/tmp/Tengine/client_body_temp

进入 /usr/local/Tengine/sbin 目录下,输入如下命令启动

$ ./nginx

启动成功后,在浏览器输入ip,可以看到 tengine 的欢迎页面

5.安装 GraphicsMagick 依赖工具

$ sudo yum -y install libjpeg libjpeg-devel libpng libpng-devel giflib giflib-devel freetype freetype-devel

6. 安装GraphicsMagick

$ tar -zxvf GraphicsMagick-1.3.26.tar.gz
$ cd GraphicsMagick-1.3.26
$ ./configure --prefix=/usr/local/GraphicsMagick --enable-shared
$ make && make install

编译完成后进入GraphicsMagick的bin目录下

$ cd /usr/local/GraphicsMagick/bin

查看命令帮助:

$ ./gm
GraphicsMagick 1.3.26 2017-07-04 Q8 http://www.GraphicsMagick.org/
Copyright (C) 2002-2017 GraphicsMagick Group.
Additional copyrights and licenses apply to this software.
See http://www.GraphicsMagick.org/www/Copyright.html for details.
Usage: ./gm command [options ...]Where commands include: animate - animate a sequence of imagesbatch - issue multiple commands in interactive or batch modebenchmark - benchmark one of the other commandscompare - compare two imagescomposite - composite images togetherconjure - execute a Magick Scripting Language (MSL) XML scriptconvert - convert an image or sequence of imagesdisplay - display an image on a workstation running Xhelp - obtain usage message for named commandidentify - describe an image or image sequenceimport - capture an application or X server screenmogrify - transform an image or sequence of imagesmontage - create a composite image (in a grid) from separate imagestime - time one of the other commandsversion - obtain release version

查看版本信息,输入如下命令后,如果看到如下类似的信息,表示安装成功

$ ./gm version
GraphicsMagick 1.3.26 2017-07-04 Q8 http://www.GraphicsMagick.org/
Copyright (C) 2002-2017 GraphicsMagick Group.
Additional copyrights and licenses apply to this software.
See http://www.GraphicsMagick.org/www/Copyright.html for details.Feature Support:Native Thread Safe       yesLarge Files (> 32 bit)   yesLarge Memory (> 32 bit)  yesBZIP                     yesDPS                      noFlashPix                 noFreeType                 yesGhostscript (Library)    noJBIG                     noJPEG-2000                noJPEG                     yesLittle CMS               noLoadable Modules         noOpenMP                   yes (201107)PNG                      yesTIFF                     noTRIO                     noUMEM                     noWebP                     noWMF                      noX11                      yesXML                      yesZLIB                     yesHost type: x86_64-unknown-linux-gnuConfigured using the command:./configure  '--prefix=/usr/local/GraphicsMagick' '--enable-shared'Final Build Parameters:CC       = gcc -std=gnu99CFLAGS   = -fopenmp -g -O2 -Wall -pthreadCPPFLAGS = -I/usr/include/freetype2 -I/usr/include/libxml2CXX      = g++CXXFLAGS = -pthreadLDFLAGS  = LIBS     = -lfreetype -ljpeg -lpng15 -lX11 -llzma -lbz2 -lxml2 -lz -lm -lgomp -lpthread

使用 GraphicsMagick 来裁剪一张图

$ rz   # 上传图片test.jpg到~/photo路径下

执行命令裁剪图片

$ sudo ./gm convert ~/photo/test.jpg -resize 200x200 ~/photo/small.jpg

下载裁剪完成后的图片(图片变小但不会失真)

$ sz ~/photo/small.jpg

2.测试

1.nginx + lua 测试

配置nginx.conf文件

$ cd /usr/local/Tengine/
$ cd conf

为了保险起见,我们先备份一下nginx.conf文件

$ cp nginx.conf nginx.conf.bak

编辑nginx.conf文件

# vim nginx.conf

找到 #user nobody; 在它的下一行添加如下代码

user root

在server块(大概在)中添加如下代码

location /lua/ {default_type 'text/html';content_by_lua 'ngx.say("<h1>hello, lua</h1>")';
}

保存退出,后执行如下命令重新加载ngnix

# ./nginx -s reload

浏览器访问:IP/lua/

2.裁剪图

(1).配置nginx

# cd /usr/local/Tengine/conf
# vim nginx.conf

找到 #user nobody; 在它的下一行添加如下代码

user root

server 中新增location

server {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {#root   html;root   /home/hadoop/images;index  index.html index.htm;}location /lua/ {default_type 'text/html';content_by_lua 'ngx.say("<h1>hello, lua</h1>")';}# url格式:如:xxxx.gif_数字x数字.giflocation ~* ^(.+\.(jpg|jpeg|gif|png))_(\d+)x(\d+)\.(jpg|jpeg|gif|png)$ {  root /home/hadoop/images;       # 根目录(必须设置)if (!-f $request_filename) {       # 如果文件不存在才裁剪# 在编写外部 Lua 脚本时,设置为 off Nginx 不会缓存 Lua,方便调试lua_code_cache off;set $request_filepath /home/hadoop/images/$1; # 原始图片路径set $width $3;     # 设置裁剪/缩放的宽度set $height $4;     # 设置裁剪/缩放的高度set $ext $5;     # 图片文件格式后缀content_by_lua_file /usr/local/Tengine/lua/ImageResizer.lua;      # 加载外部lua文件}
}

(2).编写脚本

进入如下目录

# cd /usr/local/Tengine/

新建文件夹lua,lua下新建文件

# mkdir lua
# touch ImageResizer.lua
# vim ImageResizer.lua

ImageResizer.lua 文件中编写代码如下

local command = "/usr/local/GraphicsMagick/bin/gm convert " .. ngx.var.request_filepath .. " -resize " .. ngx.var.width .. "x" .. ngx.var.height .. " +profile \"*\" " .. ngx.var.request_filepath .. "_" .. ngx.var.width .. "x" .. ngx.var.height .. "." .. ngx.var.ext;
os.execute(command);            # 执行裁剪命令
ngx.exec(ngx.var.request_uri);  # 输出裁剪后的图片

(3).执行并查看结果

输入如下命令重新加载nginx,使用修改的配置立即生效

# ./nginx -s reload

浏览器访问:http://IP/test.jpg_300x300.jpg

使用Tengine+Lua+GraphicsMagick实现图片自动裁剪缩放相关推荐

  1. nginx 配置图片自动裁剪

    nginx 配置图片自动裁剪 Image Filter module(图像过滤处理模块) 让不同部分能协同工作,nginx需要编译进图像滤波,代理和安全链接三个模块 nginx配置 location ...

  2. php图片自动裁剪图片,php裁剪图片(支持定点裁剪)

    /** * 图片裁剪函数,支持指定定点裁剪和方位裁剪两种裁剪模式 * @param $src_file 原图片路径 * @param $new_width 裁剪后图片宽度(当宽度超过原图片宽度时,去原 ...

  3. android 图片自动裁剪图片大小,android调用原生图片裁剪后图片尺寸缩放的解决方法...

    在安卓开发中,如果对拍照后的图片进行图片裁剪,如果是调用系统的裁剪,如下: /* * 裁剪图片 */ private void cropPhoto() { Intent intent = new In ...

  4. ps解决导出图片自动裁剪导致的图像位置偏移的问题

    1.将需要导出的图片复制到新的文件 2.文档处选择新建或已有的psd文件.(这些步骤只是为了将需要导出的图层放到别的文件中,方便操作) 3.注意,我们不使用导出图片,导出图片会进行裁剪,我们使用储存为 ...

  5. android 图片自动裁剪图片,Android实现拍照、选择相册图片并裁剪功能

    通过拍照或相册中获取图片,并进行裁剪操作,然后把图片显示到ImageView上. 当然也可以上传到服务器(项目中绝大部分情况是上传到服务器),参考网上资料及结合项目实际情况, 测试了多款手机暂时没有发 ...

  6. android 图片自动裁剪图片,Android图片选择到裁剪之步步深坑 – 简书

    我的问题已经解决,查阅了几十篇相关文章,换了数种解决方案,最终解决,虽然不是按照本文来的,但关闭窗口的时候发现本文算是系统,特此转载. 最近在自己的项目里实现了一个头像选择的功能,就是先从相册里选取一 ...

  7. html 图片自动裁剪,如何用CSS自动裁剪图片 - Thomas'博客

    width:100%; height:0; padding-bottom: 100%; overflow:hidden; background-position: center center; bac ...

  8. css图片自动裁剪居中 不拉伸 自适应

    如图 红框内是展示的内容,红框外的色域是被裁掉的部分 父元素宽高固定的情况下,图片子元素css代码如下: img {height: 100%;width: 100%;object-fit: cover ...

  9. 计算摄影 | 计算机如何学会自动裁剪图片(自动构图)?

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 1 自动构图基础 1.1 什么是构图 自动裁剪用摄影的话语来说,就 ...

最新文章

  1. 使用Repeater的Template
  2. 网络编程学习笔记(TCP回射服务器程序修订版)
  3. 如何用极致业务基础平台做一个通用企业ERP系列之三启用期间管理设计
  4. 王道408数据结构——第一章 绪论
  5. 归并排序的实现-代码
  6. 【bzoj3119】Book
  7. 哪些数据类型能使用切片操作python_Python学习之路「16」非数字数据类型-切片-slice...
  8. Java 多线程详解(三)------线程的同步
  9. 一篇写给从未编程过的人的入门教程
  10. 深南电国际油价期权对赌协议详解? 毁约?
  11. 国内CDN加速的背景和现状
  12. linux磁盘阵列 启动盘,Linux RAID磁盘列阵完全攻略
  13. barcode.jar处理条形码
  14. 尤大都推荐的组件库是如何开发出来的?
  15. 03 数据载入、存储及文件格式
  16. torch中的inplace操作问题解决方法
  17. 【文章】我在华为打工的日子
  18. ArcGis之椭球面积计算工具
  19. 2017qq红包雨最强攻略
  20. PS入门(1-2) 灰度色彩模式

热门文章

  1. 上海中学高考2021成绩查询,2021上海地区高考成绩排名查询,上海高考各高中成绩喜报榜单...
  2. Y - 区域内点的个数
  3. 使整个网站变黑白(灰色)的特效代码
  4. discuz admin.php换名,discuz更换域名修改方法
  5. 关于电荷放大器的问题
  6. 【转】 http请求包含什么
  7. html给图标加角标,HTML+CSS入门 如何使用CSS创建一个图片角标
  8. throw 和 throws
  9. window 相关dll文件下载
  10. c#-线程-取消架构-Task-简单实验