zimg是由国人开源的一个高性能的图片服务器,相关介绍和代码可从github上获取:
https://github.com/buaazp/zimg
1.安装Openssl (这个很关键,必须先安装)
wget http://www.openssl.org/source/openssl-1.0.1e.tar.gz
tar zxvf openssl-1.0.1e.tar.gz
./config shared --prefix=/usr --openssldir=/usr/ssl
make && make install
2. 安装 libevent (http://libevent.org/)
wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
tar zxvf libevent-2.0.17-stable.tar.gz
./configure --prefix=/usr
make && make install
3.安装Cmake
wget "http://www.cmake.org/files/v2.8/cmake-2.8.10.2.tar.gz"tar xzvf cmake-2.8.10.2.tar.gz
cd cmake-2.8.10.2
./bootstrap --prefix=/usr
make && make install
4.安装Libevhtp
wget --no-check-certificate https://github.com/ellzey/libevhtp/archive/1.2.6.tar.gz -O libevhtp-1.2.6.tar.gz
tar xzvf libevhtp-1.2.6.tar.gz
cd libevhtp-1.2.6/
cmake -DCMAKE_PREFIX_PATH=/usr -DCMAKE_INSTALL_PREFIX=/usr -DLIBEVENT_OPENSSL_LIBRARY=/usr/lib/openssl
make && make install
5.安装ImageMagick
#安装图片库
yum -y install libjpeg libjpeg-devel libpng libpng-devel
wget http://www.imagemagick.org/download/ImageMagick-6.8.7-2.tar.gz
tar xzvf ImageMagick-6.8.7-2.tar.gz
cd ImageMagick-6.8.7-2
./configure --with-perl
make && make install
6.安装 libmemcached
wget http://download.tangent.org/libmemcached-0.42.tar.gz
tar zxvf libmemcached-0.42.tar.gz
cd libmemcached-0.42
./configure --with-jp2=yes --with-png=yes --with-jpeg=yes LDFLAGS='-L/usr/lib'
make && make install
7.安装zimg
wget https://github.com/buaazp/zimg/archive/master.zip -O zimg.zip
unzip zimg.zip
cd zimg-master
#修改Makefile文件,红色的是修改的地方,修改完保存退出
#========================================================
OS = $(shell uname)
MAC = Darwin
ifeq ($(OS),Darwin)
LIBS = -levent -levent_openssl -levent_pthreads -lssl -lcrypto -levhtp -lMagickWand-6.Q16 -lmemcached
else
LIBS = -levent -levent_openssl -levent_pthreads -lssl -lcrypto -levhtp -lMagickWand-6.Q16 -lmemcached
endif
OBJS = zhttpd.o zspinlock.o zlog.o zmd5.o zutil.o zcache.o zimg.o main.o
CFLAGS = -Wall -I /usr/local/include/ImageMagick-6 -DMAGICKCORE_HDRI_ENABLE=0 -DMAGICKCORE_QUANTUM_DEPTH=16
all: ${OBJS}
gcc -${CFLAGS} -o zimg ${OBJS} ${LIBS}
clean:
rm -f zimg ${OBJS}
#=================================================================
#编译
make
#启动
./zimg
#支持启动参数
-p [port] 监听端口号,默认4869
-t [thread_num] 线程数,默认4,请调整为具体服务器的CPU核心数
-k [max_keepalive_num] 最高保持连接数,默认1,不启用长连接,0为启用
-l 启用log,会带来很大的性能损失,自行斟酌是否开启
-M [memcached_ip] 启用缓存的连接IP
-m [memcached_port] 启用缓存的连接端口
-b [backlog_num] 每个线程的最大连接数,默认1024,酌情设置
调用demo:
upload.html
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<html>
<form enctype="multipart/form-data" action="http://192.168.111.189:9000/upload" method=post>
选择文件:
<input name="userfile" type="file">
<input type="submit" value="上传">
</form>
</html>
选择图片提交,返回:
MD5:16448cd6ec7af958e948e07267798042
上传路径:
http://192.168.111.189:9000/upload
访问原图:
http://192.168.111.189:9000/16448cd6ec7af958e948e07267798042
裁剪:
http://192.168.111.189:9000/16448cd6ec7af958e948e07267798042?w=200&h=200
灰化:
http://192.168.111.189:9000/16448cd6ec7af958e948e07267798042?g=1
经过试用,发现zimg还不能满足我们的需要,于是用nodejs翻写了一个版本,做了一些更改,代码已开源到github:
https://github.com/ofpay/nimg