PHP制作图片缩略图、加水印、加字体

下面是我提供的一个类,下面封装了这三种功能:

<?php 
/**
* 
*/
class ImageHelper
{
	private $imgSrc;
	private $info;
	private $type;
	private $image;
	private $showOrSaveFunc;

	public function __construct($imgSrc) {
		$this->imgSrc = $imgSrc;
		$this->init();
	}

	/**
	* 初始化操作
	*/
	private function init() {
		//获取图片信息(通过GD库提供的方法,得到你想要处理的图片的基本信息)
		$this->info = getimagesize($this->imgSrc);
		//通过图像的编号来获取图像的类型
		$this->type = image_type_to_extension($this->info[2], false);
		//在内存中创建一个和我们图像类型一样的图像
		$fun = "imagecreatefrom{$this->type}";
		//把图片复制到内存中
		$this->image = $fun($this->imgSrc);
		$this->showOrSaveFunc = "image{$this->type}";
	}

	/**
	* 给图片添加字体
	* @param $fontfile 字体文件路径
	* @param $text 文字内容
	* @param $red 红色分量
	* @param $green 绿色分量
	* @param $blue 蓝色分量
	* @param $alpha 透明度
	* @param $angle 偏移角度
	* @param $size 字体大小
	* @param $x 距离左边的距离
	* @param $y 距离右边的距离
	*/
	public function addFont($fontfile, $text, $red, $green, $blue, $alpha
		, $angle, $size, $x, $y) {
		$color = imagecolorallocatealpha($this->image, $red, $green, $blue, $alpha);
		imagettftext($this->image, $size, $angle, $x, $y, $color, $fontfile, $text);
	}

	/**
	* 浏览器输出图片类型
	*/
	private function setOutputHeader() {
		header("Content-Type:".$this->info['mime']);
	}

	/**
	* 输出图片到浏览器
	*/
	public function outputImageToBrowser() {
		$this->setOutputHeader();
		$this->excShowOrSaveFunc($this->showOrSaveFunc);
	}

	/**
	* 输出或者保存图片
	*/
	private function excShowOrSaveFunc($type, $file='') {
		if ($file == '') {
			$type($this->image);
		} else {
			$type($this->image, $file);
		}
	}

	/**
	* 保存文件到本地
	* @param 本地路径
	* @param 文件名
	*/
	public function outputImageToStorage($filepath, $filename) {
		$this->setOutputHeader();
		$this->excShowOrSaveFunc($this->showOrSaveFunc, $filepath.$filename.'.'.$this->type);
	}

	/**
	* 为图片添加水印
	* @param $waterMarkPath 水印图片路径
	* @param $dst_x 水印距离原图左侧的距离
	* @param $dst_y 水印距离原图上侧的距离
	* @param $pct 水印的透明度
	*/
	public function addWaterMark($waterMarkPath,
	 			$dst_x, $dst_y, $pct) {

		$waterInfo = getimagesize($waterMarkPath);
		$waterType = image_type_to_extension($waterInfo[2], false);
		$waterFun = "imagecreatefrom{$waterType}";
		$waterMarkImage = $waterFun($waterMarkPath);
		//imagecopymerge(dst_im, src_im, dst_x, dst_y, src_x, src_y, src_w, src_h, pct)
		//pct透明度
		imagecopymerge($this->image, $waterMarkImage, $dst_x, $dst_y,
			 0, 0, $waterInfo[0], $waterInfo[1], $pct);
		imagedestroy($waterMarkImage);
	}

	/**
	* 生成图片缩略图
	* @param $mWidth 想要压缩的宽度
	*/
	public function createThumbnail($mWidth) {
		$width = $this->info[0];
		$height = $this->info[1];

		if ($mWidth >= $width) {
			return ;
		}
	
		$mHeight = $height * $mWidth / $width;
		//1.在内存中建立一个宽300、高200的真色彩图片
		$imageThumb = imagecreatetruecolor($mWidth, $mHeight);
		//2.核心步骤、将原图复制到新建的正色彩图片上,并按照一定比例压缩
		//imagecopyresampled(dst_image, src_image, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
		imagecopyresampled($imageThumb, $this->image, 0, 0,
			 0, 0, $mWidth, $mHeight, $width, $height);	
		$this->image = $imageThumb;
	}

	/*
	* 销毁图片资源
	*/
	public function destroy() {
		imagedestroy($this->image);
	}

	
}
 ?>

使用方法如下:

<?php 
	require_once 'ImageHelper.class.php';
	$imageHelper = new ImageHelper('P60219-211631.jpg');
	/*$imageHelper->addFont("consola.ttf", "raid", 0, 0, 0, 50
		, 0, 45, 20, 20);
	$imageHelper->addWaterMark('pic2.jpg',
	 			0, 0, 50);*/
	$imageHelper->createThumbnail(300);
	// $imageHelper->outputImageToBrowser();
	$imageHelper->outputImageToStorage('','testImageHelperAddFont');
	$imageHelper->destroy();
 ?>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值