图片处理类

test.php

 <?php

     include "images.class.php";

     $image=new Image("./images/");    

     //对图片进行缩放

     echo $image->thumb("hee.jpg",300,300,"th1_");
echo $image->thumb("hee.jpg",200,200,"th2_");
echo $image->thumb("hee.jpg",100,100,"th3_"); //对图片进行加水印
echo $image->waterMark("mag.gif","gaolf.gif",2,"wa2_");
echo $image->waterMark("mag.gif","gaolf.gif",6,"wa6_");
echo $image->waterMark("mag.gif","gaolf.gif",7,"wa7_");
?>

images.class.php

 <?php
class Image {
private $path; //构造方法用来对图片所在位置进行初始化
function __construct($path="./"){ $this->path=rtrim($path,"/")."/"; //用户在输入路径时,无斜杠则加斜杠,有斜杠则删掉再加上
} /* 功能:对图片进行缩放
*
* 参数$name:需处理的图片名称
* 参数$width:缩放后的宽度
* 参数$height:缩放后的高度
* 参数$qz:新图片的名称前缀
* 返回值:缩放后的图片名称,失败返回false
*
*/
function thumb($name,$width,$height,$qz="th_"){
//获取图片信息
$imgInfo=$this->getInfo($name); //原图片的信息 //获取图片资源,通用各种类型的图片(png,jpg,gif)
$srcImg=$this->getImg($name,$imgInfo); //获取计算图片等比例之后的大小
$size=$this->getNewSize($name,$width,$height,$imgInfo); //获取新的图片资源,处理gif透明背景问题
$newImg=$this->kid0fImage($srcImg,$size,$imgInfo); //另存为一个新的图片,返回新的缩放后的图片名称
return $this->createNewImage($newImg,$qz.$name,$imgInfo);
} private function createNewImage($newImg,$newName,$imgInfo){
//另存图片
switch($imgInfo["type"]){
case 1: //gif
$result=imagegif($newImg,$this->path.$newName);
break;
case 2: //jpg
$result=imagejpeg($newImg,$this->path.$newName);
break;
case 3: //png
$result=imagepng($newImg,$this->path.$newName);
break;
}
imagedestroy($newImg);
return $newName;
}
private function kid0fImage($srcImg,$size,$imgInfo){
//创建新图片资源 $newImg=imagecreatetruecolor($size["width"],$size["height"]); //取出透明色指数
$otsc=imagecolortransparent($srcImg); //判断是否有透明色 //()取得一幅图像的调色板中颜色的数目
if($otsc >=0 && $otsc <= imagecolorstotal($srcImg)){
$tran = imagecolorsforindex($srcImg,$otsc); //取得某索引的颜色 $newt = imagecolorallocate($newImg,$tran["red"],$tran["green"],$tran["blue"]); //为一幅图片分配颜色 imagefill($newImg,0,0,$newt); //填充颜色 imagecolortransparent($newImg,$newt); //将某个颜色定义为透明色
}
//拷贝部分图像并调整大小
imagecopyresized($newImg, $srcImg, 0, 0, 0, 0, $size["width"], $size["height"], $imgInfo["width"], $imgInfo["height"]); imagedestroy($srcImg);
return $newImg;
} private function getNewSize($name,$width,$height,$imgInfo){
$size["width"]=$imgInfo["width"];
$size["height"]=$imgInfo["height"];
//如果缩放后宽度小于原图片宽度,再重新设置图片宽度
if($width < $imgInfo["width"]){
$size["width"]=$width;
}
//如果缩放后高度小于原图高度,再重新设置图片高度
if($height < $imgInfo["height"]){
$size["height"]=$height;
} //图片等比例缩放的算法
if($imgInfo["width"]*$width > $imgInfo["height"]*$height){
$size["height"]=round($imgInfo["height"]*$size["width"]/$imgInfo["width"]);
}else{
$size["width"]=round($imgInfo["width"]*$size["height"]/$imgInfo["height"]);
} return $size;
}
private function getInfo($name){
$date=getImageSize($this->path.$name); $imageInfo["width"]=$date[0];
$imageInfo["height"]=$date[1];
$imageInfo["type"]=$date[2]; return $imageInfo;
}
private function getImg($name,$imgInfo){
$srcPic=$this->path.$name; //某路径下的图片 switch($imgInfo["type"]){
case "1": //gif
$img=imagecreatefromgif($srcPic);
break;
case "2": //jpg
$img=imagecreatefromjpeg($srcPic);
break;
case "3": //png
$img=imagecreatefrompng($srcPic);
break;
default:
return false;
}
return $img;
} /* 功能:为图片加水印
*
* 参数$groundName:背景图片,即需要加水印的图片
* 参数$waterMark:水印图片
* 参数$waterPos:水印位置,10种状态
* 0随机位置
* 1顶端居左 2顶端居中 3顶端居右
* 4中部居左 5中部居中 6中部居右
* 7底部居左 8底部居中 9底部居右
*
* 参数$qz:是加水印后图片名称的前缀
* 返回值:处理后图片的名称
*/
function waterMark($groundName,$waterName,$waterPos=0,$qz="wa_"){
if(file_exists($this->path.$groundName) && file_exists($this->path.$waterName)){
$groundInfo = $this->getInfo($groundName);
$waterInfo = $this->getInfo($waterName);
//水印位置
if(!$pos = $this->position($groundInfo,$waterInfo,$waterPos)){
echo "水印不应该比背景图片小";
return;
}
$groundImg = $this->getImg($groundName,$groundInfo);
$waterImg = $this->getImg($waterName, $waterInfo); $groundImg = $this->copyImage($groundImg, $waterImg, $pos, $waterInfo);
return $this->createNewImage($groundImg, $qz.$groundName, $groundInfo); }else{
echo "图片或水印不存在";
return false;
}
} private function copyImage($groundImg, $waterImg, $pos, $waterInfo){
imagecopy($groundImg, $waterImg, $pos["posX"], $pos["posY"], 0, 0, $waterInfo["width"], $waterInfo["height"]); imagedestroy($waterImg); return $groundImg;
}
private function position($groundInfo,$waterInfo,$waterPos){
//需要背景比水印图片大
if(($groundInfo["width"] < $waterInfo["width"]) || ($groundInfo["height"] < $waterInfo["height"])){
return false;
}
switch($waterPos){
case 1: //顶部居左
$posX=0;
$posY=0;
break;
case 2: //顶部居中
$posX=($groundInfo["width"]-$waterInfo["width"])/2;
$posY=0;
break;
case 3: //顶部居右
$posX=($groundInfo["width"]-$waterInfo["width"]);
$posY=0;
break;
case 4: //中部居左
$posX=0;
$posY=($groundInfo["height"]-$waterInfo["height"])/2;
break;
case 5: //中部居中
$posX=($groundInfo["width"]-$waterInfo["width"])/2;
$posY=($groundInfo["height"]-$waterInfo["height"])/2;
break;
case 6: //中部居右
$posX=($groundInfo["width"]-$waterInfo["width"]);
$posY=($groundInfo["height"]-$waterInfo["height"])/2;
break;
case 7: //底部居左
$posX=0;
$posY=($groundInfo["height"]-$waterInfo["height"]);
break;
case 8: //底部居中
$posX=($groundInfo["width"]-$waterInfo["width"])/2;
$posY=($groundInfo["height"]-$waterInfo["height"]);
break;
case 9: //底部居右
$posX=($groundInfo["width"]-$waterInfo["width"]);
$posY=($groundInfo["height"]-$waterInfo["height"]);
break;
case 0: //随机位置
$posX=rand(0,($groundInfo["width"]-$waterInfo["width"]));
$posY=rand(0,($groundInfo["height"]-$waterInfo["height"]));
break;
}
return array("posX"=>$posX, "posY"=>$posY);
}
}
?>

文件上传类

 <?php
/* 该用于文件上传
* 有4个公有方法可以在对象外部调用:
* __construct()构造方法用于初使化成员属性
* uploadFile()方法用于上传文件
* getNewFileName()方法用于获取上传成功后的文件名称
* getErrorMsg()方法用于上传失败后获取错误提示信息
* 其它属性和方法都被本类封装,不可以在对象外部调用
*/
class FileUpload {
private $filepath; // 上传文件的目的路径
private $allowtype = array('jpg','gif','png'); //充许上传文件的类型,使用小字母
private $maxsize = 1000000; //允许文件上传的最大长度1m
private $israndname = true; //是否随机重命名 false为不随机
private $originName; //源文件名
private $tmpFileName; //临时文件名
private $fileType; //文件类型(文件后缀)
private $fileSize; //文件大小
private $newFileName; //新文件名
private $errorNum = 0; //错误号
private $errorMess=""; //错误报告消息
/* 构造方法:为成员属性初使化
* 参数$options:为一个数组,数组下标为成员员属性名称字符串
* 本类需要初使化的属性有 filepath, allowtype, maxsize,israndname四个属性,其中filepath为必须设置的属性
* 使用的格式为 new FileUpload(array('filepath'=>'./uploads', 'maxsize'=>10000000)) 的格式
*/
function __construct($options=array()) {
foreach ($options as $key=>$val) {
$key=strtolower($key); //在为成员属性设置值时,不区分大小写
if (!in_array($key,get_class_vars(get_class($this))))
continue;
$this->setOption($key, $val);
}
} /* 调用该方法上传文件
* 参数: 上传文件的表单名称 例如:<input type="file" name="myfile"> 参数则为myfile
* 返回值: 如果上传成功返回数字0,如果上传失败则返回小于0的数,如:-1、-2、-3、-4、-5中的一个
*/ function uploadFile($fileField) {
$return=true;
if(!$this->checkFilePath()) {//检查文件路径
$this->errorMess=$this->getError();
return false;
}
$name=$_FILES[$fileField]['name'];
$tmp_name=$_FILES[$fileField]['tmp_name'];
$size=$_FILES[$fileField]['size'];
$error=$_FILES[$fileField]['error']; if(is_Array($name)){ //如果是多个文件上传则$file["name"]会是一个数组
$errors=array();
for($i = 0; $i < count($name); $i++){
if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i] )) {//设置文件信息
if(!$this->checkFileSize() || !$this->checkFileType()){
$errors[]=$this->getError();
$return=false;
}
}else{
$errors[]=$this->getError();
$return=false;
} if(!$return) // 如果有问题,则重新初使化属性
$this->setFiles();
} if($return){
$fileNames=array(); //存放所有上传后文件名的变量数组 for($i = 0; $i < count($name); $i++){
if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i] )) {//设置文件信息
$this->setNewFileName(); //设置新文件名
if(!$this->copyFile()){
$errors[]=$this->getError();
$return=false;
}
$fileNames[]=$this->newFileName;
} }
$this->newFileName=$fileNames; }
$this->errorMess=$errors;
return $return; } else {
if($this->setFiles($name,$tmp_name,$size,$error)) {//设置文件信息
if($this->checkFileSize() && $this->checkFileType()){
$this->setNewFileName(); //设置新文件名
if($this->copyFile()){ //上传文件 返回0为成功, 小于0都为错误
return true;
}else{
echo '3333333333333';
$return=false;
}
}else{
$return=false;
}
} else {
$return=false;
} if(!$return)
$this->errorMess=$this->getError(); return $return;
} } /* 获取上传后的文件名称
* 没有参数
* 返回值:上传后,新文件的名称
*/
public function getNewFileName(){
return $this->newFileName;
} public function getErrorMsg(){
return $this->errorMess;
} /* 上传失败后,调用该方法则返回,上传出错信息
* 没有参数
* 返回值:返回上传文件出错的信息提示
*/
private function getError() {
$str = "上传文件<font color='red'>{$this->originName}</font>时出错 : ";
switch ($this->errorNum) {
case 4: $str .= "没有文件被上传"; break;
case 3: $str .= "文件只有部分被上传"; break;
case 2: $str .= "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值"; break;
case 1: $str .= "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值"; break;
case -1: $str .= "未允许类型"; break;
case -2: $str .= "文件过大,上传的文件不能超过{$this->maxsize}个字节"; break;
case -3: $str .= "上传失败"; break;
case -4: $str .= "建立存放上传文件目录失败,请重新指定上传目录"; break;
case -5: $str .= "必须指定上传文件的路径"; break;
default: $str .= "未知错误";
}
return $str.'<br>';
} //设置和$_FILES有关的内容
private function setFiles($name="", $tmp_name="", $size=0, $error=0) {
$this->setOption('errorNum', $error);
if($error)
return false;
$this->setOption('originName', $name);
$this->setOption('tmpFileName',$tmp_name);
$aryStr = explode(".", $name);
$this->setOption('fileType', strtolower($aryStr[count($aryStr)-1]));
$this->setOption('fileSize', $size);
return true;
} //为单个成员属性设置值
private function setOption($key, $val) {
$this->$key = $val;
} //设置上传后的文件名称
private function setNewFileName() {
if ($this->israndname) {
$this->setOption('newFileName', $this->proRandName());
} else{
$this->setOption('newFileName', $this->originName);
}
} //检查上传的文件是否是合法的类型
private function checkFileType() {
if (in_array(strtolower($this->fileType), $this->allowtype)) {
return true;
}else {
$this->setOption('errorNum', -1);
return false;
}
}
//检查上传的文件是否是允许的大小
private function checkFileSize() {
if ($this->fileSize > $this->maxsize) {
$this->setOption('errorNum', -2);
return false;
}else{
return true;
}
} //检查是否有存放上传文件的目录
private function checkFilePath() {
if(empty($this->filepath)){
$this->setOption('errorNum', -5);
return false;
}
if (!file_exists($this->filepath) || !is_writable($this->filepath)) {
if (!@mkdir($this->filepath, 0755)) {
$this->setOption('errorNum', -4);
return false;
}
} return true;
}
//设置随机文件名
private function proRandName() {
$fileName=date('YmdHis')."_".rand(100,999); //获取随机文件名
return $fileName.'.'.$this->fileType; //返回文件名加原扩展名
} //复制上传文件到指定的位置
private function copyFile() {
if(!$this->errorNum) {
$filepath = rtrim($this->filepath, '/').'/';
$filepath .= $this->newFileName;
if (@move_uploaded_file($this->tmpFileName, $filepath)) {
return true;
}else{
$this->setOption('errorNum', -3);
return false;
}
} else {
return false;
} } }

FileClass

 <?php
include "FileUpload.class.php";
include "images.class.php"; $up = new FileUpload(array("filepath" => "./images/", "allowtype" => array("gif", "jpg","png"))); if($up->uploadFile("spic")){
$filename = $up->getNewFileName(); $img = new Image("./images"); $th_filename = $img->thumb($filename, 300,300, "th_"); $img->waterMark($th_filename, "gaolf/gif", 5, "wa_");
$img->waterMark($filename, "gaolf.gif", 0, "");
}else{
echo $up->getErrorMsg();
}
?>

upload

最新文章

  1. React Native图片控件的使用
  2. centos7.0 安装字体库
  3. PowerDesigner 逆向工程 从SQL文件转换成PDM 从PDM转成CDM
  4. 多台Linux服务器SSH相互访问无需密码--转
  5. 在objc项目中使用常量的最佳实践
  6. openwrt上opkg更新报错&amp;quot;opkg_download: Failed to download .............&amp;quot;
  7. mysql 存储过程 游标的使用 与定义
  8. JAVA课程设计 学生成绩管理
  9. vue路由--网站导航功能
  10. 深入理解java虚拟机(二)-----垃圾回收
  11. Freemarker 页面静态化技术使用入门案例
  12. Linux日志痕迹清除
  13. oracle数据库组件列表及相关的数据字典视图
  14. jquery之行自加自减
  15. Goldengate:ERROR 180 encountered commit SCN that is not greater than the highest SCN already processed
  16. Coursera-AndrewNg(吴恩达)机器学习笔记——第四周编程作业(多分类与神经网络)
  17. npm用法及离线安装方法
  18. jq实现鼠标经过出现上拉菜单
  19. [转载]C++内存管理
  20. 历届试题 小数第n位(小技巧)

热门文章

  1. Android 接入支付宝支付实现
  2. 问问javascript
  3. yii2.0安装ElasticSearch及使用
  4. OMD开源监控软件
  5. 怎样解决putty终端乱码的方法
  6. 08、Spark常用RDD变换
  7. webpack了解
  8. OpenGL纹理高级
  9. DOM笔记(十一):JavaScript对象的基本认识和创建
  10. 【CCPC-Wannafly Winter Camp Day4 (Div1) H】命命命运(概率DP)