1、圖片添加文字水印:
$bigImgPath = 'backgroud.png';
$img = imagecreatefromstring(file_get_contents($bigImgPath));
$font = 'msyhl.ttc';//字體
$black = imagecolorallocate($img, 0, 0, 0);//字體顏色 RGB
$fontSize = 20; //字體大小
$circleSize = 60; //旋轉角度
$left = 50; //左邊距
$top = 150; //頂邊距
imagefttext($img, $fontSize, $circleSize, $left, $top, $black, $font, 'Rhythmk| 坤');
list($bgWidth, $bgHight, $bgType) = getimagesize($bigImgPath);
switch ($bgType) {
case 1: //gif
header('Content-Type:image/gif');
imagegif($img);
break;
case 2: //jpg
header('Content-Type:image/jpg');
imagejpeg($img);
break;
case 3: //jpg
header('Content-Type:image/png');
imagepng($img);
break;
default:
break;
}
imagedestroy($img);
效果:

2、圖片合成
$bigImgPath = 'backgroud.png';
$qCodePath = 'qcode.png';
$bigImg = imagecreatefromstring(file_get_contents($bigImgPath));
$qCodeImg = imagecreatefromstring(file_get_contents($qCodePath));
list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($qCodePath);
// imagecopymerge使用注解
imagecopymerge($bigImg, $qCodeImg, 200, 300, 0, 0, $qCodeWidth, $qCodeHight, 100);
list($bigWidth, $bigHight, $bigType) = getimagesize($bigImgPath);
switch ($bigType) {
case 1: //gif
header('Content-Type:image/gif');
imagegif($bigImg);
break;
case 2: //jpg
header('Content-Type:image/jpg');
imagejpeg($bigImg);
break;
case 3: //jpg
header('Content-Type:image/png');
imagepng($bigImg);
break;
default:
# code...
break;
}
imagedestroy($bigImg);
imagedestroy($qcodeImg);
函數注解:
imagecopymerge()
imagecopymerge() 函數用於拷貝並合並圖像的一部分,成功返回 TRUE ,否則返回 FALSE 。
語法:
bool imagecopymerge( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h, int pct )
參數說明:
dst_im 目標圖像
src_im 被拷貝的源圖像
dst_x 目標圖像開始 x 坐標
dst_y 目標圖像開始 y 坐標,x,y同為 0 則從左上角開始
src_x 拷貝圖像開始 x 坐標
src_y 拷貝圖像開始 y 坐標,x,y同為 0 則從左上角開始拷貝
src_w (從 src_x 開始)拷貝的寬度
src_h (從 src_y 開始)拷貝的高度
pct 圖像合並程度,取值 0-100 ,當 pct=0 時,實際上什么也沒做,反之完全合並。
效果圖:

