php文字水印和php圖片水印實(shí)現(xiàn)代碼(二種加水印方法)
文字水印
文字水印就是在圖片上加上文字,主要使用gd庫的imagefttext方法,并且需要字體文件。效果圖如下:
實(shí)現(xiàn)代碼如下:
$dst_path = 'dst.jpg';
//創(chuàng)建圖片的實(shí)例
$dst = imagecreatefromstring(file_get_contents($dst_path));
//打上文字
$font = './simsun.ttc';//字體
$black = imagecolorallocate($dst, 0x00, 0x00, 0x00);//字體顏色
imagefttext($dst, 13, 0, 20, 20, $black, $font, '快樂編程');
//輸出圖片
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);
圖片水印
圖片水印就是將一張圖片加在另外一張圖片上,主要使用gd庫的imagecopy和imagecopymerge。效果圖如下:
實(shí)現(xiàn)代碼如下:
$dst_path = 'dst.jpg';
$src_path = 'src.jpg';
//創(chuàng)建圖片的實(shí)例
$dst = imagecreatefromstring(file_get_contents($dst_path));
$src = imagecreatefromstring(file_get_contents($src_path));
//獲取水印圖片的寬高
list($src_w, $src_h) = getimagesize($src_path);
//將水印圖片復(fù)制到目標(biāo)圖片上,最后個(gè)參數(shù)50是設(shè)置透明度,這里實(shí)現(xiàn)半透明效果
imagecopymerge($dst, $src, 10, 10, 0, 0, $src_w, $src_h, 50);
//如果水印圖片本身帶透明色,則使用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);
相關(guān)文章
php workerman定時(shí)任務(wù)的實(shí)現(xiàn)代碼
這篇文章主要介紹了php workerman定時(shí)任務(wù)的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
PHP經(jīng)典面試題之設(shè)計(jì)模式(經(jīng)常遇到)
php中設(shè)計(jì)模式非常多,但是設(shè)計(jì)模式在php面試題經(jīng)常會(huì)提到,本文主要給大家介紹php經(jīng)典面試題之設(shè)計(jì)模式,需要的朋友一起看看吧2015-10-10
Laravel第三方包報(bào)class not found的解決方法
今天小編就為大家分享一篇Laravel第三方包報(bào)class not found的解決方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
PHP動(dòng)態(tài)輸出JavaScript代碼實(shí)例
這篇文章主要介紹了PHP動(dòng)態(tài)輸出JS代碼實(shí)例,本文直接給出代碼實(shí)例,一看就懂,簡潔明了,需要的朋友可以參考下2015-02-02

