两种动态切图实现方案性能比较

    最近在维护图床——图片存储、优化,针对业务需要,一张图片要切成多个尺寸,QPS较低,而此时展现机却非常闲,尝试进行动态切图。实现中对两种切图方案进行比较分析:

1. nginx+php+fastcgi+image filter module+gd

具体实践可参见 http://doyoueat.iteye.com/blog/1279493

2. nginx+php+fastcgi+imagick

#ngx conf
     location ~* ^/((resize|mugshot)/(\d+)?_(\d+)?_(\d+)?/)?([^/]*)\.(jpg|png|bmp|gif)$ {
         rewrite ^/((resize|mugshot)/(\d+)?_(\d+)?_(\d+)?/)?([^/]*)\.(jpg|png|bmp|gif)$ /backend/?resize_prefix=$1&type=$2&width=$3&height=$4&quality=$5&key=$6&postfix=$7 last;
     }
location = /backend/ {                    
    internal;                             
    root           ${SRC_ROOT}/apps/fnt ; 
    expires max;                                                     
                                                                
    include        fastcgi_params ;                                  
    fastcgi_pass   127.0.0.1:${CGI_PORT};                            
    fastcgi_index  index.php;                                        
    fastcgi_param  SCRIPT_FILENAME  ${SRC_ROOT}/apps/fnt/index.php ; 
    fastcgi_param  QUERY_STRING     $query_string;                   
                                                                  
    client_max_body_size       100m;                                 
    fastcgi_connect_timeout 1000s;                                   
    fastcgi_send_timeout 1000s;                                      
    fastcgi_read_timeout 1000s;                                      

    使用imagick+php进行图片处理的例子较多(可以参见 http://blog.lizhigang.net/archives/228),这里不做详细说明,只以注释的形式说明。
//index.php
/*
 *get image blob && get _GET params
 */
switch($type) //按业务指定的动态切图类型进行处理(按指定边进行缩放或先缩再切,                  etc)                                 
{                                             
case "resize":                                    
    if(!empty($width)&&!empty($height))       
    {                                         
       /*
        *按图片大边进行缩放图片:imagick scale image
        */              
    }                                         
    else if($width||$height)                  
    {                                         
       /*
        *按指定边进行图片缩放:imagick scale image
        */               
    }                                         
    break;                                    
case "mugshot": 
    /*
     *先缩放再从中间切割图片:imagick cropthumbnailImage
     */                                   
    break;
default:
    break;

两种动态切图性能比较:
    测试环境:16*2.4G的cpu,96G内存
1. image filter module
[img]
两种动态切图实现方案性能比较
[/img]
2. imagick

两种动态切图实现方案性能比较

总结:
    从上面两图可以明显看出,随着图片大小的增大,QPS逐渐减小;同样的处理图片,第二种处理方案(php+imagick)比第一种处理方案(image_filter)高出100QPS;第一种处理方案的平均QPS在300左右,第二种方案的QPS在400左右

你可能感兴趣的:(nginx)