php文字水印和php圖片水印實(shí)現(xiàn)代碼(二種加水印方法)
文字水印
文字水印就是在圖片上加上文字,主要使用gd庫(kù)的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, '快樂(lè)編程');
//輸出圖片
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庫(kù)的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)文章
解決yii2左側(cè)菜單子級(jí)無(wú)法高亮問(wèn)題的方法
這篇文章主要為大家詳細(xì)介紹了解決yii2左側(cè)菜單子級(jí)無(wú)法高亮問(wèn)題的方法,感興趣的朋友可以參考一下2016-05-05關(guān)于laravel5.5的定時(shí)任務(wù)詳解(demo)
今天小編就為大家分享一篇關(guān)于laravel5.5的定時(shí)任務(wù)詳解(demo),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10PHP二維關(guān)聯(lián)數(shù)組的遍歷方式(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇PHP二維關(guān)聯(lián)數(shù)組的遍歷方式(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10php實(shí)現(xiàn)文件編碼批量轉(zhuǎn)換
轉(zhuǎn)換文件編碼,比如原來(lái)是gbk,轉(zhuǎn)換成utf-8的,可以轉(zhuǎn)單個(gè)文件也可以轉(zhuǎn)換整個(gè)目錄的文件,可選是否遞歸目錄2014-03-03