php文字水印和php图片水印实现代码(二种加水印方法)

前端技术 2023/09/02 PHP

文字水印

文字水印就是在图片上加上文字,主要使用gd库的imagefttext方法,并且需要字体文件。效果图如下:

实现代码如下:

复制代码 代码如下:

$dst_path = \'dst.jpg\';

//创建图片的实例
$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);

本文地址:https://www.stayed.cn/item/6026

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。