# GD 繪制水印
## 文字水印
```
<?php
/**
* @param string $file_name 文件名
* @param string $font_file 字體文件
* @param string $text 水印文字
* @param string $dest_path 水印保存地址
* @param string $prefix 保存文件前綴
* @param int $r 水印顏色 Red
* @param int $g 水印顏色 Green
* @param int $b 水印顏色 Blue
* @param int $alpha 水印顏色 Alpha
* @param int $angle 水印角度
* @param int $x 水印所在圖片 x 位置
* @param int $y 水印所在圖片 y 位置
* @param int $size 水印文字大小
* @param bool $delete_source 是否刪除源文件
* @return string
*/
function water_text($file_name, $font_file, $text = 'webfad', $dest_path = 'watertext', $prefix = 'watertext_', $r = 255, $g = 0, $b = 0, $alpha = 80, $angle = 0, $x = 0, $y = 30, $size = 30,$delete_source = false)
{
$file_info = getImageInfo($file_name);
$image = $file_info['create_fun']($file_name);
$color = imagecolorallocatealpha($image, $r, $g, $b, $alpha);
imagettftext($image, $size, $angle, $x, $y, $color, $font_file, $text);
if ($dest_path && !(file_exists($dest_path))) {
mkdir($dest_path, 0777, true);
}
$dest_name = "{$prefix}{$file_info['file_name']}" . $file_info['extension'];
$destination = $dest_path ? $dest_path . "/" . $dest_name : $dest_name;
$file_info['out_fun']($image, $destination);
if($delete_source) {
@unlink($file_name);
}
imagedestroy($image);
return $destination;
}
/**
* @param $filename string 文件名
* @return array
*/
function getImageInfo($filename)
{
if (!$info = getimagesize($filename)) {
exit('文件不是真實圖片!');
}
$file_info['file_name'] = pathinfo($filename)['filename'];
$file_info['width'] = $info[0];
$file_info['height'] = $info[1];
$mime = image_type_to_mime_type($info[2]);
$file_info['extension'] = image_type_to_extension($info[2], true);
$file_info['create_fun'] = str_replace('/', 'createfrom', $mime);
$file_info['out_fun'] = str_replace('/', '', $mime);
return $file_info;
}
```
調用示例
```
water_text('./images/image.jpg', './fonts/PingFang.ttc');
```
## 圖片水印
簡單 `imagecopymerge()` 函數使用。
```
<?php
$logo = 'images/logo.png';
$file_name = 'images/jd.jpg';
$dst_image = imagecreatefromjpeg($file_name);
$src_image = imagecreatefrompng($logo);
imagecopymerge($dst_image, $src_image, 0, 0, 0, 0, 190, 170, 60);
header('Content-Type: image/jpg');
imagejpeg($dst_image);
imagedestroy($dst_image);
imagedestroy($src_image);
```
- 開始
- PHP配置參數的介紹
- PHP代碼優化
- php中的命名空間
- PHP文件上傳類
- PHP文件下載
- PHP驗證碼
- ThinkPHP3.2 框架函數
- A函數:實例化控制器
- C函數:設置和獲取配置參數
- D函數:實例化模型
- F 函數:快速緩存設置和存取
- M函數:例化模型(無需定義模型類)
- L函數:設置和獲取語言變量
- S 函數:緩存設置和存取
- R函數:直接調用控制器的操作方法
- U函數:URL地址生成
- I 函數:安全獲取系統輸入變量
- 日志
- ThinkPHP在關閉調試模式導致函數被緩存
- MySQL觸發器使用時遇到的坑
- PHP常用函數
- 五一回家記錄
- window的PHP開發(wamp)下安裝redis擴展
- Windows下安裝使用Redis
- PHP7新特性
- 利用 phpmailer 類實現隊列發送郵件
- GD 庫圖像處理
- 檢測 PHP 模塊是否開啟
- GD 庫操作一般步驟
- GD 庫繪畫改變字體
- GD 繪制驗證碼
- GD 縮略圖實現
- GD 繪制水印
- 日期時間函數庫
- PHP 函數
- 無限極分類