使用 GD 函式庫即時生成帶圓角效果的圖片,支援四個角獨立控制、背景色自定、多格式輸入 (JPEG/PNG/GIF)、PNG 輸出保持透明度。
<?php
$image_file = $_GET['src'];
$corner_radius = isset($_GET['radius']) ? $_GET['radius'] : 20; // 預設 20px
$topleft = (isset($_GET['topleft']) && $_GET['topleft'] == "no") ? false : true;
$bottomleft = (isset($_GET['bottomleft']) && $_GET['bottomleft'] == "no") ? false : true;
$bottomright = (isset($_GET['bottomright']) && $_GET['bottomright'] == "no") ? false : true;
$topright = (isset($_GET['topright']) && $_GET['topright'] == "no") ? false : true;
$imagetype = strtolower($_GET['imagetype']);
$backcolor = $_GET['backcolor']; // 十六進位 RGB,如 FFFFFF
$forecolor = $_GET['forecolor']; // 注意:原碼未定義此變數,需補充
$endsize = $corner_radius;
$startsize = $endsize * 3 - 1;
$arcsize = $startsize * 2 + 1;
// 載入源圖片
if (in_array($imagetype, ['jpeg', 'jpg'])) {
$image = imagecreatefromjpeg($image_file);
} elseif ($imagetype == 'gif') {
$image = imagecreatefromgif($image_file);
} else {
$image = imagecreatefrompng($image_file);
}
$size = getimagesize($image_file);
// 建立 2x 放大畫布以獲得更平滑圓角
$background = imagecreatetruecolor($size[0], $size[1]);
imagecopymerge($background, $image, 0, 0, 0, 0, $size[0], $size[1], 100);
$startx = $size[0] * 2 - 1;
$starty = $size[1] * 2 - 1;
$im_temp = imagecreatetruecolor($startx, $starty);
imagecopyresampled($im_temp, $background, 0, 0, 0, 0, $startx, $starty, $size[0], $size[1]);
// 顏色分配
$bg = imagecolorallocate($im_temp,
hexdec(substr($backcolor, 0, 2)),
hexdec(substr($backcolor, 2, 2)),
hexdec(substr($backcolor, 4, 2))
);
$fg = imagecolorallocate($im_temp,
hexdec(substr($forecolor, 0, 2)),
hexdec(substr($forecolor, 2, 2)),
hexdec(substr($forecolor, 4, 2))
);
// 繪製四個圓角(扇形填充)
if ($topleft) {
imagearc($im_temp, $startsize, $startsize, $arcsize, $arcsize, 180, 270, $bg);
imagefilltoborder($im_temp, 0, 0, $bg, $bg);
}
if ($bottomleft) {
imagearc($im_temp, $startsize, $starty - $startsize, $arcsize, $arcsize, 90, 180, $bg);
imagefilltoborder($im_temp, 0, $starty, $bg, $bg);
}
if ($bottomright) {
imagearc($im_temp, $startx - $startsize, $starty - $startsize, $arcsize, $arcsize, 0, 90, $bg);
imagefilltoborder($im_temp, $startx, $starty, $bg, $bg);
}
if ($topright) {
imagearc($im_temp, $startx - $startsize, $startsize, $arcsize, $arcsize, 270, 360, $bg);
imagefilltoborder($im_temp, $startx, 0, $bg, $bg);
}
// 縮回原尺寸並輸出
$newimage = imagecreatetruecolor($size[0], $size[1]);
imagecopyresampled($image, $im_temp, 0, 0, 0, 0, $size[0], $size[1], $startx, $starty);
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
imagedestroy($background);
imagedestroy($im_temp);
?>
| 參數 | 必要 | 預設 | 說明 |
|---|---|---|---|
| src | 是 | - | 源圖片 URL |
| radius | 否 | 20 | 圓角半徑 (px) |
| topleft/bottomleft/bottomright/topright | 否 | yes | 設為 "no" 關閉該角圓角 |
| imagetype | 是 | - | jpeg/jpg/gif/png |
| backcolor | 是 | - | 背景色十六進位 RRGGBB |
| forecolor | 是 | - | 前景色十六進位 RRGGBB(原碼遺漏) |
forecolor 參數獲取邏輯imagesavealpha + imagealphablending)imagefilledarc 簡化填充邏輯此腳本可直接部署作為圖片處理微服務,配合 CDN 快取極具擴展性。