Chinaunix首页 | 论坛 | 博客
  • 博客访问: 651048
  • 博文数量: 149
  • 博客积分: 3901
  • 博客等级: 中校
  • 技术积分: 1558
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-16 14:33
文章分类

全部博文(149)

文章存档

2014年(2)

2013年(10)

2012年(32)

2011年(21)

2010年(84)

分类:

2010-06-24 10:24:56


示意图:

http://blog.chinaunix.net/photo/93118_100624101239.jpg



实例 - nginx - perl  + gearman - python 切图 :
编译 nginx 时先装 pcre -
CFLAGS='-O2 -Wall'
./configure --prefix=/usr/local/ \
            --enable-utf8 \
            --enable-unicode-properties \
            --with-match-limit=500000 \
            --with-match-limit-recursion=500000


nginx 编译 :
./configure --with-http_perl_module \
                         --with-http_image_filter_module \
                         --prefix=/root/tools/nginx \
                         --sbin-path=/root/tools/nginx \
                         --conf-path=/root/tools/nginx/conf/nginx.conf \
                         --error-log-path=/root/tools/nginx/error.log \
                         --pid-path=/root/tools/nginx/nginx.pid \
                         --lock-path=/root/tools/nginx/nginx.lock \
                         --with-http_flv_module \
                         --with-http_gzip_static_module \
                         --http-log-path=/root/tools/nginx/access.log \
                         --http-fastcgi-temp-path=/var/tmp/nginx/fcgi


nginx 主配置 :



http {
............

    perl_modules perl/lib;
    perl_require hello.pm;

    perl_set $msie6 '
    sub {
      my $r = shift;
      my $ua = $r->header_in("User-Agent");
      return "" if $ua =~ /Opera/;
      return "1" if $ua =~ / MSIE [6-9] \.\d+/;
      return "";
    }
   ';

    .......


    server {

        ........

        # /type/mask/uid/appid/file
        location ~ ^/img/(.*) {
            alias /tmp/img/$1 ;
            error_page 404 /img/404.jpg;
        }

        # /img_convert/origin_abc.jpg/format_zyf.png
        # /uid_00000/appid_00000
        # /h_000/w_000
        # > http://192.168.102.207/img/001/1001/100040/up_user.700x800.png
        # > http://192.168.102.207/img_convert/format_png/uid_1001/appid_100040/h_800/w_700/origin_up_user.jpg
        location ~ ^/img_convert/.*$ {
            root /tmp/img/ ;
            perl hello::handler ;
        }

         ............
}



nginx + perl

package hello;
use nginx;
use Gearman::Client;

my $client = Gearman::Client->new();
$client->job_servers("192.168.102.207:4730");

sub handler {
  my $r = shift;
  # $r->send_http_header("image/jpeg");
  return OK if $r->header_only;

  my $rpath = '/tmp/img/';
  my %m = {} ;
  # 取 url 参数

  #map{ $m{$1}=$2 if /(.*?)=(.*)/ ; } split /\&/,$r->args ;
  # rewrite
  # /convert/origin_abc.jpg/format_zyf.png
  # /type_img audio video txt bin
  # /uid_00000/appid_00000
  # /h_000/w_000
  map{ $m{$1}=$2 if /(.*?)_(.*)/ ; } split /\//,$r->uri ;



  # jpg|jpeg|gif
  my $mask = '000'+$m{uid};
  $mask = $1 if $mask =~ /.*?(.{3})$/;
  my $filename = $1 if $m{origin} =~ /(.*?)\..*/;
  if( $m{format} eq "" ){ $m{format}=$1 if $m{origin} =~ /.*?\.(.*)/ }

  my $sou_file = "/$m{type}/$mask/$m{uid}/$m{appid}/$m{origin}" ;
  # 没有 w h format 走原图
  if( $m{h} eq "" and $m{w} eq "" ){
     $r->internal_redirect("/img/$sou_file");
     return OK ;
  }

  my $to_file = "/$mask/$m{uid}/$m{appid}/$filename.$m{w}x$m{h}.$m{format}" ;

  #处理裁图
  #filePath原文件路径
  #targetWidth目标尺寸宽
  #targetHeight目标尺寸高
  #targetFilePath目标文件路径
  #返回处理命令字符串
  unless (-e "$rpath/$to_file" ){
   my $comm = &handleResize( "/$rpath/$sou_file" , $m{w} , $m{h} , "/$rpath/$to_file" ) ;
   $client->do_task("shell_run", $comm );
  }


  $r->internal_redirect("/img/$to_file");
  return OK ;
}


#通过调用系统命令identify来确定图片尺寸
#返回宽高数组
sub getImageSize($){
    my ($fileName) = @_;
    open (IDENTIFY, "identify ".$fileName." |");
    my $file_desc = "";
    while (my $line = <IDENTIFY>){
        $file_desc = $line;
    }
    close IDENTIFY;
    my ($fName,$extandName,$size) = split(/\s/,$file_desc);
    my ($width,$height) = split(/x/,$size);
    my @result = ();
    push(@result,$width);
    push(@result,$height);
    return @result;
}



#处理裁图
#filePath原文件路径
#targetWidth目标尺寸宽
#targetHeight目标尺寸高
#targetFilePath目标文件路径
#返回处理命令字符串
sub handleResize($$$$){
    my ($filePath,$targetWidth,$targetHeight,$targetFilePath) = @_;
    my @size = &getImageSize($filePath);
    my $width = $size[0]; #原图宽
    my $height = $size[1]; #原图高

    #比例一致
    if( $width/$height == $targetWidth/$targetHeight ){
        return "convert ".$filePath." -resize ".$targetWidth."x".$targetHeight." ".$targetFilePath
    } else {
    #比例不一致时,先缩放到较长的一边,另一边裁剪处理,默认缩小操作

        if( $targetWidth>$targetHeight ){
            $tempHeight = int( $targetWidth*($height/$width) );
            $tempCutHeight = int( ($tempHeight-$targetHeight)/2 ); return "convert ".$filePath." -resize ".$targetWidth."x".$tempHeight." -crop ".$targetWidth."x".$targetHeight."+0+".$tempCutHeight." +repage ".$targetFilePath;
        } else {
           $tempWidth = int( $targetHeight*($width/$height) );
           $tempCutWidth = int( ($tempWidth-$targetWidth)/2 );
           return "convert ".$filePath." -resize ".$tempWidth."x".$targetHeight." -crop ".$targetWidth."x".$targetHeight."+".$tempCutWidth."+0 +repage ".$targetFilePath;
        }
    }
}

1;
__END__





greamam + work :

from gearman import *
worker = GearmanWorker(["192.168.102.207:4730"])

def job_run(job) :
    import os
    print job.arg
    return os.system(job.arg)

worker.register_function("shell_run",job_run)
worker.work()









阅读(2301) | 评论(1) | 转发(0) |
0

上一篇:gearman python 使用

下一篇:python 异常处理

给主人留下些什么吧!~~

ayl0012010-07-19 18:45:58

很好,很强大!