<?php
/*
* [图像类]
* @author: luochen[houdunwangmzy@163.com]
* @Date: 2014-05-02 21:22:54
* @Last Modified time: 2014-05-03 20:13:36
*/
class Image{
//缩略图属性********************
//缩略图宽
private $thumbW;
//缩略图高
private $thumbH;
//缩略图路径
private $thumbPath;
//缩略图前缀
private $prefix;
//水印属性
//水印位置
private $position;
//水印不透明度
private $alpha;
//水印资源
private $water;
//保存的质量
private $quality;
/**
* [thumb 缩略方法]
* @param [type] $img [description]
* @param [type] $thumbW [description]
* @param [type] $thumbH [description]
* @param [type] $prefix [description]
* @return [type] [description]
*/
public function thumb($img,$thumbPath = NULL, $thumbW = NULL,$thumbH = NULL, $prefix = NULL){
if(!extension_loaded('GD')) return false;
$this->thumbW = is_null($thumbW) ? 500 : $thumbW;
$this->thumbH = is_null($thumbH) ? 300 : $thumbH;
$this->prefix = is_null($prefix) ? 'thumb_' : $prefix;
//1,打开源图资源******************
//获得图片信息
$imgInfo = getimagesize($img);
//宽高
$srcW = $imgInfo[0];
$srcH = $imgInfo[1];
//类型
$type = ltrim(strrchr($imgInfo['mime'], '/'),'/');
//打开源图资源
$fn = 'imagecreatefrom' . $type;
$srcImg = $fn($img);
//2.创建画布(目标图)(处理透明问题,只需要理解)************
if($type == 'gif'){
$dstImg = imagecreate($this->thumbW,$this->thumbH);
$color = imagecolorallocate($dstImg, 255, 0, 0);
}else{
$dstImg = imagecreatetruecolor($this->thumbW, $this->thumbH);
//关闭混色通道
imagealphablending($dstImg, false);
//开启透明通道
imagesavealpha($dstImg, true);
}
//3.进行缩放***********************
if(function_exists('imagecopyresampled')){
imagecopyresampled($dstImg, $srcImg, 0, 0, 0, 0, $this->thumbW, $this->thumbH, $srcW, $srcH);
}else{
imagecopyresized($dstImg, $srcImg, 0, 0, 0, 0, $this->thumbW, $this->thumbH, $srcW, $srcH);
}
if($type == 'gif'){
imagecolortransparent($dstImg, $color);
}
//用pathinfo获得路径等其他信息
$imgInfo = pathinfo($img);
//处理路径,如果用户传递,按照用户传递的目录来,如果没有,默认是源图路径
$path = is_null($thumbPath) ? rtrim($imgInfo['dirname'], '/') . '/' : $thumbPath;
//目录创建
is_dir($path) || mkdir($path, 0777, true);
//加前缀
$filename = $this->prefix . $imgInfo['basename'];
//4.保存****************
$fn = 'image' . $type;
$fn($dstImg, $path . $filename);
imagedestroy($srcImg);
imagedestroy($dstImg);
return true;
}
/**
* [water 加盖水印]
* @param [type] $dst [description]
* @param [type] $src [description]
* @param [type] $path [description]
* @param [type] $position [description]
* @param [type] $alpha [description]
* @param [type] $quality [description]
* @return [type] [description]
*/
public function water($dst,$src,$path = NULL,$position = NULL, $alpha = NULL, $quality = NULL){
if(!extension_loaded('GD')) return false;
$this->position = is_null($position) ? 3 : $position;
$this->alpha = is_null($alpha) ? 80 : $alpha;
$this->quality = is_null($quality) ? 80 : $quality;
//================获得目标图资源(要放水印的图)==============
$imgInfo = getimagesize($dst);
$dstW = $imgInfo[0];
$dstH = $imgInfo[1];
//获得类型
$dstType = ltrim(strrchr($imgInfo['mime'], '/'), '/');
$fn = 'imagecreatefrom' . $dstType;
//打开资源
$dstImg = $fn($dst);
//================获得源图资源(水印)==============
$imgInfo = getimagesize($src);
$srcW = $imgInfo[0];
$srcH = $imgInfo[1];
//获得类型
$srcType = ltrim(strrchr($imgInfo['mime'], '/'), '/');
$fn = 'imagecreatefrom' . $srcType;
//打开资源
$srcImg = $fn($src);
//位置信息数组
$positionArr = $this->_get_postion($dstW, $dstH, $srcW, $srcH);
$x = $positionArr['x'];
$y = $positionArr['y'];
if($srcType == 'png'){
imagecopy($dstImg, $srcImg, $x, $y, 0, 0, $srcW, $srcH);
}else{
imagecopymerge($dstImg, $srcImg, $x, $y, 0, 0, $srcW, $srcH,$this->alpha);
}
//处理保存路径
$imgInfo = pathinfo($dst);
//处理路径,如果用户传递,按照用户传递的目录来,如果没有,默认是源图路径
$path = is_null($path) ? rtrim($imgInfo['dirname'], '/') . '/' : $path;
is_dir($path) || mkdir($path, 0777, true);
//组合文件名
$filename = time() . mt_rand(0,9999) . '.' . $dstType;
//完整带有文件名路径
$fullPath = $path . $filename;
$fn = 'image' . $dstType;
//保存,
if($dstType == 'jpeg'){
//可以设置压缩质量
$fn($dstImg, $fullPath, $this->quality);
}else{
$fn($dstImg, $fullPath);
}
imagedestroy($srcImg);
imagedestroy($dstImg);
return $fullPath;
}
/**
* [_get_postion 获得水印位置]
* @param [type] $dstW [description]
* @param [type] $dstH [description]
* @param [type] $srcW [description]
* @param [type] $srcH [description]
* @return [type] [description]
*/
private function _get_postion($dstW, $dstH, $srcW, $srcH){
$arr['x'] = 25;
$arr['y'] = 25;
switch ($this->position) {
case 1:
break;
case 2:
# code...
break;
case 3:
$arr['x'] = $dstW - $srcW - 20;
break;
case 9:
$arr['x'] = $dstW - $srcW - 20;
$arr['y'] = $dstH - $srcH - 20;
break;
}
return $arr;
}
}
$img = new Image();
$img->water('./big.jpg','./logo.png');
// $img->thumb('./1.jpg');