首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP GD 生成圓角圖片完整實作

     編輯:PHP 相關知識

PHP GD 生成圓角圖片完整實作

使用 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);
?>

URL 參數說明

參數必要預設說明
src是-源圖片 URL
radius否20圓角半徑 (px)
topleft/bottomleft/bottomright/topright否yes設為 "no" 關閉該角圓角
imagetype是-jpeg/jpg/gif/png
backcolor是-背景色十六進位 RRGGBB
forecolor是-前景色十六進位 RRGGBB(原碼遺漏)

技術要點

  • 2x 超採樣:先放大 2 倍繪製圓角,再縮回原尺寸,有效消除鋸齒
  • imagearc + imagefilltoborder:繪製扇形邊界並填充背景色實現圓角切割
  • 即時輸出:直接輸出 PNG 流,適合 `` 嵌入

改進建議

  1. 補充 forecolor 參數獲取邏輯
  2. 加入輸入驗證與錯誤處理
  3. 支援透明背景 (imagesavealpha + imagealphablending)
  4. 加入快取機制 (ETag/Last-Modified) 避免重複運算
  5. 考慮改用 imagefilledarc 簡化填充邏輯

此腳本可直接部署作為圖片處理微服務,配合 CDN 快取極具擴展性。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved