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

类别:移动互联网 | 发布时间:2012-9-3 14:20:16 | 网迅科技  |  阅读次数: 

这些天重新改进了我的图片存储系统,已经完全改用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

此文关键字:宝鸡网站建设,宝鸡网站开发,宝鸡网站优化,宝鸡文理学院课程

在线项目合作

网迅科技结合创新的设计理念与专业技术,为客户制定从策划到创意再到技术的系列支持,帮助企业通过全新的媒体展示平台实现更多价值。如果您喜欢我们的项目,有兴趣合作?请您联系我们,以便我们能够为您提供可行的价值服务。