这篇文章主要介绍PHP如何生成随机水印图片,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!一、需求网站的布局用到了类似慕课网课程列表的风格,每一个课程是一个banner图,图下面是标题
这篇文章主要介绍PHP如何生成随机水印图片,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
网站的布局用到了类似慕课网课程列表的风格,每一个课程是一个banner图,图下面是标题加简介。因为课程的数量较大没有为所有的课程设计专门的banner,所以需要按照一定的规则,来自己生成图片(本打算用div布局来解决,但div+img在响应式布局中不是很好控制)。
1.php开启GD图形库扩展
2.准备多个小的水印图
3.获取预生成图片的背景色RGB值
生成图片的过程,代码中做了详细的注释。
class GenerateRandomImage{
public $imgWidth = 272;
public $imgHeight = 162;
public $type = '';
public $text = '';
public $fontSize = 16;
public function __construct($type, $text)
{ $this->type = $type;
$this->text = $text;
}
public function createImg()
{
$backgroundColor = p_w_picpathcolorallocate($p_w_picpath, $rgb['r'], $rgb['g'], $rgb['b']);
$textColor = p_w_picpathcolorallocate($p_w_picpath, 255, 255, 255);
$font = $_SERVER['DOCUMENT_ROOT'].'/public/font/simhei.ttf';
$x = 18;//文字起始位置x坐标
$y = 50;//文字起始位置y坐标
$angle = 0;//角度0
p_w_picpathttftext($p_w_picpath, $this->fontSize, $angle, $x, $y, $textColor, $font, $this->text);
$waterImgPath = $this->randWaterImage();
$waterInfo = getp_w_picpathsize($waterImgPath);
$waterType = p_w_picpath_type_to_extension($waterInfo[2], false);//获取文件类型
$createImageFunc = 'p_w_picpathcreatefrom'.$waterType;
$mask = $createImageFunc($waterImgPath);
$posX = $this->imgWidth - $waterInfo[0];//水印图片,在目标图片中的位置的x坐标
$posY = $this->imgHeight - $waterInfo[1];//水印图片,在目标图片中的位置的y坐标
header("Content-Type:p_w_picpath/png");
p_w_picpathcopy($p_w_picpath, $mask, $posX, $posY, 0, 0, $waterInfo[0], $waterInfo[1]);
p_w_picpathpng($p_w_picpath);//输入图片到浏览器或者文件
p_w_picpathdestroy($p_w_picpath);//销毁图片
}
public function getBackground()
{ $background = [
'1'=>['r'=>0, 'g'=>160,'b'=>233],
'2'=>['r'=>198,'g'=>0, 'b'=>110],
'3'=>['r'=>237,'g'=>109,'b'=>0],
'4'=>['r'=>33, 'g'=>148,'b'=>75],
'5'=>['r'=>63, 'g'=>58, 'b'=>57],
'6'=>['r'=>202,'g'=>162,'b'=>101],
];
return $background[$this->type];
}
public function randWaterImage()
{ $folder = [
'1'=>'product','2'=>'team','3'=>'architecture','4'=>'developer','5'=>'test','6'=>'engineer'
];
$targetFolder = $_SERVER['DOCUMENT_ROOT'].'/public/p_w_picpaths/role/'.$folder[$this->type].'/'.rand(1,38).'.png';
return $targetFolder;
}
}$p_w_picpath = new GenerateRandomImage(1,"扛得住的Mysql数据架构");$p_w_picpath->createImg();
这样我们就可以直接在页面中使用 <img src="Http://xxx.com/GenerateRandomImage.php" />来直接显示图片。
以上是“PHP如何生成随机水印图片”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网PHP编程频道!
--结束END--
本文标题: PHP如何生成随机水印图片
本文链接: https://www.lsjlt.com/news/80443.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0