时光网:
http://img21.mtime.cn/CMS/Gallery/2011/07/02/170837.97014036_160X160.jpg
http://img21.mtime.cn/CMS/Gallery/2011/07/02/170837.97014036_362X571.jpg
http://img21.mtime.cn/CMS/Gallery/2011/07/02/170837.97014036_81X27.jpg
Google+
使用URLRewrite+实时处理+缓存
淘宝的图片就是使用这种方法来处理的。根据原图使用graphmagick去生成缩略图
简要得代码
nginx rewrite:
rewrite ^/upload/(.+)_(\d+)_(\d+)\.(jpg|png|gif) /upload/thumb.php?src=$1&w=$2&h=$3&type=$4;
$src= $_REQUEST["src"];
$width = $_REQUEST["w"];
$height = $_REQUEST["h"];
$type = strtolower($_REQUEST["type"]);
$filename = $src.".".$width."x".$height.".".$type;
if(!file_exists($filename))
{
$im = new Imagick();
$im->readImage($src);
if($im->getImageWidth() >=$im->getImageHeight())
{
$im->cropThumbnailImage( $width, $height );
}else
{
$im->cropImage($im->getImageWidth(),$im->getImageWidth(),0,0);
$im->cropThumbnailImage($width,$height);
}
$im->writeImage($filename);
$im->destroy();
}
$contenttype = "";
switch($type)
{
case "jpg":$contenttype = "jpeg";break;
case "jpeg":$contenttype = "jpeg";break;
case "png":$contenttype = "png";break;
case "gif":$contenttype = "gif";break;
}
header("Content-type:image/".$contenttype);
readfile($filename);
.htaccess url指向image.php
url初始为 image.php
GET过来的url,
虽然url的结尾是.jpg,但它指向的是一个程序而不是静态图片,该程序根据url的参数返回图片.
Rewrite+实时处理+缓存,前段时间小组刚做了个图片站点,使用的就是这种方式,流量不高所以压力也不大。具体实现是Nginx+使用GM做成的Nginx模块,Nginx提供URLRrwrite+缓存,GM模块提供缩放和加水印。站点是这个:
http://pic.hatrix.org/random
http://pic.hatrix.org/random一开始是用IM,后来听淘宝的同学讲GM性能更好,便换成GM喽。