針對常見的圖片後台管理需求——讀取屬性、套用濾鏡、產生縮圖、加浮水印——本文提供一個 image 類別,封裝 GD 底層操作,採用方法鏈風格,程式碼乾淨易讀,適合直接納入專案或作為教學範本。
class image {
public $filename; // 檔案路徑
private $info; // getimagesize() 回傳陣列
private $im; // GD 圖片資源
public function __construct($filename) {
$this->filename = $filename;
$this->info = @getimagesize($filename);
if ($this->info[2] > 3) { exit("只支援 gif、jpeg、png 格式。"); }
}
// 載入圖片資源
public function imgload() {
switch ($this->info[2]) {
case 1: $this->im = @imagecreatefromgif($this->filename); break;
case 2: $this->im = @imagecreatefromjpeg($this->filename); break;
case 3: $this->im = @imagecreatefrompng($this->filename); break;
}
return $this; // 支援鏈式
}
// 儲存圖片
public function imagesave($img, $savename, $type) {
switch ($type) {
case 1: imagegif($img, $savename); break;
case 2: imagejpeg($img, $savename); break;
case 3: imagepng($img, $savename); break;
}
return $this;
}
// 濾鏡效果
public function filter($arg=1, $savename='') {
$this->imgload();
if (!$savename) $savename = 'f_'.$this->filename;
$ok = false;
switch ($arg) {
case 1: imagefilter($this->im, IMG_FILTER_NEGATE); $ok=true; break; // 反色
case 2: imagefilter($this->im, IMG_FILTER_GRAYSCALE); $ok=true; break; // 灰階
case 3: imagefilter($this->im, IMG_FILTER_EMBOSS); $ok=true; break; // 浮雕
case 4: imagefilter($this->im, IMG_FILTER_GAUSSIAN_BLUR); $ok=true; break; // 高斯模糊
case 5: imagefilter($this->im, IMG_FILTER_BRIGHTNESS, 50); $ok=true; break; // 亮度 +50
case 6: imagefilter($this->im, IMG_FILTER_CONTRAST, -50); $ok=true; break; // 對比度 -50
}
if ($ok) {
$this->imagesave($this->im, $savename, $this->info[2]);
imagedestroy($this->im);
return true;
}
imagedestroy($this->im);
return false;
}
// ... 後續可擴充 thumbnail(), watermark(), rotate() 等
}
| 參數 $arg | GD 常數 | 效果 | 額外參數 |
|---|---|---|---|
| 1 | IMG_FILTER_NEGATE | 反色 (負片) | 無 |
| 2 | IMG_FILTER_GRAYSCALE | 灰階 | 無 |
| 3 | IMG_FILTER_EMBOSS | 浮雕 | 無 |
| 4 | IMG_FILTER_GAUSSIAN_BLUR | 高斯模糊 | 無 |
| 5 | IMG_FILTER_BRIGHTNESS | 亮度調整 | -255 ~ 255 |
| 6 | IMG_FILTER_CONTRAST | 對比度調整 | -100 ~ 100 |
| 7* | IMG_FILTER_COLORIZE | 著色 | R,G,B,Alpha |
| 8* | IMG_FILTER_EDGEDETECT | 邊緣偵測 | 無 |
| 9* | IMG_FILTER_SELECTIVE_BLUR | 選擇性模糊 | 無 |
| 10* | IMG_FILTER_MEAN_REMOVAL | 均值移除 (素描感) | 無 |
| 11* | IMG_FILTER_SMOOTH | 平滑 | 權重 (預設 6) |
| 12* | IMG_FILTER_PIXELATE | 像素化 | 區塊大小, 模式 |
* 原類別未包含,建議自行擴充。
require_once 'image.php';
// 1. 灰階濾鏡並存新檔
$img = new image('uploads/photo.jpg');
$img->filter(2, 'uploads/photo_gray.jpg');
// 2. 亮度 +30 並直接輸出瀏覽器
$img2 = new image('uploads/photo.jpg');
$img2->imgload();
imagefilter($img2->im, IMG_FILTER_BRIGHTNESS, 30);
header('Content-Type: image/jpeg');
imagejpeg($img2->im);
imagedestroy($img2->im);
// 3. 串聯操作:載入 → 灰階 → 高斯模糊 → 存檔
// (需修改 filter() 回傳 $this 並分離存檔邏輯)
$img3 = new image('uploads/photo.jpg');
$img3->imgload();
imagefilter($img3->im, IMG_FILTER_GRAYSCALE);
imagefilter($img3->im, IMG_FILTER_GAUSSIAN_BLUR);
imagejpeg($img3->im, 'uploads/photo_gray_blur.jpg');
imagedestroy($img3->im);
// 等比縮圖
public function thumbnail($maxW, $maxH, $savePath='') {
$this->imgload();
$srcW = imagesx($this->im);
$srcH = imagesy($this->im);
$ratio = min($maxW/$srcW, $maxH/$srcH);
$newW = (int)($srcW * $ratio);
$newH = (int)($srcH * $ratio);
$thumb = imagecreatetruecolor($newW, $newH);
imagecopyresampled($thumb, $this->im, 0,0,0,0, $newW,$newH, $srcW,$srcH);
if ($savePath) $this->imagesave($thumb, $savePath, $this->info[2]);
imagedestroy($thumb);
return $this;
}
// 文字浮水印
public function textWatermark($text, $fontPath, $size=20, $color=[255,255,255], $opacity=50, $margin=10) {
$this->imgload();
$rgb = imagecolorallocatealpha($this->im, $color[0],$color[1],$color[2], $opacity);
$box = imagettfbbox($size, 0, $fontPath, $text);
$tw = $box[2]-$box[0]; $th = $box[1]-$box[7];
$x = imagesx($this->im) - $tw - $margin;
$y = imagesy($this->im) - $margin;
imagettftext($this->im, $size, 0, $x, $y, $rgb, $fontPath, $text);
return $this;
}
ImageInterface,支援 GD/Imagick 雙引擎切換。$img->pipe([new Grayscale, new Blur(5), new Watermark('logo.png')]) 批次處理。image 類別以精簡程式碼展示 GD 核心操作流程:載入 → 處理 → 儲存。濾鏡部分完整對應 GD 內建常數,極易擴充。若專案需求較複雜,建議直接採用 Intervention Image 或 Spatie/Image,獲得更完整的功能與型別安全。