php生成文字水印和圖片水印


生成文字水印
 
//文字水印
/*打開圖片*/
//1.配置圖片路徑
$src = "4.jpg";
//2.獲取圖片的信息(得到圖片的基本信息)
$info = getimagesize($src );
//3.通過獲取圖片類型
$type = image_type_to_extension($info[2],false);
//4.在內存中創建一個圖片類型一樣的圖像
$fun = "imagecreatefrom{$type}";
//5.圖片復制到內存中
$image = $fun($src);
/*操作圖片*/
//1.設置字體的路徑
$font = "STLITI.TTF";//c盤windows/fonts
//2.填寫水印內容
$content = '你好';
//3.設置字體的顏色rgb和透明度
$col = imagecolorallocatealpha($image,255,255,255,50);
//4.寫入文字
imagettftext($image,20,0,20,30,$col,$font,$content);
/*輸出圖片*/
//瀏覽器輸出
header("Content-type:{$info['mime']}");
$func = "image{$type}";
$func($image);
//保存圖片
$func($image,'newimage.'.$type);
/*銷毀圖片*/
imagedestroy($image);
生成圖片水印
$dst_path = '4.jpg';
$src_path = '1.png';
//創建圖片的實例
$dst = imagecreatefromstring(file_get_contents($dst_path));
$src = imagecreatefromstring(file_get_contents($src_path));
//獲取水印圖片的寬高
list($src_w, $src_h) = getimagesize($src_path);
//將水印圖片復制到目標圖片上,最后個參數80是設置透明度,這里實現半透明效果
imagecopymerge($dst, $src, 10, 10, 0, 0, $src_w, $src_h, 80);
//如果水印圖片本身帶透明色,則使用imagecopy方法
//imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h);
//輸出圖片
list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
switch ($dst_type) {
	case 1://GIF
		header('Content-Type: image/gif');
		imagegif($dst);
		break;
	case 2://JPG
		header('Content-Type: image/jpeg');
		imagejpeg($dst);
		break;
	case 3://PNG
		header('Content-Type: image/png');
		imagepng($dst);
		break;
	default:
		break;
}
imagedestroy($dst);
imagedestroy($src);

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM