php加图片代码,php给图片加文字水印与图片水印代码

这是一个关于PHP如何在图片上添加文字和图片水印的示例代码。类`editimage`包含了创建图片、设置水印位置、字体路径等功能,并提供了`wprint_img`和`wprint_string`方法来执行水印操作。代码使用了SIMHEI.TTF字体,支持自定义水印图片和文字,以及调整水印在图片上的位置。最后,通过`choose_imgouttype`方法根据原始图片类型输出处理后的图片。
摘要由CSDN通过智能技术生成

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

这款程序给图片加文字水印时是调用 了C:\\WINDOWS\\Fonts\\\\SIMHEI.TTF字体,给图片加水印时就可以自定图片,实例代码如下:

$image->wprint_img();//执行图片水印

$image->wprint_string();//执行文字水印

class editimage{

private $imagefile;//图片文件

private $smallimg;//水印图片

private $string;//水印文字

private $position;//存放位置

private $dst_x=600;//原始图片打水印x坐标

private $dst_y=0;//原始图片打水印y坐标

private $str_x=450;

private $str_y=200;

private $font="c:windows ontssimhei.ttf";//原始图片打水印字体路径

private $imgej;// imagecolorallocate后的变量

function __get($value){

return $this->$value;

}

function __set($property,$value){

$this->$property=$value;

}

/**

* 构造函数初始化

*

* @param string $imagefile 被上水印的文件

* @param string $smallimg 水印文件

* @param string $string 水印文字

* @param string $position 存放位置

* @param int $dst_x 被上水印的图片x

* @param int $dst_y 被上水印的图片y

*/

function __construct($imagefile,$smallimg='',$string=''){//,$position='',$dst_x=0,$dst_y=0

$this->imagefile=$imagefile;

$this->smallimg=$smallimg;

$this->string=$string;

$this->imgej=$this->imagecreatef($this->imagefile);

}

function get_extname($file){//获取文件的后缀名

if (file_exists($this->imagefile)) {

$img=getimagesize($file);

switch ($img[2]){

case "1":

return "gif";

case "2":

return "jpg";

case "3":

return "png";

}

}else{

return false;

}

}

function getsize($file,$wh){//获取图大小. $wh:w获得宽,h获得高

$image=getimagesize($file);

if ($wh) {

switch ($wh){

case "w":

return $image[0];

case "h":

return $image[1];

}

}else{

return false;

}

}

function imagecreatef($file){//创建类型

if ($this->get_extname($file)) {

switch($this->get_extname($file)){

case "gif":

return imagecreatefromgif($file);

case "jpg":

return imagecreatefromjpeg($file);

case "png":

return imagecreatefrompng($file);

}

}else{

echo "文件不存在";

}

}

//水印图片处理

function wprint_img(){

if($this->smallimg){

imagecopy($this->imgej,$this->imagecreatef($this->smallimg),$this->dst_x,$this->dst_y,0,0,$this->getsize($this->smallimg,"w"),$this->getsize($this->smallimg,"h"));

}else{

return "水印图片不存在!";

}

}

//水印文字处理

function wprint_string(){

return imagettftext($this->imgej,20,0,$this->str_x,$this->str_y,imagecolorallocate($this->imgej,200,200,200),$this->font,iconv("gb2312","utf-8",$this->string));

}

function choose_imgouttype(){//输出

if($this->position){

$this->get_extname($this->imagefile);

switch ($this->get_extname($this->imagefile)){

case "gif":

return imagegif($this->imgej,$position);

case "jpg":

return imagejpeg($this->imgej,$this->position);

case "jpeg":

return imagejpeg($this->imgej,$this->position);

case "png":

return imagepng($this->imgej,$position);

}

}else{

switch ($this->get_extname($this->imagefile)){

case "gif":

return imagegif($this->imgej);

case "jpg":

return imagejpeg($this->imgej);

case "jpeg":

return imagejpeg($this->imgej);

case "png":

return imagepng($this->imgej);

}

}

}

}

//使用方法如下:

$image=new editimage("d90.gif","hknmtt.png","我的d90");

$image->wprint_img();//执行图片水印

$image->wprint_string();//执行文字水印,开源代码phpfensi.com

$image->choose_imgouttype();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值