2007年10月16日 星期二

GD縮圖範例

http://chuhsi.idv.tw/index_doc.html?cid=2&sid=50

//檢查檔案是否存在
if (file_exists($src) && isset($dest)) {

$destInfo = pathInfo($dest);
$srcSize = getImageSize($src); //圖檔大小
$srcRatio = $srcSize[0]/$srcSize[1]; // 計算寬/高
$destRatio = $maxWidth/$maxHeight;
if ($destRatio > $srcRatio) {
$destSize[1] = $maxHeight;
$destSize[0] = $maxHeight*$srcRatio;
}
else {
$destSize[0] = $maxWidth;
$destSize[1] = $maxWidth/$srcRatio;
}


//GIF 檔不支援輸出,因此將GIF轉成JPEG
if ($destInfo['extension'] == "gif") $dest = substr_replace($dest, 'jpg', -3);

//建立一個 True Color 的影像
$destImage = imageCreateTrueColor($destSize[0],$destSize[1]);

//根據副檔名讀取圖檔
switch ($srcSize[2]) {
case 1: $srcImage = imageCreateFromGif($src); break;
case 2: $srcImage = imageCreateFromJpeg($src); break;
case 3: $srcImage = imageCreateFromPng($src); break;
default: return false; break;
}

//取樣縮圖
ImageCopyResampled($destImage, $srcImage, 0, 0, 0, 0,$destSize[0],$destSize[1],
$srcSize[0],$srcSize[1]);

//輸出圖檔
switch ($srcSize[2]) {
case 1: case 2: imageJpeg($destImage,$dest,$quality); break;
case 3: imagePng($destImage,$dest); break;
}
return true;
}
else {
return false;
}
}
?>


http://www.phpdc.com/article/9/
PHP 已經內建了製作縮圖的函式,它是 imagecopyresized,以下是 imagecopyresized 的語法:

int imagecopyresized ( resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )

dst_image -- 輸出目標檔案
src_image -- 來源檔案
dst_x -- 目標檔案開始點的 x 座標
dst_y -- 目標檔案開始點的 y 座標
src_x -- 來源檔案開始點的 x 座標
src_y -- 來源檔案開始點的 y 座標
dst_w -- 目標檔案的長度
dst_h -- 目標檔案的高度
src_w -- 來源檔案的長度
src_h -- 來源檔案的高度

例如上傳的檔案是 $_FILES['pic'],而它是屬於 jpeg 圖案,縮圖的長及高不大於 100,那麼實作方法如下:

01 02 $src = imagecreatefromjpeg($_FILES['pic']['tmp_name']);
03
// get the source image's widht and hight
04
$src_w = imagesx($src);
05
$src_h = imagesy($src);
06
07
// assign thumbnail's widht and hight
08
if($src_w > $src_h){
09
$thumb_w = 100;
10
$thumb_h = intval($src_h / $src_w * 100);
11 }else{
12
$thumb_h = 100;
13
$thumb_w = intval($src_w / $src_h * 100);
14 }
15
16
// if you are using GD 1.6.x, please use imagecreate()
17
$thumb = imagecreatetruecolor($thumb_w, $thumb_h);
18
19
// start resize
20
imagecopyresized($thumb, $src, 0, 0, 0, 0, $thumb_w, $thumb_h, $src_w, $src_h);
21
22
23
// save thumbnail
24
imagejpeg($thumb, "/var/www/html/uploads/thumb/".$_FILES['pic']['name']);
25
?>


以上的程式大致上是先取得 $_FILES['pic'] 圖檔的長度及高度,然後再計算出縮圖的相應長度及高度,製作好縮圖後,最後將縮圖儲存到 /var/www/html/uploads/thumb/ 目錄下。


高精度圖片的質量以上製作縮圖的方法在處理一般圖檔是沒有問題,但當要處理的是高精度圖片,那麼造出的縮圖會很難看,與繪圖軟件造出的縮圖有很大程度上的 分別,要解決這個問題,可以使用在 php.net 上用戶貼出的一個函式,這個函式可以解決高精度圖片的問題,但換來的代價是處理的時間較慢:

01 02 function ImageResize (&$src, $x, $y) {
03
$dst=imagecreatetruecolor($x, $y);
04
$pals=ImageColorsTotal ($src);
05
06 for (
$i=0; $i<$pals; $i++) {
07
$colors=ImageColorsForIndex ($src, $i);
08
ImageColorAllocate ($dst, $colors['red'], $colors['green'], $colors['blue']);
09
10 }
11
$scX =(imagesx ($src)-1)/$x;
12
$scY =(imagesy ($src)-1)/$y;
13
$scX2 =intval($scX/2);
14
$scY2 =intval($scY/2);
15
16 for (
$j = 0; $j < ($y); $j++) {
17
$sY = intval($j * $scY);
18
$y13 = $sY + $scY2;
19 for (
$i = 0; $i < ($x); $i++) {
20
$sX = intval($i * $scX);
21
$x34 = $sX + $scX2;
22
$c1 = ImageColorsForIndex ($src, ImageColorAt ($src, $sX, $y13));
23
$c2 = ImageColorsForIndex ($src, ImageColorAt ($src, $sX, $sY));
24
$c3 = ImageColorsForIndex ($src, ImageColorAt ($src, $x34, $y13));
25
$c4 = ImageColorsForIndex ($src, ImageColorAt ($src, $x34, $sY));
26
$r = ($c1['red']+$c2['red']+$c3['red']+$c4['red'])/4;
27
$g = ($c1['green']+$c2['green']+$c3['green']+$c4['green'])/4;
28
$b = ($c1['blue']+$c2['blue']+$c3['blue']+$c4['blue'])/4;
29
ImageSetPixel ($dst, $i, $j, ImageColorClosest ($dst, $r, $g, $b));
30 }
31 }
32 return (
$dst);
33 }
34
?>


以上函式的用法相檔簡單,$src 是來源檔案,$x 是縮圖的長度,$y 是縮圖的高度,回傳的是縮圖。

沒有留言:

張貼留言