一、圖片添加文字水印:php
$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);
效果:函數
二、圖片合成字體
$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);
函數註解:spa
imagecopymerge()code
imagecopymerge() 函數用於拷貝併合並圖像的一部分,成功返回 TRUE ,不然返回 FALSE 。blog
語法:圖片
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 )ci
參數說明:get
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 時,實際上什麼也沒作,反之徹底合併。string
效果圖: