自己写的一个图片加水印类,暂时只支持水印图片,水印文字的还没有完善
代码不是很完善,欢迎大家交流指正!
调用方式:
先包含类文件
然后实例化类:
$img = new images;
然后提供图片:
$img->imgpath(原图片路径及名称,水印图片路径及名称);
之后开始加水印:
$img->send();
说明:
send( strimg filename ) 这个方法有一个函数,可以省略,在省略的情况下,保存的图片路径和名称和原来的一样,如需保存成例外的图片,就在这里提供要保存的图片路径及名称
例外,还有一个方法,show(); 这个方法用于显示加好水印的图片
这个方法可以单独调用,但必须先调用 send()方法, 也可以连续调用: $img->send()->show();
代码不是很完善,欢迎大家交流指正!
调用方式:
先包含类文件
然后实例化类:
$img = new images;
然后提供图片:
$img->imgpath(原图片路径及名称,水印图片路径及名称);
之后开始加水印:
$img->send();
说明:
send( strimg filename ) 这个方法有一个函数,可以省略,在省略的情况下,保存的图片路径和名称和原来的一样,如需保存成例外的图片,就在这里提供要保存的图片路径及名称
例外,还有一个方法,show(); 这个方法用于显示加好水印的图片
这个方法可以单独调用,但必须先调用 send()方法, 也可以连续调用: $img->send()->show();
imga = $oldimg;
$this->imgb = $newing;
$this->weizhi = $weizhi;
return $this;
}
public function send( $name = '' ){
!empty( $name ) or $name = $this->imga;
list($this->imgwa, $this->imgwb, $this->typea) = $this->imgread( $this->imga );
list($this->imgha, $this->imghb, $this->typeb) = $this->imgread( $this->imgb );
$this->objecta = $this->openimg( $this->imga, $this->typea );
$this->objectb = $this->openimg( $this->imgb, $this->typeb );
$this->wz();
$this->start();
$this->over( $name );
return $this;
}
private function wz() {
switch( $this->weizhi ) {
case 2:
$this->x = ( $this->imgwa - $this->imgha ) / 2;
break;
case 3:
$this->x = ( $this->imgwa - $this->imgha );
break;
case 4:
$this->y = ( $this->imgwb - $this->imghb ) / 2 ;
break;
case 5:
$this->x = ( $this->imgwa - $this->imgha ) / 2;
$this->y = ( $this->imgwb - $this->imghb ) / 2 ;
break;
case 6:
$this->x = ( $this->imgwa - $this->imgha );
$this->y = ( $this->imgwb - $this->imghb ) / 2 ;
break;
case 7:
$this->y = ( $this->imgwb - $this->imghb );
break;
case 8:
$this->x = ( $this->imgwa - $this->imgha ) / 2;
$this->y = ( $this->imgwb - $this->imghb );
break;
case 9:
$this->x = ( $this->imgwa - $this->imgha );
$this->y = ( $this->imgwb - $this->imghb );
break;
default:
$this->x = 0;
$this->y = 0;
}
}
private function openimg( $filename = '', $type = 0 ) {
switch( $type ) {
case 1:
$obj = imagecreatefromgif( $filename ); break;
case 2:
$obj = imagecreatefromjpeg( $filename ); break;
case 3:
$obj = imagecreatefrompng( $filename ); break;
default:
return false;
}
return $obj;
}
private function start(){
imagecopy( $this->objecta, $this->objectb, $this->x, $this->y, 0, 0, $this->imgha, $this->imghb );
}
private function over( $name = '1.jpg' ) {
$this->okfile = $name;
switch ( $this->typea ) {
case 1:
imagegif( $this->objecta, $name );break;
case 2:
imagejpeg( $this->objecta, $name );break;
case 3:
imagepng( $this->objecta, $name ); break;
default:
return false;
}
}
private function imgread( $filepath = '' ) {
$arr = getimagesize( $filepath );
return array( $arr[0], $arr[1], $arr[2] );
}
public function show(){
@header('content-type:image/*');
echo @file_get_contents( $this->okfile );
}
}
读着倒才猪