高品質縮圖的產生函數(多種剪下模式,按高度寬度最佳縮放等)
來源:互聯網
上載者:User
函數|縮圖 /**
* 可擴充的縮圖產生函數
* 在http://yodoo.com的論壇裡可以獲得最新版本(註冊使用者)
* 示範效果也請登入http://yodoo.com看看,該站所有的縮圖(jpg,png)都是使用該函數產生的
*
* 轉載請保留完整資訊
*
* @author Austin Chin <austinfay@hotmail.com> http://yodoo.com
* @version $Revision: 1.7 $
*
*
* version
*
* + 表示增加的功能
* - 表示丟棄的功能
* C 表示修正的功能
* E 表示擴充了功能
*
* v1.5
* makeThumb($srcFile, $dstFile, $dstW, $dstH, $option=1)
*
* v1.6
* + 增加了剪下模式
* + $option 8: 寬度最佳縮放
* + $option 16: 高度最佳縮放
* makeThumb($srcFile, $dstFile, $dstW, $dstH, $option=1, $cutmode=0, $startX=0,
* $startY=0)
*
* v1.7
* E 傳回值改為數組,第一個元素是代碼 0 表示正常, 其它位錯誤碼;第二個元素是錯誤描述。
* 錯誤碼:
* -1 源檔案不存在。
* -2 不支援的圖片輸出函數
* -3 不支援的圖片建立函數
* -4 HTTP頭資訊已經輸出,無法向瀏覽器輸出圖片。
* -5 無法檢測輸出的圖片類型
* + 增加函數 message2Image 可以把字串輸出成圖片格式
*/
/**
* 可擴充的縮圖產生函數
*
* @param string $srcFile 源檔案
* @param string $srcFile 目標檔案
* @param int $dstW 靶心圖表片的寬度(單位:像素)
* @param int $dstH 靶心圖表片的高度(單位:像素)
* @param int $option 附加參數,可以相加使用,如1+2(或者 1|2)表示同時執行1和2的功能。
* 1: 預設,輸出到指定檔案 2: 圖片內容輸出到瀏覽器 4: 不保持圖片比例
* 8:寬度最佳縮放 16:高度最佳縮放
* @param int $cutmode 剪下模式 0: 預設模式,剪下模式 1: 左或上 2: 中 3: 右或下
* @param int $startX 剪下的起始橫座標(像素)
* @param int $startY 剪下的起始縱座標(像素)
* @return array return[0]=0: 正常; return[0] 為錯誤碼 return[1] string: 錯誤描述
*/
define(OP_TO_FILE, 1); //輸出到指定檔案
define(OP_OUTPUT, 2); //圖片內容輸出到瀏覽器
define(OP_NOT_KEEP_SCALE, 4); //不保持圖片比例, 即使用展開
define(OP_BEST_RESIZE_WIDTH, 8); //寬度最佳縮放
define(OP_BEST_RESIZE_HEIGHT, 16); //高度最佳縮放
define(CM_DEFAULT, 0); // 預設模式
define(CM_LEFT_OR_TOP, 1); // 左或上
define(CM_MIDDLE, 2); // 中
define(CM_RIGHT_OR_BOTTOM, 3); // 右或下
function makeThumb($srcFile, $dstFile, $dstW, $dstH, $option=OP_TO_FILE,
$cutmode=CM_DEFAULT, $startX=0, $startY=0) {
$img_type = array(1=>"gif", 2=>"jpeg", 3=>"png");
$type_idx = array("gif"=>1, "jpg"=>2, "jpeg"=>2, "jpe"=>2, "png"=>3);
if (!file_exists($srcFile)) {
return array(-1, "Source file not exists: $srcFile.");
}
$path_parts = @pathinfo($dstFile);
$ext = strtolower ($path_parts["extension"]);
if ($ext == "") {
return array(-5, "Can't detect output image's type.");
}
$func_output = "image" . $img_type[$type_idx[$ext]];
if (!function_exists ($func_output)) {
return array(-2, "Function not exists for output:$func_output.");
}
$data = @GetImageSize($srcFile);
$func_create = "imagecreatefrom" . $img_type[$data[2]];
if (!function_exists ($func_create)) {
return array(-3, "Function not exists for create:$func_create.");
}
$im = @$func_create($srcFile);
$srcW = @ImageSX($im);
$srcH = @ImageSY($im);
$srcX = 0;
$srcY = 0;
$dstX = 0;
$dstY = 0;
if ($option & OP_BEST_RESIZE_WIDTH) {
$dstH = round($dstW * $srcH / $srcW);
}
if ($option & OP_BEST_RESIZE_HEIGHT) {
$dstW = round($dstH * $srcW / $srcH);
}
$fdstW = $dstW;
$fdstH = $dstH;
if ($cutmode != CM_DEFAULT) { // 剪下模式 1: 左或上 2: 中 3: 右或下
$srcW -= $startX;
$srcH -= $startY;
if ($srcW*$dstH > $srcH*$dstW) {
$testW = round($dstW * $srcH / $dstH);
$testH = $srcH;
} else {
$testH = round($dstH * $srcW / $dstW);
$testW = $srcW;
}
switch ($cutmode) {
case CM_LEFT_OR_TOP: $srcX = 0; $srcY = 0; break;
case CM_MIDDLE: $srcX = round(($srcW - $testW) / 2);
$srcY = round(($srcH - $testH) / 2); break;
case CM_RIGHT_OR_BOTTOM: $srcX = $srcW - $testW;
$srcY = $srcH - $testH;
}
$srcW = $testW;
$srcH = $testH;
$srcX += $startX;
$srcY += $startY;
} else { // 原始縮放
if (!($option & OP_NOT_KEEP_SCALE)) {
// 以下代碼計算新大小,並保持圖片比例
if ($srcW*$dstH>$srcH*$dstW) {
$fdstH=round($srcH*$dstW/$srcW);
$dstY=floor(($dstH-$fdstH)/2);
$fdstW=$dstW;
} else {
$fdstW=round($srcW*$dstH/$srcH);
$dstX=floor(($dstW-$fdstW)/2);
$fdstH=$dstH;
}
$dstX=($dstX<0)?0:$dstX;
$dstY=($dstX<0)?0:$dstY;
$dstX=($dstX>($dstW/2))?floor($dstW/2):$dstX;
$dstY=($dstY>($dstH/2))?floor($dstH/s):$dstY;
}
} /// end if ($cutmode != CM_DEFAULT) { // 剪下模式
if( function_exists("imagecopyresampled") and
function_exists("imagecreatetruecolor") ){
$func_create = "imagecreatetruecolor";
$func_resize = "imagecopyresampled";
} else {
$func_create = "imagecreate";
$func_resize = "imagecopyresized";
}
$newim = @$func_create($dstW,$dstH);
$black = @ImageColorAllocate($newim, 0,0,0);
$back = @imagecolortransparent($newim, $black);
@imagefilledrectangle($newim,0,0,$dstW,$dstH,$black);
@$func_resize($newim,$im,$dstX,$dstY,$srcX,$srcY,$fdstW,$fdstH,$srcW,$srcH);
if ($option & OP_TO_FILE) {
@$func_output($newim,$dstFile);
}
if ($option & OP_OUTPUT) {
if (function_exists("headers_sent")) {
if (headers_sent()) {
return array(-4, "HTTP already sent, can't output image to browser.");
}
}
header("Content-type: image/" . $img_type[$type_idx[$ext]]);
@$func_output($newim);
}
@imagedestroy($im);
@imagedestroy($newim);
return array(0, "OK");
}