图片处理类

有获得图片的宽,高,后缀的函数,有等比例缩略图的函数,有添加水印的函数,有产生验证码的函数

<?php
defined('ACC')||exit('无权访问');
//图片处理类
class ImageTool{
	//imageInfo分析图片的信息
	//return array
	public static function imageInfo($image){
		//判断图片是否存在
		if(!file_exists($image)){
			return false;
		}

		$info=getimagesize($image);

		if($info==false){
			return false;
		}

		$img['width']=$info[0];
		$img['height']=$info[1];
		$img['ext']=substr($info['mime'],strpos($info['mime'],'/')+1);

		return $img;
	}
	
	//加水印功能
	//water(原图,水印图,替换原图或新图路径,水印位置,透明度);
	public static function water($dst,$water,$save=null,$pos=2,$alpha=50){
		//是否存在
		if(!file_exists($dst)||!file_exists($water)){
			return false;
		}

		//图片信息
		$dinfo=self::imageInfo($dst);
		$winfo=self::imageInfo($water);

		if($winfo['height']>$dinfo['height']||$winfo['width']>$dinfo['width']){
			return false;
		}

		//获得函数名
		$dfunc='imagecreatefrom'.$dinfo['ext'];
		$wfunc='imagecreatefrom'.$winfo['ext'];

		//判断方法是否存在
		if(!function_exists($dfunc)||!function_exists($wfunc)){
			return false;
		}

		//创建画布
		$dim=$dfunc($dst);
		$wim=$wfunc($water);

		//计算水印位置坐标
		switch($pos){
			case 0://左上角
			$posx=0;
			$posy=0;
			break;
			case 1://右上角
			$posx=$dinfo['width']-$winfo['width'];
			$posy=0;
			break;
			case 2://左下角
			$posx=0;
			$posy=$dinfo['height']-$winfo['height'];
			break;
			default://右下角
			$posx=$dinfo['width']-$winfo['width'];
			$posy=$dinfo['height']-$winfo['height'];
		}

		imagecopymerge($dim,$wim,$posx,$posy,0,0,$winfo['width'],$winfo['height'],$alpha);

		//保存
		if(!$save){
			$save=$dst;
			unlink($dst);//删除原图
		}

		$createfunc='image'.$dinfo['ext'];
		$createfunc($dim,$save);

		imagedestroy($dim);
		imagedestroy($wim);

		return true;
	}

	//thumb生成缩略图,等比缩放,留白
	public static function thumb($dst,$save=null,$width=200,$height=200){
		//判断原图是否存在
		$dinfo=self::imageInfo($dst);
		if($dinfo==false){
			return false;
		}
		//计算缩放比例
		$calc=min($width/$dinfo['width'],$height/$dinfo['height']);

		//创建原始图画布
		$dfunc='imagecreatefrom'.$dinfo['ext'];
		$dim=$dfunc($dst);

		//创建缩略图画布
		$tim=imagecreatetruecolor($width,$height);
		
		//创建白色
		$white=imagecolorallocate($tim,255,255,255);

		imagefill($tim,0,0,$white);
		//复制并缩略
		$dwidth=(int)$dinfo['width']*$calc;
		$dheight=(int)$dinfo['height']*$calc;

		$paddingx=(int)($width-$dwidth)/2;
		$paddingy=(int)($height-$dheight)/2;

		imagecopyresampled($tim,$dim,$paddingx,$paddingy,0,0,$dwidth,$dheight,$dinfo['width'],$dinfo['height']);

		//保存
		if(!$save){
			$save=$dst;
			unlink($dst);
		}

		$createfunc='image'.$dinfo['ext'];
		$createfunc($tim,$save);

		imagedestroy($tim);
		imagedestroy($dim);

		return true;
	}

	public static function code($width=50,$height=30){
		$rs=imagecreatetruecolor($width,$height);//创建画布资源
		$gray=imagecolorallocate($rs, 100, 100, 100);

		$fontcolor=imagecolorallocate($rs, mt_rand(150,255), mt_rand(150,255), mt_rand(150,255));

		$linecolor1=imagecolorallocate($rs, mt_rand(100,200), mt_rand(100,200), mt_rand(100,200));
		$linecolor2=imagecolorallocate($rs, mt_rand(100,200), mt_rand(100,200), mt_rand(100,200));
		$linecolor3=imagecolorallocate($rs, mt_rand(100,200), mt_rand(100,200), mt_rand(100,200));

		//填充颜色
		imagefill($rs, 0, 0, $gray);

		//随机字符串
		$str=substr(str_shuffle('ABCDEFGHJKMNOPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789'),0,4);

		//书写字符串
		imagestring($rs, 5, 8, 5, $str, $fontcolor);

		//画干扰线
		imageline($rs,0,mt_rand(0,30),50,mt_rand(0,30),$linecolor1);
		imageline($rs,0,mt_rand(0,30),50,mt_rand(0,30),$linecolor2);
		imageline($rs,0,mt_rand(0,30),50,mt_rand(0,30),$linecolor3);

		//输出
		header('content-type:image/png');
		imagepng($rs);
		//销毁
		imagedestroy($rs);
		return $str;
	} 
}	


?>

 

转载于:https://www.cnblogs.com/lzzhuany/p/4797559.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值