php 生成文字水印,php生成图片及文字水印

场景:网站统计代码,在用户网站是,根据一个字段值,显示图片或文字水印图片icon

public function createIcon($icon)

{

//图标类型 0:不显示 1:默认 2:默认(篮色) 3:默认(绿) 4:默认(紫) 10:文字显示

$config = config('img');

$width = $config['width'];

$widthTxt = $config['width_txt'];

$height = $config['height'];

$bgRed = $config['bg_red'];

$bgGreen = $config['bg_green'];

$bgBlue = $config['bg_blue'];

$textRed = $config['text_red'];

$textGreen = $config['text_green'];

$textBlue = $config['text_blue'];

$ds = DIRECTORY_SEPARATOR;

$rootDir = $_SERVER['DOCUMENT_ROOT'];

//图片或文字时,显示的宽度不一样

$hbWidth = $icon == 10 ? $widthTxt : $width;

//画布

if (function_exists('imagecreatetruecolor')) {

$hb = imagecreatetruecolor($hbWidth, $height);

} else {

$hb = imagecreate($hbWidth, $height);

}

if ($icon > 0 && $icon <= 4) {

//显示icon图片

$img = $rootDir . $ds . 'source' . $ds . 'img' . $ds . $this->img[$icon];

$source = imagecreatefrompng($img);

imagecopyresampled($hb, $source, 0, 0, 0, 0, $width, $height, $width, $height);

imagepng($hb);

imagedestroy($source);

} else if ($icon == 10) {

//显示文字icon

//图片背景

$background = imagecolorallocate($hb, $bgRed, $bgGreen, $bgBlue);

//填充背景

imagefill($hb, 0, 0, $background);

//字体颜色

$black = imagecolorallocate($hb, $textRed, $textGreen, $textBlue);

//字体文件

$font = $rootDir . $ds . 'source' . $ds . 'font' . $ds . 'simsun.ttc';

//显示的文字

$defaultTxt = $config['default_txt'];

//将文本写入图像:4个数字依次为:字号,旋转角度,padding-left,padding-top

imagefttext($hb, 10, 0, 0, 16, $black, $font, $defaultTxt);

imagepng($hb);

}

imagedestroy($hb);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值