class image {
/**
* @var string $filename 文件名
* @access private
*/
private $filename = '';
/**
* @var gd resource $imageresource 原图像
* @access private
*/
private $imageresource = null;
/**
* @var int $imagewidth 原图像宽
* @access private
*/
private $imagewidth = null;
/**
* @var int $imageheight 原图像高
* @access private
*/
private $imageheight = null;
/**
* @var int $imagetype 原图像类型
* @access private
*/
private $imagetype = null;
/**
* @var int $newresource 新图像
* @access private
*/
private $newresource = null;
/**
* @var int $newrestype 新图像类型
* @access private
*/
private $newrestype = null;
/**
* 构造函数
* @param string $filename 文件名
*/
public function __construct($filename = null) {
$this->filename = $filename;
if ($this->filename) {
$this->getsrcimageinfo();
}
}
/**
* 取源图像信息
* @access private
* @return void
*/
private function getsrcimageinfo() {
$info = $this->getimageinfo();
$this->imagewidth = $info[0];
$this->imageheight = $info[1];
$this->imagetype = $info[2];
}
/**
* 取图像信息
* @param string $filename 文件名
* @access private
* @return array
*/
private function getimageinfo($filename = null) {
if ($filename==null) {
$filename = $this->filename;
}
$info = getimagesize($filename);
return $info;
}
/**
* 创建源图像gd 资源
* @access private
* @return void
*/
private function createsrcimage () {
$this->imageresource = $this->createimagefromfile();
}
/**
* 跟据文件创建图像gd 资源
* @param string $filename 文件名
* @return gd resource
*/
public function createimagefromfile($filename = null)
{
if (!$filename) {
$filename = $this->filename;
$imgtype = $this->imagetype;
}
if (!is_readable($filename) || !file_exists($filename)) {
throw new exception('unable to open file ' . $filename . '');
}
if (!$imgtype) {
$imageinfo = $this->getimageinfo($filename);
$imgtype = $imageinfo[2];
}
switch ($imgtype) {
case imagetype_gif:
$tempresource = imagecreatefromgif($filename);
break;
case imagetype_jpeg:
$tempresource = imagecreatefromjpeg($filename);
break;
case imagetype_png:
$tempresource = imagecreatefrompng($filename);
break;
case imagetype_wbmp:
$tempresource = imagecreatefromwbmp($filename);
break;
case imagetype_xbm:
$tempresource = imagecreatefromxbm($filename);
break;
default:
throw new exception('错误的图片格式,或图片有问
题!');
}
return $tempresource;
}
/**
* 改变图像大小
* @param int $width 宽
* @param int $height 高
* @param string $flag 按什么方式改变 0=长宽转换成参数指定的 1=按比
例缩放,长宽约束在参数指定内,2=以宽为约束缩放,3=以高为约束缩放
* @return string
*/
public function resizeimage($width, $height, $flag=1) {
global $cfg;
$widthratio = $width/$this->imagewidth;
$heightratio = $height/$this->imageheight;
switch ($flag) {
case 1:
if ($this->imageheight
>imagewidth $endwidth = $this->imagewidth;
$endheight = $this->imageheight;
//return;
} elseif (($this->imageheight * $widthratio)
>$height) {
$endwidth = ceil($this->imagewidth *
$heightratio);
$endheight = $height;
} else {
$endwidth = $width;
$endheight = ceil($this->imageheight *
$widthratio);
}
break;
case 2:
$endwidth = $width;
$endheight = ceil($this->imageheight * $widthratio);
break;
case 3:
$endwidth = ceil($this->imagewidth * $heightratio);
$endheight = $height;
break;
case 4:
$endwidth2 = $width;
$endheight2 = $height;
if ($this->imageheight
>imagewidth $endwidth = $this->imagewidth;
$endheight = $this->imageheight;
//return;
} elseif (($this->imageheight * $widthratio)
$endwidth = ceil($this->imagewidth *
$heightratio);
$endheight = $height;
} else {
$endwidth = $width;
$endheight = ceil($this->imageheight *
$widthratio);
}
break;
case 5:
$endwidth2 = $width;
$endheight2 = $height;
if ($this->imageheight > $height && $this-
>imagewidth > $width) {
//都大
$ratio = max($this->imageheight/
$height,$this->imagewidth/$width);
}elseif ($this->imageheight > $height){
$ratio = $this->imageheight/$height;
}elseif ( $this->imagewidth > $width){
$ratio =$this->imagewidth/$width;
}else{
$ratio =1;
}
$endwidth = $this->imagewidth / $ratio;
$endheight = $this->imageheight / $ratio;
break;
default:
$endwidth = $width;
$endheight = $height;
break;
}
if ($this->imageresource==null) {
$this->createsrcimage();
}
if($flag == 5){
//直接缩略
$this->newresource = imagecreatefromjpeg($cfg
['path']['data'].'blank_thumb.jpg');
}elseif ($flag==4) {
$this->newresource = imagecreatetruecolor
($endwidth2,$endheight2);
} else {
$this->newresource = imagecreatetruecolor
($endwidth,$endheight);
}
$this->newrestype = $this->imagetype;
if($flag == 5){
$dest_x = ($width-$endwidth)/2;
$dest_y = ($height-$endheight)/2;
imagecopyresampled($this->newresource, $this-
>imageresource, $dest_x, $dest_y, 0, 0, $endwidth, $endheight,$this-
>imagewidth,$this->imageheight);
}else{
imagecopyresampled($this->newresource, $this-
>imageresource, 0, 0, 0, 0, $endwidth, $endheight,$this->imagewidth,$this-
>imageheight);
}
}
/**
* 给图像加水印
* @param string $watercontent 水印内容可以是图像文件名,也可以是文
字
* @param int $pos 位置0-9可以是数组
* @param int $textfont 字体大字,当水印内容是文字时有效
* @param string $textcolor 文字颜色,当水印内容是文字时有效
* @return string
*/
public function watermark($watercontent, $pos = 0, $textfont=5,
$textcolor=#ffffff) {
$iswaterimage = file_exists($watercontent);
if ($iswaterimage) {
$waterimgres = $this->createimagefromfile
($watercontent);
$waterimginfo = $this->getimageinfo($watercontent);
$waterwidth = $waterimginfo[0];
$waterheight = $waterimginfo[1];
} else {
$watertext = $watercontent;
//$temp = @imagettfbbox(ceil
($textfont*2.5),0,./cour.ttf,$watercontent);
if ($temp) {
$waterwidth = $temp[2]-$temp[6];
$waterheight = $temp[3]-$temp[7];
} else {
$waterwidth = 100;
$waterheight = 12;
}
}
if ($this->imageresource==null) {
$this->createsrcimage();
}
switch($pos)
{
case 0://随机
$posx = rand(0,($this->imagewidth - $waterwidth));
$posy = rand(0,($this->imageheight - $waterheight));
break;
case 1://1为顶端居左
$posx = 0;
$posy = 0;
break;
case 2://2为顶端居中
$posx = ($this->imagewidth - $waterwidth) / 2;
$posy = 0;
break;
case 3://3为顶端居右
$posx = $this->imagewidth - $waterwidth;
$posy = 0;
break;
case 4://4为中部居左
$posx = 0;
$posy = ($this->imageheight - $waterheight) / 2;
break;
case 5://5为中部居中
$posx = ($this->imagewidth - $waterwidth) / 2;
$posy = ($this->imageheight - $waterheight) / 2;
break;
case 6://6为中部居右
$posx = $this->imagewidth - $waterwidth;
$posy = ($this->imageheight - $waterheight) / 2;
break;
case 7://7为底端居左
$posx = 0;
$posy = $this->imageheight - $waterheight;
break;
case 8://8为底端居中
$posx = ($this->imagewidth - $waterwidth) / 2;
$posy = $this->imageheight - $waterheight;
break;
case 9://9为底端居右
$posx = $this->imagewidth - $waterwidth-20;
$posy = $this->imageheight - $waterheight-10;
break;
default://随机
$posx = rand(0,($this->imagewidth - $waterwidth));
$posy = rand(0,($this->imageheight - $waterheight));
break;
}
imagealphablending($this->imageresource, true);
if($iswaterimage) {
imagecopy($this->imageresource, $waterimgres, $posx,
$posy, 0, 0, $waterwidth,$waterheight);
} else {
$r = hexdec(substr($textcolor,1,2));
$g = hexdec(substr($textcolor,3,2));
$b = hexdec(substr($textcolor,5));
$textcolor = imagecolorallocate($this-
>imageresource, $r, $g, $b);
imagestring ($this->imageresource, $textfont, $posx,
$posy, $watertext, $textcolor);
}
$this->newresource = $this->imageresource;
$this->newrestype = $this->imagetype;
}
/**
* 生成验证码图片
* @param int $width 宽
* @param string $height 高
* @param int $length 长度
* @param int $validtype 0=数字,1=字母,2=数字加字母
* @param string $textcolor 文字颜色
* @param string $backgroundcolor 背景颜色
* @return void
*/
public function imagevalidate($width, $height, $length = 4,
$validtype = 1, $textcolor = '#000000', $backgroundcolor = '#ffffff') {
if ($validtype==1) {
//$validstring =
'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz';
//$validlength = 52;
//no i no l
$validstring =
'abcdefghjkmnopqrstuvwxyzabcdefghjkmnopqrstuvwxyz';
$validlength = 48;
} elseif ($validtype==2) {
//$validstring =
'0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz';
//$validlength = 62;
//no i no l no 1
$validstring =
'023456789abcdefghjkmnopqrstuvwxyzabcdefghjkmnopqrstuvwxyz';
$validlength = 57;
} else {
$validstring = '0123456789';
$validlength = 10;
}
srand((int)time());
$valid = '';
for ($i=0; $i $valid .= $validstring{rand(0, $validlength-1)};
}
$this->newresource = imagecreate($width,$height);
$bgr = hexdec(substr($backgroundcolor,1,2));
$bgg = hexdec(substr($backgroundcolor,3,2));
$bgb = hexdec(substr($backgroundcolor,5,2));
$backgroundcolor = imagecolorallocate($this->newresource,
$bgr, $bgg, $bgb);
$tr = hexdec(substr($textcolor,1,2));
$tg = hexdec(substr($textcolor,3,2));
$tb = hexdec(substr($textcolor,5,2));
$textcolor = imagecolorallocate($this->newresource, $tr,
$tg, $tb);
for ($i=0;$i imagestring($this->newresource,5,$i*$width/
$length+3,2, $valid[$i],$textcolor);
}
$this->newrestype = imagetype_jpeg;
return $valid;
}
/**
* 显示输出图像
* @return void
*/
public function display($filename='', $quality=60) {
$imgtype = $this->newrestype;
$imagesrc = $this->newresource;
switch ($imgtype) {
case imagetype_gif:
if ($filename=='') {
header('content-type: image/gif');
}
imagegif($imagesrc, $filename, $quality);
break;
case imagetype_jpeg:
if ($filename=='') {
header('content-type: image/jpeg');
}
imagejpeg($imagesrc, $filename, $quality);
break;
case imagetype_png:
if ($filename=='') {
header('content-type: image/png');
imagepng($imagesrc);
} else {
imagepng($imagesrc, $filename);
}
break;
case imagetype_wbmp:
if ($filename=='') {
header('content-type: image/wbmp');
}
imagewbmp($imagesrc, $filename, $quality);
break;
case imagetype_xbm:
if ($filename=='') {
header('content-type: image/xbm');
}
imagexbm($imagesrc, $filename, $quality);
break;
default:
throw new exception('unsupport image type');
}
imagedestroy($imagesrc);
}
/**
* 保存图像
* @param int $filenametype 文件名类型 0使用原文件名,1使用指定的文
件名,2在原文件名加上后缀,3产生随机文件名
* @param string $folder 文件夹路径 为空为与原文件相同
* @param string $param 参数$filenametype为2时为文件名加后缀
* @return void
*/
public function save($filenametype = 0, $folder = null, $param =
'_miniature') {
if ($folder==null) {
$folder = dirname($this-
>filename).directory_separator;
}
$fileextname = filesystem::fileext($this->filename, true);
$filebesicname = filesystem::getbasicname($this->filename,
false);
switch ($filenametype) {
case 1:
//$newfilename = $folder.$param;
$newfilename = $folder.basename($this-
>filename);
//var_dump($newfilename);
break;
case 2:
$newfilename =
$folder.$filebesicname.$param.$fileextname;
break;
case 3:
$tmp = date('ymdhis');
$filebesicname = $tmp;
$i = 0;
while (file_exists
($folder.$filebesicname.$fileextname)) {
$filebesicname = $tmp.$i;
$i++;
}
$newfilename =
$folder.$filebesicname.$fileextname;
break;
default:
$newfilename = $this->filename;
break;
}
$this->display($newfilename);
return $newfilename;
}
/**
* 剪切出选定区域
*
* @param string $srcimgurl 原图
* @param string $endimgurl 处理过的图
* @param int $x 坐标原点x
* @param int $y 坐标原点y
* @param int $endimg_w 最终图宽
* @param int $endimg_h 最终图高
* @param int $border_w 末坐标x
* @param int $border_h 末坐标y
* @param int $scale 原图缩放情况百分比
* @param int $fix 是否自动取值
*/
public function cutimg
($srcimgurl,$endimgurl,$x,$y,$endimg_w,$endimg_h,$border_w,$border_h,$scale=
100,$fix=0){
$path = dirname ($endimgurl);
if (!is_dir($path)) {
if(!@mkdir ($path, 0777)){
die ({$path} 此目录不能创建,文件创建失败);
}
}
$ground_info = getimagesize($srcimgurl);
switch($ground_info[2]){
case 1:$im = imagecreatefromgif($srcimgurl);break;
case 2:$im = imagecreatefromjpeg($srcimgurl);break;
case 3:$im = imagecreatefrompng($srcimgurl);break;
default:die(图片格式不允许$srcimgurl);
}
if($fix){//方便截取头像的一部分
if($ground_info[0] $border_w=$ground_info[0];
$border_h=$endimg_h*$ground_info[0]/
$endimg_w;
}elseif($ground_info[0]>$ground_info[1]){
$border_h=$ground_info[1];
$border_w=$endimg_w*$ground_info[1]/
$endimg_h;
}else{
$border_w=$ground_info[0];
$border_h=$ground_info[1];
}
}
$newim = imagecreatetruecolor($endimg_w, $endimg_h);
$x=($x*100)/$scale;
$y=($y*100)/$scale;
$border_width=($border_w*100)/$scale;
$border_height=($border_h*100)/$scale;
imagecopyresampled($newim, $im, 0,0, $x,$y, $endimg_w,
$endimg_h, $border_width, $border_height );
if(function_exists(imagegif)){
switch($ground_info[2]){
case 1:imagegif($newim,$endimgurl);break;
case 2:imagejpeg($newim,$endimgurl);break;
case 3:imagepng($newim,$endimgurl);break;
default:die(errormsg);
}
}elseif(function_exists(imagejpeg)){
imagejpeg($newim,$endimgurl);
}else{
imagepng($newim,$endimgurl);
}
imagedestroy ($newim);
imagedestroy ($im);
}
}
http://www.bkjia.com/phpjc/445017.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/445017.htmltecharticlephp 图片处理类,缩略,水印 class image { /** * @var string $filename 文件名 * @access private */ private $filename = ''; /** * @var gd resource $imageresource 原图像...