一个PHP的图片处理类
<?php
//
// +----------------------------------------------------------------------+
// | PHP version 5.0 |
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 |
// | Moocky Mark(木目子). |
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Authors: Moocky Mark |
// | QQ: 44333697 |
// | MSN: [email]moocky@hotmail.com[/email] |
// | Email: [email]moocky@moocky.net[/email]
|
// +----------------------------------------------------------------------+
//
//
/**
* 图片处理部分
*
*
* @version v 1.0
* @access Upload
*/
class UploadGD
{
var $mimePhoto
=array(''image/gif''=>''.gif'',''image/pjpeg''=>''.jpg'',''image/jpeg''=>''.jpg''
var $Path ="/photo";
var $smallPath ="/photo/small";
var $MaxSize =5242880000;
var $waterstring ="MOocky.COM";
var $font_size =5;
var $watermark =true;
var $width =120;
var $heigh =160;
var $DOC_ROOT = "";
function UploadGD(&$MyPublic)
{
$this->DOC_ROOT = $_SERVER["DOCUMENT_ROOT"];
$this->Public = $MyPublic;
}
/**
* 得到图片信息
* @params Array;
* @return Boolean;
*/
function getInfo($Image)
{
$info=@getimagesize($this->DOC_ROOT.$this->Path."/".$Image
if($info)
{
$return["width"]=$info[0];
$return["heigh"]=$info[1];
$return["mime"]=$info["mime"];
$return["channels"]=$info["channels"];
$return["bits"]=$info["bits"];
return $return;
}
return;
}
/**
* 上传照片
* @params Array;
* @return Boolean;
*/
function upload($infoAry)
{
if($infoAry["size"]>$this->MaxSize)
{
return -2;
}
if($this->mimePhoto[$infoAry["type"]]=="")
{
return -1;
}
$upload_name=$this->Public->make_rand(16).$this->mimePhoto[$infoAry["type"]]
;
/*如果该文件名存在,重新生成一个*/
if(@file_exists($this->DOC_ROOT.$this->Path."/".$upload_name))
{
return $this->upload($infoAry
}
if(@move_uploaded_file($infoAry["tmp_name"],$this->DOC_ROOT.$this->Path."/".
$upload_name))
{
return $upload_name;
}else{
return 0;
}
}
/**
* 给图片加水印
* @params String;
* @return Byte;
* 只有jpg图片才可以加水印
*/
function watermark($Image)
{
$info=$this->getInfo($Image
if(!$info)
{
return 0;
}
$file=$this->DOC_ROOT.$this->Path."/".$Image;
$nimage=@imagecreatetruecolor($info["width"],$info["heigh"]
$white=@imagecolorallocate($nimage,255,255,255
$black=@imagecolorallocate($nimage,0,0,0
$simage =@imagecreatefromjpeg($file
if($simage)
{
@imagecopy($nimage,$simage,0,0,0,0,$info["width"],$info["heigh"]
@imagestring($nimage,$this->font_size,$info["width"]-109,$info["heigh"]-25,$
this->waterstring,$black
@imagestring($nimage,$this->font_size,$info["width"]-110,$info["heigh"]-26,$
this->waterstring,$white
$result=@imagejpeg($nimage, $file
//覆盖原上传文件
@imagedestroy($nimage
}else{
$result=0;
}
@imagedestroy($simage
return $result;
}
/**
* 生成一个缩略图
* @params String;
* @return String;
*/
function make2small($Image)
{
$info=$this->getInfo($Image
if(!$info)
{
return 0;
}
$file=$this->DOC_ROOT.$this->Path."/".$Image;
/*随即生成一个文件名*/
$small=$this->Public->make_rand(16).$this->mimePhoto[$info["mime"]];
if(@file_exists($this->DOC_ROOT.$this->Path."/".$small))
{
$this->make2small($Image
}
/*如果大图小于小缩略图,则直接复制*/
if($this->width>=$info["width"] && $this->heigh>=$info["heigh"])
{
if(@copy($file,$this->DOC_ROOT.$this->smallPath."/".$small))
{
return $small;
}else{
return 0;
}
}
/*按照比例得到缩略图的高、宽*/
if(($info["width"] / $info["heigh"]) > ($this->width /
$this->heigh))
{
$w=$this->width;
$h=floor($info["heigh"]*$w / $info["width"]
}else{
$h=$this->heigh;
$w=floor($info["width"]*$h / $info["heigh"]
}
/*创建缩略图*/
switch($info["mime"])
{
case "image/gif":
$im=@imagecreatefromgif($file
break;
case "image/pjpeg":
case "image/jpeg":
$im=@imagecreatefromjpeg($file
break;
case "image/png":
$im=@imagecreatefrompng($file
break;
default:
return -1;
}
if($im)
{
$ni=imagecreate($w,$h
@imagecopyresampled($ni,$im,0,0,0,0,$w,$h,$info["width"],$info["heigh"]
$result=@imagejpeg($ni,$this->DOC_ROOT.$this->smallPath."/".$small,100
@imagedestroy($ni
}
@imagedestroy($im
if($result)
{
return $small;
}else{
return "NoSmall.gif";
}
}
function delete($Image)
{
if(@file_exists($this->DOC_ROOT.$this->Path."/".basename($Image)))
{
return
@unlink($this->DOC_ROOT.$this->Path."/".basename($Image)
}elseif(@file_exists($this->DOC_ROOT.$this->smallPath."/".basename($Image)))
{
return
@unlink($this->DOC_ROOT.$this->smallPath."/".basename($Image)
}
}
}
?>
//
// +----------------------------------------------------------------------+
// | PHP version 5.0 |
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 |
// | Moocky Mark(木目子). |
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Authors: Moocky Mark |
// | QQ: 44333697 |
// | MSN: [email]moocky@hotmail.com[/email] |
// | Email: [email]moocky@moocky.net[/email]
|
// +----------------------------------------------------------------------+
//
//
/**
* 图片处理部分
*
*
* @version v 1.0
* @access Upload
*/
class UploadGD
{
var $mimePhoto
=array(''image/gif''=>''.gif'',''image/pjpeg''=>''.jpg'',''image/jpeg''=>''.jpg''
var $Path ="/photo";
var $smallPath ="/photo/small";
var $MaxSize =5242880000;
var $waterstring ="MOocky.COM";
var $font_size =5;
var $watermark =true;
var $width =120;
var $heigh =160;
var $DOC_ROOT = "";
function UploadGD(&$MyPublic)
{
$this->DOC_ROOT = $_SERVER["DOCUMENT_ROOT"];
$this->Public = $MyPublic;
}
/**
* 得到图片信息
* @params Array;
* @return Boolean;
*/
function getInfo($Image)
{
$info=@getimagesize($this->DOC_ROOT.$this->Path."/".$Image
if($info)
{
$return["width"]=$info[0];
$return["heigh"]=$info[1];
$return["mime"]=$info["mime"];
$return["channels"]=$info["channels"];
$return["bits"]=$info["bits"];
return $return;
}
return;
}
/**
* 上传照片
* @params Array;
* @return Boolean;
*/
function upload($infoAry)
{
if($infoAry["size"]>$this->MaxSize)
{
return -2;
}
if($this->mimePhoto[$infoAry["type"]]=="")
{
return -1;
}
$upload_name=$this->Public->make_rand(16).$this->mimePhoto[$infoAry["type"]]
;
/*如果该文件名存在,重新生成一个*/
if(@file_exists($this->DOC_ROOT.$this->Path."/".$upload_name))
{
return $this->upload($infoAry
}
if(@move_uploaded_file($infoAry["tmp_name"],$this->DOC_ROOT.$this->Path."/".
$upload_name))
{
return $upload_name;
}else{
return 0;
}
}
/**
* 给图片加水印
* @params String;
* @return Byte;
* 只有jpg图片才可以加水印
*/
function watermark($Image)
{
$info=$this->getInfo($Image
if(!$info)
{
return 0;
}
$file=$this->DOC_ROOT.$this->Path."/".$Image;
$nimage=@imagecreatetruecolor($info["width"],$info["heigh"]
$white=@imagecolorallocate($nimage,255,255,255
$black=@imagecolorallocate($nimage,0,0,0
$simage =@imagecreatefromjpeg($file
if($simage)
{
@imagecopy($nimage,$simage,0,0,0,0,$info["width"],$info["heigh"]
@imagestring($nimage,$this->font_size,$info["width"]-109,$info["heigh"]-25,$
this->waterstring,$black
@imagestring($nimage,$this->font_size,$info["width"]-110,$info["heigh"]-26,$
this->waterstring,$white
$result=@imagejpeg($nimage, $file
//覆盖原上传文件
@imagedestroy($nimage
}else{
$result=0;
}
@imagedestroy($simage
return $result;
}
/**
* 生成一个缩略图
* @params String;
* @return String;
*/
function make2small($Image)
{
$info=$this->getInfo($Image
if(!$info)
{
return 0;
}
$file=$this->DOC_ROOT.$this->Path."/".$Image;
/*随即生成一个文件名*/
$small=$this->Public->make_rand(16).$this->mimePhoto[$info["mime"]];
if(@file_exists($this->DOC_ROOT.$this->Path."/".$small))
{
$this->make2small($Image
}
/*如果大图小于小缩略图,则直接复制*/
if($this->width>=$info["width"] && $this->heigh>=$info["heigh"])
{
if(@copy($file,$this->DOC_ROOT.$this->smallPath."/".$small))
{
return $small;
}else{
return 0;
}
}
/*按照比例得到缩略图的高、宽*/
if(($info["width"] / $info["heigh"]) > ($this->width /
$this->heigh))
{
$w=$this->width;
$h=floor($info["heigh"]*$w / $info["width"]
}else{
$h=$this->heigh;
$w=floor($info["width"]*$h / $info["heigh"]
}
/*创建缩略图*/
switch($info["mime"])
{
case "image/gif":
$im=@imagecreatefromgif($file
break;
case "image/pjpeg":
case "image/jpeg":
$im=@imagecreatefromjpeg($file
break;
case "image/png":
$im=@imagecreatefrompng($file
break;
default:
return -1;
}
if($im)
{
$ni=imagecreate($w,$h
@imagecopyresampled($ni,$im,0,0,0,0,$w,$h,$info["width"],$info["heigh"]
$result=@imagejpeg($ni,$this->DOC_ROOT.$this->smallPath."/".$small,100
@imagedestroy($ni
}
@imagedestroy($im
if($result)
{
return $small;
}else{
return "NoSmall.gif";
}
}
function delete($Image)
{
if(@file_exists($this->DOC_ROOT.$this->Path."/".basename($Image)))
{
return
@unlink($this->DOC_ROOT.$this->Path."/".basename($Image)
}elseif(@file_exists($this->DOC_ROOT.$this->smallPath."/".basename($Image)))
{
return
@unlink($this->DOC_ROOT.$this->smallPath."/".basename($Image)
}
}
}
?>