php图片处理类

<?php
/*打开图片*/
//设置图片路径
	$src='1.jpg';
	//获取图像信息
	$info = getimagesize($img);
	//通过图像的编号来获取图像的额类型;
	$type = image_type_to_extension($info[2],false);
	//在内存中创建一个和我们图像类型一样的图像
	$fun = "imagecreatefrom{$type}";//$fun = imagecreatefromjpeg,png;
	//把图像复制到我们的内存中
	$image = $fun($src);//imagecreatefromjpeg($src);
	
/*操作图片*/

	//设置字体路径
	$font = 'msyh.ttf';
	//填写水印内容
	$content = 'hello world';
	//设置字体颜色和透明度 参数:图片 三基色 透明度
	$col = imagecolorallocatealpha($image,255,255,255,50);
	//写入图片
	imagettftext($image,fontsize,angle,x,y,$col,$font,$content); 
	
	
	//设置水印路径
	$image_Mark='xxx.png';
	//获取水印图片的基本信息
	$info2 = getimagesize($image_Mark);
	$type2 = image_type_to_extension($info2[2],false);
	//复制到内存中
	$fun2 = 'imagecreatefrom{$type2}';
	$wwater = $fun2($image_Mark);
	imagecopymerge($image,$water,20,30,0,0,$info2[0],$info[1],30);
	//销毁水印图片
	imagedestroy($water);
	
	//压缩图片
	 //在内存中建立一个宽300,高200的真色彩图片;
	 $image_thumb = imagecreatetruecolor(300,200);
	 //讲原图复制到新建的真色彩的图片上,并按照一定的比例压缩
	 imagecopyresampled($image_thumb,$image,0,0,0,0,300,200,$info[0],$info[1]);
	
/*输出图片*/
	//浏览器输出
	header("Content-type:".$info['mime']);
	$func = "image{$type}";
	$func($image);
	
	
	//保存图片
	$func($image,'Newimage.'.$type);
/*销毁图片*/
	imagedestroy($imge);
	
	
	
	/*封装类*/
	class Image {
		private $info;
		private $image
		//打开图片
		public function __construct($src) {
			$info = getimagesize($src);
			$this->info = array(
				'width' => $info[0],
				'height' => $info[1],
				'type' => image_type_to_extension($info[2],false);
			);
			$fun = "imagecreatefrom{$this->info['type']}";
			$this->image = $fun($src);
		}
		//压缩图片
		public function thumb($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]);
			imagettftext($this->image,$size,$angle,$local['x'],$local['y'],$color,$font_url,$content); 
		}
		
		//添加图片水印
		public functionimageMark($source,$local,$alpha) {
			$info = getimagesize($source);
			$type = image_type_to_extension($info[2],false);
			$fun = 'imagecreatefrom{$type2}';
			$water = $fun($source);
			imagecopymerge($this->image,$water,$local['x'],$local['y'],0,0,$info2[0],$info[1],$alpha);
			imagedestroy($water);
		}
		//输出图片
		public function show() {
			header("Content-type:".$this->info['type']);
			$func = "image{$this->info['type']}";
			$func($this->image);
		}
		//保存图片
		public function save($newname) {
			$func = "image{$this->info['type']}";
			$func($image,$newname.'.'.$type);
		}
		//销毁图片
		public function __destruct() {
			imagedestroy($this->image);
		}
		
	}
?>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值