图片缩略图动态生成

旧博文备份http://blog.uedao.com/blog/?p=85

整理下网站缩略图水印图动态生成的方法:

会员上传图片后,调用图片的各地方需要的尺寸不全一样,需要生成大小不同的等比例缩略图。
实现方法是nginx判断,请求的缩略图规则url中,如果图片不存在则调用php程序以原始图片生成对应的缩略图,然后显示出来,并且图片第一次生成后下次直接调用即可。

一、基本原则:
只保留原始图片,其它尺寸由原图生成。
缩略图按需生成,大小体现在固定的url。

二、图片文件:
原始图路径:/home/htdocs/app/upfiles/house/年/月/日/src图片名
生成图路径:/home/htdocs/app/upfiles/house/active/年/月/日/图片名
图片生成大小
生成图路径:/home/htdocs/app/upfiles/house/active/年/月/日/图片名_图片生成大小及会员编号
原始图url:

http://p2.uedao.com/house/2011/02/12/src_130037359579225801.jpg

缩略图url(不存在则自动生成):

http://p1.uedao.com/house/active/2011/02/12/130037359579225801_128x92.jpg

水印图url(不存在则自动生成):

http://p1.uedao.com/house/active/2011/02/12/130037359579225801_128x92x1001.jpg

三、nginx配置(nginx.conf)

#p1.uedao.com p2.uedao.com
server {
    # listen port
    listen       80;
    server_name  p1.uedao.com p2.uedao.com;
    charset utf-8;

    # root
    root   /home/htdocs/app/upfiles;
    index  index.php index.html;

    # blocked
    location ~ .*\.(gif|jpg|png|jpeg|bmp|swf|cur|ico)$ {
        valid_referers none blocked *.uedao.com *.facebook.com;
        if ($invalid_referer) {
           #return 404;
        }
        location ~* /house/active/(.+)$ {
            if (!-f $request_filename) {
                proxy_pass http://localhost:8000/app/action/createimg.php?s=$1&$args;
                break;
            }
        }
        expires max;
        access_log off;
    }
}
#注:$1,为location匹配的正则结果;$args, 请求中的参数。

四、php程序(createimg.php)

$src = $GET ['s']; //图片路径
if ($src) {
    //省略数据验证(如请求来源判断、只响应指定尺寸等)
    $src_array = explode ( '/', $src );
    $src_array_len = count ( $src_array );
    $file_path = $src_array [0] . '/' . $src_array [1] . '/' . $src_array [2] . '/';
    $file_name = $src_array [3];
    //生成大小
    $size_ext_str = strrchr ( trim ( $file_name ), '_' );
    $size_str = substr ( str_replace ( strrchr ( $size_ext_str, '.' ), '', $size_ext_str ), 1 );
    $size_array = explode ( 'x', $size_str );

    $des_file_name = getSourceImgSrc ( $file_path, $file_name ); //会员原始图片
    if (! file_exists ( $des_file_name )) {
        $width = $size_array [0];
        $heigh = $size_array [1];
        $src_file_name = getDestImgSrc ( $file_path, $file_name ); //生成目标图片
        $obj_image = new Tool_Image ();
        if (2 == count ( $size_array )) {
            //生成缩略图
            $result = $obj_image->make_thumb ( $src_file_name, $width, $heigh, $des_file_name );
        } else if (3 == count ( $size_array ) && 0 != intval ( $size_array [2] )) {
            //生成水印图
            $user_id = $size_array [2];
            $water_img = FILE_PATH . 'images/index/public/global/logo.png';
            $result = $obj_image->make_watermark ( $src_file_name, $width, $heigh, $des_file_name, $water_img, $user_id );
        }
    }
    loadImgFile ( $des_file_name ); //读取图片并显示
}
//更快的读取图片并显示
function loadImgFile($file_name) {
    $fp = fopen ( $file_name, 'r' );
    header ( "Content-type:image/jpeg" );
    fpassthru ( $fp );
    return true;
}

注:生成缩略图后需要程序读取显示出来。

五、相关文章
如果你感兴趣阅读到此,下面的文章你可能也想看。
Nginx做动态生成缩略图

http://cnctblog.com/?p=633

Abusing Amazon images

http://aaugh.com/imageabuse.html

基于MongoDB GridFS的图片存储

http://liut.cc/blog/2010/12/about-imsto_my-first-open-source-project.html