Nginx Proxy cache + Perl + ImageMagick 做Wap站图片适配 实时压缩
2015-08-21 09:43
190 查看
访问地址: http://172.19.5.47/aaaa/test1.jpg_300x240.jpg
Nginx的配置就不上了,直接贴Perl代码:
package ImageConvertForTRS;
use nginx;
use Image::Magick;
our $baseDir = "/home/tuanCenter/headPhoto/";
sub convertImg{
my $r = shift;
my $path = "";
my $thumbnail = "";
#$r->send_http_header('text/html; charset=utf-8');
my @arr = split('/',$r->uri); #分割请求的uri
my $imgPath = @arr[2];
#$r->print("imgPath: ".$imgPath."\n\r");
my @imgURI = split('_',$imgPath);
$suffix = (split('\.', @imgURI[0]))[1];
#$r->print("suffix: ".$suffix."\n\r");
if("jpg" eq lc($suffix) || "png" eq lc($suffix) || "gif" eq lc($suffix)){
my $imgWHstr = (split('\.', @imgURI[1]))[0];
if ($imgWHstr eq "300x240" || $imgWHstr eq "150x120" || $imgWHstr eq "315x135") {
$thumbnail = $imgWHstr."^";
} elsif ($imgWHstr eq "300x") {
$thumbnail = $imgWHstr;
} else {
return 404;
}
$r->send_http_header('image/'.lc($suffix));
$image= new Image::Magick;
$image->Read($baseDir.@imgURI[0]); #读取图片
$image->Thumbnail(geometry=>$thumbnail); # 注意 ! 缩略图 先Thumbnail(压缩)再Extent(裁剪) 顺序要对 否则图片的尺寸或者压缩都不对
$image->Extent(geometry=>$thumbnail, gravity=>'Center');
#$image = exec("convert /home/tuanCenter/headPhoto/test1.jpg -thumbnail \"300x240^\" -gravity center -extent 300x 240 /home/tuanCenter/headPhoto/test2.jpg");
@blobs = $image->ImageToBlob(); #将resize过的图片转换成Blob二进制对象
$r->print(@blobs); #直接返回二进制对象
return OK;
}
#$r->rflush();
return 404;
}
1;
__END__
Nginx的配置就不上了,直接贴Perl代码:
package ImageConvertForTRS;
use nginx;
use Image::Magick;
our $baseDir = "/home/tuanCenter/headPhoto/";
sub convertImg{
my $r = shift;
my $path = "";
my $thumbnail = "";
#$r->send_http_header('text/html; charset=utf-8');
my @arr = split('/',$r->uri); #分割请求的uri
my $imgPath = @arr[2];
#$r->print("imgPath: ".$imgPath."\n\r");
my @imgURI = split('_',$imgPath);
$suffix = (split('\.', @imgURI[0]))[1];
#$r->print("suffix: ".$suffix."\n\r");
if("jpg" eq lc($suffix) || "png" eq lc($suffix) || "gif" eq lc($suffix)){
my $imgWHstr = (split('\.', @imgURI[1]))[0];
if ($imgWHstr eq "300x240" || $imgWHstr eq "150x120" || $imgWHstr eq "315x135") {
$thumbnail = $imgWHstr."^";
} elsif ($imgWHstr eq "300x") {
$thumbnail = $imgWHstr;
} else {
return 404;
}
$r->send_http_header('image/'.lc($suffix));
$image= new Image::Magick;
$image->Read($baseDir.@imgURI[0]); #读取图片
$image->Thumbnail(geometry=>$thumbnail); # 注意 ! 缩略图 先Thumbnail(压缩)再Extent(裁剪) 顺序要对 否则图片的尺寸或者压缩都不对
$image->Extent(geometry=>$thumbnail, gravity=>'Center');
#$image = exec("convert /home/tuanCenter/headPhoto/test1.jpg -thumbnail \"300x240^\" -gravity center -extent 300x 240 /home/tuanCenter/headPhoto/test2.jpg");
@blobs = $image->ImageToBlob(); #将resize过的图片转换成Blob二进制对象
$r->print(@blobs); #直接返回二进制对象
return OK;
}
#$r->rflush();
return 404;
}
1;
__END__
相关文章推荐
- 03_Nginx添加新模块
- 03_Nginx添加新模块
- Apache ab测试 Apache优于 nginx
- MySQL+nginx+php环境在ubuntu14.04下的搭建
- Nginx负载均衡
- nginx报502 bad GateWay错误的解决方法
- nginx中的try_files指令解释
- 树莓派做web服务器(nginx、Apache)
- Nginx工作原理和优化、漏洞
- nginx upstream的分配方式
- 使用socket方式连接nginx
- nginx配置
- Nginx下限速限制下载速度实例
- Nginx配置文件nginx.conf中文详解(总结)
- Nginx 动态 upstreams 实现
- Nginx的常用命令(基本操作)
- [nginx] nginx + spawn-fcgi + wsapi/lua(搭建lua的web dev平台)
- Saltstack系列:Saltstack安装Nginx
- Nginx网站自定义404错误页面
- nginx location及rewrite的写法说明