网站图片处理最佳工具 GraphicsMagick
转载请注明原文出处: 21Andy.com
这些天重新改进了我的图片存储系统, 已经完全改用 php + 命令行GraphicsMagick 及 nginx + lua + GraphicsMagick 的方法处理, php + 命令行GraphicsMagick 用于预先生成规定范围内尺寸的图片, nginx + lua + GraphicsMagick 用于动态生成指定大小缩图
原文作者: 21Andy.com
GraphicsMagick 是从 ImageMagick 分离出来的, 比 ImageMigack 性能更好, 更适合网站服务器端处理图片用
CentOS 下安装 GraphicsMagick
很简单, 先启用EPEL repo, 直接yum安装
搞定!
如果你需要在php内使用 pecl 扩展 gmagick,安装方法如下
# echo 'extension=gmagick.so' > /etc/php.d/gmagick.ini
安装 pecl 扩展出错
Failed to download pecl/gmagick within preferred state "stable", latest release is version 1.1.0RC3, stability "beta", use "channel://pecl.php.net/gmagick-1.1.0RC3" to install
使用如下命令
搞定!
GraphicsMagick 常用命令
使用命令基本和 ImageMagick 相同, 放一段我生缩图函数内的代码
注意: 使用 thumbnail 参数比使用 convert 性能好, 用于处理大的图片
if (!$height) {
$cmd = "gm convert -thumbnail {$width}x -quality $quality $file $new_file";
}
else {
$cmd = "gm convert -thumbnail {$width}x{$height}^ -gravity center -extent {$width}x{$height} -quality $quality $file $new_file";
}
}
elseif ($height) {
$cmd = "gm convert -thumbnail x$height -quality $quality $file $new_file";
}
更多可以参考官方文档: http://www.graphicsmagick.org/GraphicsMagick.html#details-thumbnail
nginx + lua + GraphicsMagick
nginx + lua 安装
http://openresty.org/cn/index.html
OpenResty (也称为 ngx_openresty)是一个全功能的 Web 应用服务器,它打包了标准的 [[Nginx]] 核心,很多的常用的[[第三方模块|http://wiki.nginx.org/3rdPartyModules]],以及它们的大多数依赖项。
tar -zxvf ngx_openresty-1.0.15.10.tar.gz
cd ngx_openresty-1.0.15.10
./configure --user=www --group=www --prefix=/usr/local/openresty \
--with-luajit \
--with-http_iconv_module
make -j4 #2核就j2,8核j8
make install
我的 nginx 代码就不贴了, 参考以下
set $image_root /home/tomcat/eisp-files;
set $file "$image_root$uri";
if (!-f $file) {
rewrite_by_lua '
local index = string.find(ngx.var.uri, "([0-9]+)x([0-9]+)");
local originalUri = string.sub(ngx.var.uri, 0, index-2);
local area = string.sub(ngx.var.uri, index);
index = string.find(area, "([.])");
area = string.sub(area, 0, index-1);
local image_sizes = {"80x80", "800x600", "40x40"};
function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
if table.contains(image_sizes, area) then
local command = "gm convert " .. ngx.var.image_root .. originalUri .. " -thumbnail " .. area .. " -background gray -gravity center -extent " .. area .. " " .. ngx.var.file;
os.execute(command);
else
ngx.exit(404);
end;
';
}
alias /home/tomcat/eisp-files/images/;
expires 7d;
}
Incoming search terms:
- graphicsmagick
- nginx lua graphicsmagick
- nginx GraphicsMagick
- GraphicsMagick ImageMagick
- lua GraphicsMagick
- nginx lua gm
- nginx 图片处理插件
- graphicsmagick 性能
- local index = string find(ngx var uri ([0-9] )x([0-9] ));
- openresty
- graphicsmagick quality
- GraphicsMagicks thumbnail
- MagickNet
- graphicsmagick 优化
Tags: nginx, GraphicsMagick, ImageMagick, PHP, 图片处理
貌似国外图片获取流量很容易,我看周围有很多人都在悄悄布局图片站
vps呀
学习了