欢迎来到山村网

网站图片处理最佳工具:GraphicsMagick

2019-02-28 17:13:44浏览:1014 来源:山村网   
核心摘要:这些天重新改进了我的图片存储系统,已经完全改用php + 命令行GraphicsMagick 及 nginx + lua + GraphicsMagick 的方法处理,php

这些天重新改进了我的图片存储系统,已经完全改用php + 命令行GraphicsMagick 及 nginx + lua + GraphicsMagick 的方法处理,php + 命令行GraphicsMagick 用于预先生成规定范围内尺寸的图片,nginx + lua + GraphicsMagick 用于动态生成指定大小缩图。

GraphicsMagick 是从ImageMagick分离出来的, 比ImageMigack 性能更好, 更适合网站服务器端处理图片用。

CentOS下安装GraphicsMagick

很简单, 先启用EPEL repo, 直接yum安装

# yum -y install GraphicsMagick GraphicsMagick-devel

搞定!

如果你需要在php内使用 pecl 扩展 gmagick,安装方法如下

# pecl install gmagick

# echo 'extension=gmagick.so'> /etc/php.d/gmagick.ini

安装 pecl 扩展出错

# pecl install gmagick

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

使用如下命令

# pecl install channel://pecl.php.net/gmagick-1.1.0RC3

搞定!

GraphicsMagick 常用命令

使用命令基本和 ImageMagick 相同, 放一段我生缩图函数内的代码

注意: 使用 thumbnail 参数比使用 convert 性能好, 用于处理大的图片

if ($width) {

if (!$height) {

$cmd = "gm convert -thumbnail $width -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]],以及它们的大多数依赖项。

wget http://agentzh.org/misc/nginx/ngx_openresty-1.0.15.10.tar.gz

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 代码就不贴了, 参考以下

location /images/ {
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;
}

原文地址:21Andy.com

(责任编辑:豆豆)
下一篇:

缓冲加载图片的jQuery插件lazyload.js 使用方法详解

上一篇:

基于SNS文本数据挖掘: 分析关键词分词技巧

  • 信息二维码

    手机看新闻

  • 分享到
打赏
免责声明
• 
本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们 xfptx@outlook.com