PHP图像操作类

基于已给出的各种图像操作方法,这里我总结出了PHP图像操作的一个类,包括给图像添加文字水印、图像水印和压缩图片。读者可自行添加功能。

<?php

class Image {

	private $info;
	private $type;
	private $iamge;


	/**
	* 打开图片,写入内存
	*
	**/
	public function __construct($src) 
	{
		$this->info = getimagesize($src);
		$this->type = image_type_to_extension($this->info[2],false);
		$fun = "imagecreatefrom".$this->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[0], $this->info[1]);
		imagedestroy($this->image);
		$this->image = $image_thumb;
	}

	//文字水印
	public function fontMark($content, $fontUrl, $size, $color, $locate=array('x'=>0,'y'=>0), $angle=0)
	{
		$color = imagecolorallocatealpha($this->image, $color[0], $color[1], $color[2], $color[3]);
		imagettftext($this->image, $size, $angle, $locate['x'], $locate['y'], $color, $fontUrl, $content);
	}

	//图片水印
	public function imageMark($src,$locate,$alpha)
	{
		$info2 = getimagesize($src);
		$type2 = image_type_to_extension($info2[2],false);
		$fun2 = "imagecreatefrom".$type2;
		$water = $fun2($src);
		imagecopymerge($this->image, $water, $locate['x'], $locate['y'], 0, 0, $info2[0], $info2[1], $alpha);
		imagedestroy($water);
	}

	//在浏览器中显示
	public function show()
	{
		header("Content-type:".$this->info['mime']);
		$fun = "image".$this->type;
		$fun($this->image);
	}

	//保存为文件
	public function save($newname)
	{
		$fun = "image".$this->type;
		$fun($this->image, $newname.'.'.$this->type);
	}

	public function __destruct()
	{
		imagedestroy($this->image);
	}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值