php gd库 图片水印,php使用GD库实现文字图片水印及缩略图教程

我们要使用gd库就必须先打开gd库,具体如下

Windows下开启PHP的GD库支持

找到php.ini,打开内容,找到:

;extension=php_gd2.dll

把最前面的分号“;”去掉,再保存即可,如果本来就没有分号,那就是已经开启了。

具体可以参考下文:http://www.jquerycn.cn/phper/php/48352.htm

一:添加文字水印 使用方法

require 'image.class.php'

$src="001.jpg";

$content="hello";

$font_url="my.ttf";

$size=20;

$image=new Image($src);

$color=array(

0=>255,

1=>255,

2=>255,

2=>20

);

$local=array(

'x'=>20,

'y'=>30

);

$angle=10;

$image->fontMark($content,$font_url,$size,$color,$local,$angle);

$image->show();

二:图片缩略图 使用方法:

require 'image.class.php'

$src="001.jpg";

$image=new Image($src);

$image->thumb(300,200);

$image->show();

三:image.class.php

class image{

private $info;

private $image;

public function __contruct($src){

$info= getimagesize($src);

$this->info=array(

'width'=> $info[0],

'height'=>$info[1],

'type'=>image_type_to_extension($info[2],false),

'mime'=>$info['mime'],

);

$fun="imagecreatefrom{$this->info['type']}";

$this->image= $fun($src);

}

//缩略图

public function thumd($width,$height){

$image_thumb= imagecreatetruecolor($width,$height);

imagecopyresampled($image_thumb,$this->image,0,0,0,0,$width,$height,$this->info['width'],$this->info['height']);

imagedestroy($this->image);

$this->image=$image_thumb;

}

//文字水印

public function fontMark($content,$font_url,$size,$color,$local,$angle){

$col=imagecolorallocatealpha($this->image,$color[0],$color[1],$color[2],$color[3]);

$text=imagettftext($this->image,$size,$angle,$local['x'],$local['y'],$col,$font_url,$content);

}

//输出图片

public function show()

{

header("Content-type:",$this->info['mime']);

$func="image{$this->info['type']}";

$func($this->image);

}

public function save($nwename){

$func="image{$this->info['type']}";

//从内存中取出图片显示

$func($this->image);

//保存图片

$func($this->image,$nwename.$this->info['type']);

}

public function _destruct(){

imagedestroy($this->image);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值