原來圖片服務器采用Windows .net架構,鑒于需求需要生成各種尺寸圖片。
流程說明:
用戶從Nginx請求對應的圖片,判斷是否存在_200x300的對應參數,如果沒有就直接請求到對應目錄的原圖,否則繼續判斷是否在本地已經生成了對應的緩存圖片,如果存在返回已經生成過的定制尺寸圖片,否則請求PHP動態生成。
Nginx部分配置:
????server?{listen???????80;server_name??pics.abc.com;location?/?{root???/var/www/html;index??index.html?index.htm?index.php;error_page???500?502?503?504??/50x.html;location?=?/50x.html?{root???html;}location?~?\_(\d+)x(\d+)\.(jpg|png|gif|jpeg|bmp)$?{??//判斷是否定制圖try_files?$uri?/temp/$uri?/get.php;????//判斷是否已生成過定制圖否則轉交給/get.phpexpires??????30d;}location?~?\.php$?{fastcgi_pass???127.0.0.1:9000;fastcgi_index??index.php;fastcgi_param??SCRIPT_FILENAME??$document_root$fastcgi_script_name;include????????fastcgi_params;}location?~?.*\.(gif|jpg|jpeg|png|bmp)${expires??????30d;}}}
在/var/www/html我們以只讀方式掛載Windows的目錄,修改/etc/fstab,添加
\\192.168.2.3\f$\pics.abc.com\pics?/var/www/html/pics/?cifs????ro,username=user,password=pass???1??2
然后重啟netfs服務,另外執行下面命令,安裝依賴的包
yum?-y?install?samba-client?cifs-utils service?netfs?restart chkconfig?netfs?on
生成的縮率圖會放到網站目錄的temp目錄下,如請求的http://pics.abc.com/pics/201604/29/abc_200x300.jpg
則生成的圖片放在temp/pics/201604/29/abc_200x300.jpg目錄下
PHP腳本:
function?thumb($src,?$width,?$height,?$filename,?$mode?=?'scale',?$quality?=?'100')?{
try?{$p_w_picpathValue?=?getp_w_picpathsize($src);$sourceWidth?=?$p_w_picpathValue[0];?//原圖寬$sourceHeight?=?$p_w_picpathValue[1];?//原圖高$thumbWidth?=?$width;?//縮略圖寬$thumbHeight?=?$height;?//縮略圖高$_x?=?0;$_y?=?0;$w?=?$sourceWidth;$h?=?$sourceHeight;if?($mode?==?'scale')?{if?($sourceWidth?<=?$thumbWidth?&&?$sourceHeight?<=?$thumbHeight)?{$_x?=?floor(($thumbWidth?-?$sourceWidth)?/?2);$_y?=?floor(($thumbHeight?-?$sourceHeight)?/?2);$thumbWidth?=?$sourceWidth;$thumbHeight?=?$sourceHeight;}?else?{if?($thumbHeight?*?$sourceWidth?>?$thumbWidth?*?$sourceHeight)?{$thumbHeight?=?floor($sourceHeight?*?$width?/?$sourceWidth);$_y?=?floor(($height?-?$thumbHeight)?/?2);}?else?{$thumbWidth?=?floor($sourceWidth?*?$height?/?$sourceHeight);$_x?=?floor(($width?-?$thumbWidth)?/?2);}}}?else?if?($mode?==?'crop')?{if?($sourceHeight?<?$thumbHeight)?{?//如果原圖尺寸小于當前尺寸?$thumbWidth?=?floor($thumbWidth?*?$sourceHeight?/?$thumbHeight);$thumbHeight?=?$sourceHeight;}if?($sourceWidth?<?$thumbWidth)?{$thumbHeight?=?floor($thumbHeight?*?$sourceWidth?/?$thumbWidth);$thumbWidth?=?$sourceWidth;}$s1?=?$sourceWidth?/?$sourceHeight;?//原圖比例$s2?=?$width?/?$height;?//新圖比例if?($s1?==?$s2)?{}?else?if?($s1?>?$s2)?{?//全高度?$y?=?0;$ax?=?floor($sourceWidth?*?($thumbHeight?/?$sourceHeight));$x?=?($ax?-?$thumbWidth)?/?2;$w?=?$thumbWidth?/?($thumbHeight?/?$sourceHeight);}?else?{?//全寬度?$x?=?0;$ay?=?floor($sourceHeight?*?($thumbWidth?/?$sourceWidth));?//模擬原圖比例高度$y?=?($ay?-?$thumbHeight)?/?2;$h?=?$thumbHeight?/?($thumbWidth?/?$sourceWidth);}}switch?($p_w_picpathValue[2])?{case?2:?$source?=?p_w_picpathcreatefromjpeg($src);break;case?1:?$source?=?p_w_picpathcreatefromgif($src);break;case?3:?$source?=?p_w_picpathcreatefrompng($src);break;case?6:?$source?=?p_w_picpathcreatefromwbmp($src);break;default:?defulat();return;}header("Content-type:?p_w_picpath/jpeg");$thumb?=?p_w_picpathcreatetruecolor($width,?$height);p_w_picpathfill($thumb,?0,?0,?p_w_picpathcolorallocate($thumb,?255,?255,?255));p_w_picpathcopyresampled($thumb,?$source,?0,?0,?$x,?$y,?$width,?$height,?$w,?$h);p_w_picpathjpeg($thumb,?null,?$quality);p_w_picpathjpeg($thumb,?$filename,?$quality);p_w_picpathdestroy($thumb);p_w_picpathdestroy($source);
}?catch?(Exception?$ex)?{defulat();}
}function?defulat()?{
/*$default_img?=?realpath('../pictures/nopic.gif');ob_start();header('Content-type:p_w_picpath/jpeg');readfile($default_img);ob_flush();flush();
*/
echo?'error';
}function?mkDirs($dir){if(!is_dir($dir)){if(!mkDirs(dirname($dir))){return?false;}if(!mkdir($dir,0755)){return?false;}}return?true;
}$uri=$_SERVER['REQUEST_URI'];
$p_w_picpath=basename($uri);$temp='./temp/'.dirname($uri).'/';
$imgpath='.'.dirname($uri).'/';/*
//檢查本地是否存在文件,原圖
if(file_exists($temp.$p_w_picpath)){ob_start();header('Content-type:p_w_picpath/jpeg');readfile($temp.$p_w_picpath);ob_flush();flush();exit();
}
*///檢查生成的圖片是否曾經生成過,存在即返回,否則重新生成新圖
if(!preg_match('/_(\d+)x(\d+)/',?$p_w_picpath,?$wh)){ob_start();header('Content-type:p_w_picpath/jpeg');readfile($imgpath.$p_w_picpath);ob_flush();flush();exit();
}$width?=?$wh[1];
$height?=?$wh[2];
$source_img=preg_replace('/_(\d+)x(\d+)/',?'',?$p_w_picpath);
//對長寬都超過的圖片返回原圖
if($width>=2000?||?$height>=2000){ob_start();header('Content-type:p_w_picpath/jpeg');readfile($imgpath.$source_img);ob_flush();flush();exit();
}//圖片處理
$src=$imgpath.$source_img;
$filename=$temp.$p_w_picpath;
mkDirs($temp);
//thumb(realpath($src),?$width,?$height,?$filename,?'crop',?'85');
thumb(realpath($src),?$width,?$height,?$filename,?'crop',?'100');
PHP生成尺寸部分參考<PHP圖片自動裁切應付不同尺寸的顯示>
如果上述服務器出現問題,則降低的Nginx配置為
????server?{listen???????80;server_name??localhost;access_log??logs/host.access.log??main;location?/?{rewrite?^/(.*)\_(\d+)x(\d+)\.(.*)$?/$host/$1?permanent;rewrite?^/([0-9a-zA-Z]+)/(.*)$?/$host/$1/$2?permanent;root???html;index??index.html?index.htm;}}
轉載于:https://blog.51cto.com/fengwan/1769043