PHP 為圖片加上浮水印
avatar-img
天空

PHP 為圖片加上浮水印

更新於 發佈於 閱讀時間約 7 分鐘

使用 GD庫:

1.先確認 PHP GD lib 是否已安裝,利用 phpinfo() 頁面來檢查。

<?php
phpinfo();
?>

如果出現 GD Support "enabled",就是有啟用 GD lib了,

raw-image

如果沒有的話,請打開 php.ini 找到 ;extension=gd 把前面的 ; 拿掉,再重開網頁伺服器,php應該就可以支援 GD lib了。

raw-image

2.PHP 範例程式碼:

<?php
/* 原始圖片 */
$imagePath = "images/hat2_4982.jpg";
$outputPath = "images/hat2_4982_xxxxxx.jpg";
// 定義浮水印文字
$watermarkText1 = 'GD_Test';
$watermarkText2 = 'xxxxxx';

/* 用來加上浮水印的程式 */
function addWatermark($imagePath, $outputPath, $text1, $text2) {
    // 載入圖片
    $image = imagecreatefromjpeg($imagePath);
    $width = imagesx($image);
    $height = imagesy($image);
   
    // 設定字型顏色,使用半透明的白色
    $textColor = imagecolorallocatealpha($image, 255, 255, 255, 50);
// 255, 255, 255 為白色,100 是透明度
// 設定字型,這裡使用可用的字型
//$fontSize = 20; // 字型大小
// 根據圖片大小設定字型大小,這裡以圖片寬度的比例來設定字型大小
$fontSize = min($width, $height) / 20; // 例如,字型大小設為圖片最小邊長的1/20
$fontFile = 'fonts/Arial.ttf'; // 字型檔案路徑
// 計算字型的高寬,以便安排行距
$bbox1 = imagettfbbox($fontSize, 0, $fontFile, $text1);
$bbox2 = imagettfbbox($fontSize, 0, $fontFile, $text2);
$lineHeight = max($bbox1[1] - $bbox1[7], $bbox2[1] - $bbox2[7]) + 10; // 加入一些間隔
// 設定浮水印在圖片的四個角落的位置
$positions = [
        [10, 1.5*$lineHeight], // 左上角
        [$width - 130, 1.5*$lineHeight], // 右上角
        [10, $height - 1.5*$lineHeight], // 左下角
        [$width - 130, $height - 1.5*$lineHeight] // 右下角
  ];
// 寫入浮水印文字到圖片
foreach ($positions as $index => $position) {
  // 在每個角落寫入 第一行文字
imagettftext($image, $fontSize, 0, $position[0], $position[1], $textColor, $fontFile, $text1);
// 偏移位置以顯示 第二行文字
      imagettftext($image, $fontSize, 0, $position[0], $position[1] + $lineHeight, $textColor, $fontFile, $text2);
}
// 保存加了浮水印的圖片
imagejpeg($image, $outputPath);
imagedestroy($image);
}
addWatermark($imagePath, $outputPath, $watermarkText1, $watermarkText2);
?>
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8">
<title>顯示圖片</title>
<style>
.image-container {
            width: 640px; /* 設置容器寬度 */
            height: 480px; /* 設置容器高度 */
            background-image: url('<?php echo $imagePath; ?>');
            background-size: cover; /* 覆蓋整個容器 */
}
.image-container2 {
            width: 640px; /* 設置容器寬度 */
            height: 480px; /* 設置容器高度 */
            background-image: url('<?php echo $outputPath; ?>');
            background-size: cover; /* 覆蓋整個容器 */
}
</style>
</head>
<body>
<div>
<img src="<?php echo $imagePath; ?>"></img>
</div>
<div>
<img src="<?php echo $outputPath; ?>"></img>
</div>
</body>
</html>

3.結果如下圖:

raw-image


avatar-img
天空
1會員
20內容數
如果可以無所事事的放空,那才是真的幸福?
留言
avatar-img
留言分享你的想法!
天空 的其他內容
使用 ldap3 2.9.1 pip install ldap3​ python: from ldap3 import Server, Connection, SIMPLE, SYNC, ALL import pandas as p d import json # LDAP 伺服器地址
最近看了一本書「29歲開始做,43歲提早退休…」 書中的大意是, 作者由29歲起,每月將收入的50%都存起來投資基金和買保險(養老險), 到了43歲時因職場上的不順遂,毅然決定提前退休。 書中並沒有詳細指出作者是如何「投資基金和買保險」來建立被動收入, 本書的內容比較多的是描述作者的退休生
什麼是 Active Directory? Active Directory (AD)是企業經常用來做電腦登入認證的系統服務,透過 AD 伺服器 IT管理員能夠用來儲存 使用者(User)、使用者群組(Group)和電腦(Computer) …等的相關資訊以進行認證與網域存取管理。 如何
使用 ldap3 2.9.1 pip install ldap3​ python: from ldap3 import Server, Connection, SIMPLE, SYNC, ALL import pandas as p d import json # LDAP 伺服器地址
最近看了一本書「29歲開始做,43歲提早退休…」 書中的大意是, 作者由29歲起,每月將收入的50%都存起來投資基金和買保險(養老險), 到了43歲時因職場上的不順遂,毅然決定提前退休。 書中並沒有詳細指出作者是如何「投資基金和買保險」來建立被動收入, 本書的內容比較多的是描述作者的退休生
什麼是 Active Directory? Active Directory (AD)是企業經常用來做電腦登入認證的系統服務,透過 AD 伺服器 IT管理員能夠用來儲存 使用者(User)、使用者群組(Group)和電腦(Computer) …等的相關資訊以進行認證與網域存取管理。 如何