首頁 - 開發(fā)工具
 收藏
搜索

php文字水印和圖片水印實現(xiàn)代碼gd庫


文字水印

$dst_path = 'dst.jpg';
//創(chuàng)建圖片的實例
$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);

圖片水印

$dst_path = 'dst.jpg';
$src_path = 'src.jpg';
//創(chuàng)建圖片的實例
$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)圖片上,最后個參數(shù)50是設(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);

參考文章:https://www.jb51.net/article/44829.htm

工具說明:

推薦工具:

工具標(biāo)簽:

開發(fā)工具