<?php
/**
* Image Class
* Driver: GD Library
* 暂时未处理当缩略图比原图片小时 无法添加水印的情况。
*/
class Image {
public $_sourceImg ; // 源图片
public $_tmpImg; // 操作图片
public $_imgType ; // 图片后缀
public $_create; // 执行方法
public $_save; // 保存方法
public function __construct($image) {
if(is_file($image)){
@chmod($image, 0777);
$this->_sourceImg = $image;
$this->_imgType = pathinfo($image, PATHINFO_EXTENSION);
$this->_tmpImg = dirname($image).'/'.'thumb_'.basename($image);
@copy($this->_sourceImg, $this->_tmpImg);
@chmod($this->_tmpImg,0777);
switch($this->_imgType){
case 'gif' :
$this->_save = "imagegif";
$this->_create = 'imagecreatefromgif';
break;
case 'png' :
$this->_save = "imagepng";
$this->_create = 'imagecreatefrompng';
break;
case 'jpg' :
case 'jpeg' :
$this->_save = "imagejpeg";
$this->_create = 'imagecreatefromjpeg';
break;
default :
throw new Exception("ERROR; UNSUPPORTED IMAGE TYPE");
break;
}
}else{
throw new Exception($image.' is not a Image-Source!');
}
}
/*
* 缩略图片
* @param width Integer 图片剪裁后宽度
* @param width Integer 图片剪裁后高度
* @param auto bool 是否等比例额剪裁
* @param xoffset Integer 剪裁左端偏移量
* @param yoffset Integer 剪裁顶端偏移量
***/
function resize($width, $height, $auto = true, $xoffset = 0, $yoffset = 0){
if($width > $height){
$size = $width;
}else{
$size = $height;
}
$_create = $this->_create;
$_save = $this->_save;
$img = $_create($this->_sourceImg);
if($auto == TRUE){
list($org_width, $org_height) = getimagesize($this->_sourceImg);
if($org_width < $size && $org_height < $size){
$img4= $img;
}else{
if($org_width > $org_height){
$swidth = $size;
$sheight = ($org_height/$org_width) * $size;
$img4=imagecreatetruecolor ($swidth, $sheight);
imagecopyresampled($img4, $img, 0, 0, 0, 0, $swidth, $sheight, $org_width, $org_height);
}else{
$swidth = ($org_width/$org_height) * $size;
$sheight = $size;
$img4=imagecreatetruecolor ($swidth, $sheight);
imagecopyresampled($img4, $img, 0, 0, 0, 0, $swidth, $sheight, $org_width, $org_height);
}
}
$value = $this->_imgType == 'png' ? 9 : 100;
$_save($img4,$this->_tmpImg,$value);
$img = $_create($this->_tmpImg);
}else{
// 自定义裁切
$img_n=imagecreatetruecolor($width, $height);
imagecopyresized($img_n, $img, 0, 0, $xoffset, $yoffset, $width, $height, $width, $height);
$value = $this->_imgType == 'png' ? 9 : 100; // PNG图片质量最高为9 其他格式为 100 默认为75
$_save($img_n,$this->_tmpImg,$value);
}
return $this;
}
/*
* 添加水印
$gWaterPos = 9;//水印位置 0:随机 1:顶端居左 2:顶端居中 3:顶端居右 4:中部居左 5:中部居中 6:中部居右 7:底端居左 8:底端居中 9:底端居右
$gWaterMarkType = 'text';//text:用字符串作水印 img:用图片作水印
$groundImage = '' 原图
$gWaterImg = '1.jpg';//作为水印的图片,支持GIF,JPG,PNG格式
$gWaterText = 'text';//字符串内容 支持中文
$gWaterFontSize = 28;//字体大小
$gWaterTextColor = '#FF0000'; //字体颜色
$gWaterTtfFile = 'arial.ttf'; //ttf文件,可从C:/WINDOWS/Fonts得到
*/
function makeWater($gWaterPos = 9, $gWaterMarkType='img', $groundImage = '',$gWaterImg='water.jpg',$gWaterText = '',$gWaterFontSize = 28,$gWaterTextColor= '#FF0000', $gwaterTtfFile = 'arial.ttf') {
$groundImage = $this->_tmpImg;
//读取水印文件
if($gWaterMarkType == 'img'){
if(!empty($gWaterImg) && file_exists($gWaterImg)) {
list($water_w,$water_h,$water_t) = getimagesize($gWaterImg);
switch($water_t) {
case 1:$water_im = imagecreatefromgif($gWaterImg);break;
case 2:$water_im = imagecreatefromjpeg($gWaterImg);break;
case 3:$water_im = imagecreatefrompng($gWaterImg);break;
default:die($formatMsg);
}
}else{
throw new Exception('the waterImg is not fined!');
}
}
//读取背景图片
if(!empty($groundImage) && file_exists($groundImage)) {
list($ground_w,$ground_h,$ground_t) = getimagesize($groundImage);
switch($ground_t) {
case 1:$ground_im = imagecreatefromgif($groundImage);break;
case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
case 3:$ground_im = imagecreatefrompng($groundImage);break;
default:die($formatMsg);
}
}else{
throw new Exception('the BackgroundImg is not fined!');
}
//水印位置
if($gWaterMarkType == 'img') {
$w = $water_w;
$h = $water_h;
}else{
$temp = imagettfbbox(ceil($gWaterFontSize),0,$gWaterTtfFile,$gWaterText);//取得使用 TrueType 字体的文本的范围
$w = $temp[2] - $temp[6];
$h = $temp[3] - $temp[7];
unset($temp);
}
if($ground_w < $w || $ground_h < $h) {
throw new Exception('the waterImg is too small!');
}
switch($gWaterPos) {
case 0://随机
$posX = rand(0,($ground_w - $w));
$posY = rand(0,($ground_h - $h));
break;
case 1://1为顶端居左
$posX = 0;
$posY = 0;
break;
case 2://2为顶端居中
$posX = ($ground_w - $w) / 2;
$posY = 0;
break;
case 3://3为顶端居右
$posX = $ground_w - $w;
$posY = 0;
break;
case 4://4为中部居左
$posX = 0;
$posY = ($ground_h - $h) / 2;
break;
case 5://5为中部居中
$posX = ($ground_w - $w) / 2;
$posY = ($ground_h - $h) / 2;
break;
case 6://6为中部居右
$posX = $ground_w - $w;
$posY = ($ground_h - $h) / 2;
break;
case 7://7为底端居左
$posX = 0;
$posY = $ground_h - $h;
break;
case 8://8为底端居中
$posX = ($ground_w - $w) / 2;
$posY = $ground_h - $h;
break;
case 9://9为底端居右
$posX = $ground_w - $w;
$posY = $ground_h - $h;
break;
default://随机
$posX = rand(0,($ground_w - $w));
$posY = rand(0,($ground_h - $h));
break;
}
//设定图像的混色模式
imagealphablending($ground_im, true);
if($gWaterMarkType == 'img'){
imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//拷贝水印到目标文件
}else{
if(!empty($gWaterTextColor) && (strlen($gWaterTextColor)==7) ) {
$R = hexdec(substr($gWaterTextColor,1,2));
$G = hexdec(substr($gWaterTextColor,3,2));
$B = hexdec(substr($gWaterTextColor,5));
}else{
throw new Exception('The waterText color format error!');
}
imagettftext($ground_im,$gWaterFontSize,0,$posX,$posY,imagecolorallocate($ground_im,$R,$G,$B),$gWaterTtfFile, iconv("GB2312","UTF-8",$gWaterText));
}
//生成水印后的图片
@unlink($groundImage);
switch($ground_t) {
case 1:imagegif($ground_im,$groundImage);break;
case 2:imagejpeg($ground_im,$groundImage);break;
case 3:imagepng($ground_im,$groundImage);break;
default:throw new Exception('The backgoundImage is not Support MakeWaterImage!');
}
//释放内存
if(isset($water_info)){
unset($water_info);
}
if(isset($water_im)){
imagedestroy($water_im);
}
unset($ground_info);
imagedestroy($ground_im);
return $this;
}
/*
* 返回处理后的图片地址
* @param old bool 是否替换原图
* return String
**/
public function save($old = false) {
if($old){
@unlink($this->_sourceImg);
@rename($this->_tmpImg, $this->_sourceImg);
return $this->_sourceImg;
}
return $this->_tmpImg;
}
/*
* 显示图片
* @paras source String 图片路径
**/
public function render($source = TRUE) {
switch($this->_imgType){
case 'jpg':
case 'jpeg':
header('Content-Type: image/jpeg');
break;
case 'gif':
header('Content-Type: image/gif');
break;
case 'png':
header('Content-Type: image/png');
break;
}
$imgSrc = $source ? $this->_sourceImg : $this->_tmpImg;
$_create = $this->_create;
$_save = $this->_save;
$source = $_create($imgSrc);
$_save($source);
imagedestroy($imgResource);
}
}
// 测试用例
$img = new Image('111.jpg');
$img->render();
$saveFileName = $img->resize(300,300,false,300,100)->makewater(9)->save();//自定义裁切 添加水印
//$saveFileName = $img->resize(200,200)->save(); // 等比例缩略
//$saveFileName = $img->resize(300,200)->makewater(9)->save(); // 等比例缩略 添加水印
//$saveFileName = $img->makewater(1)->save(); // 制作水印
echo $saveFileName;die;
图片处理类(剪裁、缩放、水印)
最新推荐文章于 2022-04-13 09:16:25 发布