<?php
/**
+------------------------------------------------------------------------------
* 后台管理 图片文件上传综合处理功能类
+------------------------------------------------------------------------------
* @category
* @package
* @author Revised by pengzl<pzl7758@163.com>
* @version $Id: 2009-02-17 update by pengzl$
+------------------------------------------------------------------------------
*/
class Image
{
private $image;
private $imageUrl;
private $imagepath;
private $imageSize;
private $imageWidth;
private $imageHeight;
private $image_mime;
private $saveName;
private $savePath;
private $waterPath; //水印路径
//水印处理
private $font_angle = 0;//倾斜角度
private $font_text = "文字水印";
private $font_color = array("233","14","91");
private $font_size = 20;
private $font_ttf = "";
private $font_x = 20;
private $font_y = 20;
private $img_width;
private $img_height;
private $img_url;
private $img_water;
private $img_water_x;
private $img_water_y;
private $img_water_Transparency = 20;//水印透明度
private $img_water_PosType;//水印位置
//缩略图设置
private $shrink_width = 0;
private $shrink_height = 0;
private $shrink_type = "gif";
//上传设置
private $upfile_size = 0;//为0则大小不限制
private $upfile_type = array(
'image/jpg',
'image/jpeg',
'image/png',
'image/pjpeg',
'image/gif',
'image/bmp',
'image/x-png'
);
private $erro_msg = "";
public function Image()
{
$this->font_size = 12;
$this->font_angle = 0;
}
//上传
public function upfile($fname, $savepath="")
{
clearstatcache();
$file = $_FILES[$fname];
$fileInfo = pathinfo($file['name']);
if (!empty($savepath)) {
$this->savePath = $savepath;
} else {
$this->savePath = "./".date("Ymd")."/";
}
if (empty($this->saveName)) {
$this->saveName = time().rand(0,999999).".".$fileInfo['extension'];
}
self::is_write($this->savePath);
$savection = $this->savePath.$this->saveName;
if ($this -> upfile_size) {
if ($file['size'] > $this->upfile_size) {
$this->erro_msg .= "上传文件过大\r\n";
return false;
}
}
if (!in_array($file['type'], $this->upfile_type)) {
$this->erro_msg .= "出错代码:{$file['error']}\r\n类型不符!\r\n";
return false;
}
if (!@move_uploaded_file ($file['tmp_name'], $savection))
{
$this->erro_msg .= "出错代码:{$file['error']}\r\n移动文件失败!\r\n";
return false;
} else {
chmod ($savection,0775);//给文件赋权限
}
$temp = getimagesize($savection);
$this->image = self::readImage($fileInfo['extension'], $savection);
$this->image_mime = $temp['mime'];
$this->imageHeight = $temp[0];
$this->imageWidth = $temp[1];
$this->imagepath = $savection;
return $savection;
}
//加图片水印
public function createImg($fname="",$savepath="")
{
if (empty($fname))
{
$fname = $this->imagepath;
}
if ($fileInfo = self::is_File_True($fname)) {
$this->image = self::readImage($fileInfo['extension'],$fname);//取背景图片信息
}
if (empty($this->saveName)) {
$this->saveName = $fileInfo['basename'];
}
if (empty($savepath))
{
empty($this->savePath) ? $savepath = $fname : $savepath = $this->savePath;
} else {
self::is_write($savepath);
}
$savection = $savepath.$this->saveName;
if (empty($this->img_water))
{
$this->erro_msg .= "水印文件未读取\r\n";
return false;
}
$bg_w = imagesx($this->image);
$bg_h = imagesy($this->image);
if($bg_w < ($this->img_width+ceil($this->img_width/2)) || $bg_h < ($this->img_Height+ceil($this->img_Height/2)))
{
//height=h/(w/fixwidth);
//width=w/(h/fixheight);
$this->erro_msg .= "文件太小未加水印!\r\n";
return false;
}
/*
if ($bg_w < ($this->img_width+10) || $bg_h < ($this->img_Height+10))
{
//height=h/(w/fixwidth);
//width=w/(h/fixheight);
$this->erro_msg .= "文件太小未加水印!";
return false;
}
*/
imagealphablending($this->image,true);
imagealphablending($this->img_water,true);
if (empty($this-> img_water_PosType)) {
$this-> img_water_PosType = 4;//右下角
}
self::WaterPos($this-> img_water_PosType);
imagecopymerge(
$this->image,
$this->img_water,
$this->img_water_x,
$this->img_water_y,
0,0,
$this->img_width,
$this->img_Height,
$this->img_water_Transparency);
/*
imagecopyresampled(
$this->image,
$this->img_water,
$this->img_water_x,$this->img_water_y,
0,0,
$this->img_width,$this->img_Height,
$this->img_width,$this->img_Height
);
*/
$_rtn = self::saveImage($this->image, $fileInfo['extension'], $savepath,substr($this->saveName,0,-4));
imagedestroy($this->image);
imagedestroy($this->img_water);
return $_rtn;
}
//读取水印文件(必须操作)
function read_waterImg($Cimgu="")
{
if (empty($Cimgu))
{
$Cimgu = "./water.jpg";
}
$this->waterPath = $Cimgu;
if ($Cimginfo = self::is_File_True($this->waterPath))
{
$this->img_water = self::readImage($Cimginfo['extension'],$this->waterPath);//取水印图片信息
}
$this->img_width = imagesx($this->img_water);
$this->img_Height = imagesy($this->img_water);
return $this;
}
//建立文字水印
function createTextImg($fname="", $savepath="")
{
if (empty($fname))
{
$fname = $this->imagepath;
}
if ($fileInfo = self::is_File_True($fname)) {
$this->image = self::readImage($fileInfo['extension'],$fname);//取背景图片信息
}
if (empty($this->saveName)){
$this->saveName = $fileInfo['basename'];
}
if (empty($savepath))
{
empty($this->savePath) ? $savepath = $fname : $savepath = $this->savePath;
} else {
self::is_write($savepath);
}
$savection = $savepath.$this->saveName;
if (empty($this->font_text)) {
$this->erro_msg .= "水印文字为空\r\n";
return false;
}
if (!function_exists("iconv")) {
$this->erro_msg .= "字体转换模块iconv未加载\r\n";
return false;
} else {
$this->font_text = iconv('GB2312','UTF-8//IGNORE',$this->font_text);
}
if (empty($this->font_color)) {
$this->font_color = "#000000";
}
//将16进制转为数组array(r,g,b)
if (strlen($t