简单的php图片上传类_php
2012-07-27 10:35
555 查看
某天突然做一个图片上传的功能,然后就萌发了写一个简单的php图片上传类的想法,呵呵,代码还有很多不足的地方,希望各位大侠批评指出。。
1.增加水印选项
2.功能还在完善。。。
<?php /* * {by}D.n.sky * {description}图片上传类 */ class UnloadImage{ /* 文件域 */ public $file; /* 上传文件保存路径 */ public $img_path = '../image-data-all'; /* 是否器启用时间文件夹命名来储存图片 */ public $is_fun_datefile = true; /* 时候覆盖相同文件名文件 */ public $is_cover = true; /* 是否自动命名文件 */ public $is_auto_sove = false; /* 是否增加图片水印 */ public $is_watermark = true; /* 放缩图像 */ public $is_brevimage = true; /* 允许上传图片大小 */ public $img_size = 102400; /* 允许上传图片格式 */ public $img_type = array( 'image/jpg', 'image/jpeg', 'image/png', 'image/pjpeg', 'image/gif', 'image/bmp', 'image/x-png'); /* 最终图片保存路径 */ public $sove_new_name; /* 图片信息 */ public $info; public $info_type; public $info_size; public $info_name; public $info_tmp; /* 构造函数 */ function __construct(){ } /* 析构函数 */ function __destruct(){ } /* 读取文件信息 */ public function set_image_info($file, $img_path){ if($file != ''){ $file = $_FILES[$file]; $this -> info_name = $file['name']; #1.jpg $this -> info_size = $file['size']; #13456 $this -> info_type = $file['type']; #image/jpeg $this -> info_tmp = $file['tmp_name']; #C:\Windows\Temp\php3CFC.tmp if($img_path != '') $this -> img_path = $img_path; /* 获取原始图像尺寸 */ $this -> info = getimagesize($this -> info_tmp); } } /* 检测和创建目录 */ public function auto_path(){ if($this -> is_fun_datefile){ /* 生成日期文件夹 */ $d = date('Ymd'); /* 生成日期文件夹 */ $this -> img_path = $this -> img_path .'/'. $d ; } if(!file_exists($this -> img_path)){ mkdir($this -> img_path); } } /* 判断文件类型 */ public function check_type(){ if(!in_array($this-> info_type, $this->img_type )){ $this -> set_msg(array(0, 'image type is not this.', '')); } } /* 判断图片大小 */ public function check_size(){ if($this -> info_size > $this -> img_size){ $this -> set_msg(array(0, 'image size is >' .$this -> img_size. '.', '')); } } /* 上传图片 */ public function start_upload_image(){ $img_sove_path = $this -> img_path .'/'. $this -> info_name; /* 自动重命名 */ if($this -> is_auto_sove){ $img_sove_path = $this -> img_path .'/'. date('YmdHis') . $this -> file_sign(); } /* 文件是否存在 */ $fi = file_exists($img_sove_path); if(!$fi || ($fi && $this -> is_cover)){ /* 上传文件 */ if(!move_uploaded_file($this -> info_tmp, $img_sove_path)){ $this -> set_msg(array(0, 'error', null)); }else{ $this -> set_msg(array(1, 'ok', 'date' => array( 'imageurl' => $img_sove_path, 'width' => $this -> info[0], 'height' =>$this -> info[1] ))); } } $this-> sove_new_name = $img_sove_path; } /* 水印 */ public function makewater(){ /* 获取上传完成的图片 */ $success_image = $this -> create(); $str = 'www.desetang.com'; /* 创建一个等宽的图像 */ $water_image = imagecreatetruecolor($this -> info[1], 40); $alpha = imagecolorallocatealpha($water_image, 255, 255, 255, 90); $color = imagecolorallocate($water_image, 255, 255, 0); imagefill($water_image, 0, 0, $alpha); imagettftext($water_image, 16, 4, 0, 40, $color, 'msyh.ttf', $str); imagecopy($success_image, $water_image, 0, $this -> info[1]-40, 0, 0, $this -> info[0], 40); /* 重新保存水印后的图片 */ $this -> copyimage($success_image); /* 释放缓存 */ imagedestroy($success_image); imagedestroy($water_image); } /* 输出后缀 */ private function file_sign(){ return strrchr($this -> info_name, '.'); } /* 创建/保存水印图片 */ private function create(){ switch($this -> info[2]){ case 1: # GIF image $timg = imagecreatefromgif($this-> sove_new_name); break; case 2: # JPEG image $timg = imagecreatefromjpeg($this-> sove_new_name); break; case 3: # PNG image $timg = imagecreatefrompng($this-> sove_new_name); break; } return $timg; } /* 输出/保存图片 */ private function copyimage($simage){ switch($this -> info[2]){ case 1: # GIF image imagegif($simage, $this-> sove_new_name); break; case 2: # JPEG image imagejpeg($simage, $this-> sove_new_name); break; case 3: # PNG image imagepng($simage, $this-> sove_new_name); break; } } /* 上传文件 */ public function save_image(){ /* 检测图片保存路径 */ $this -> auto_path(); /* 检测文件类型 */ $this -> check_type(); /* 检测图片大小 */ $this -> check_size(); /* 上传 */ $this -> start_upload_image(); /* 执行水印 */ if($this -> is_watermark) $this-> makewater(); return 1; } /* error */ public function set_msg($error){ /* 如果是错误则停止代码 */ if($error[0] == 0) exit(); } } ?>
1.增加水印选项
2.功能还在完善。。。
相关文章推荐
- 一款简单PHP图片上传类带图片显示代码
- php 简单文件图片上传类
- PHP最简单多图片上传
- php 简单实现 图片压缩
- PHP导出word文档的简单实现方法(可导出图片)
- 一个超简单的php上传类代码
- 编写还可以的php 图片上传类代码
- PHP中简单图片处理类
- php简单实现批量上传图片的方法
- 做简单的插入图片代码 PHP+Mysql
- PHP的GD库学习1:一个简单的随机数图片
- php图片上传类 附调用方法
- 可嵌入php程序的一款简单实用的jquery图片裁切自由缩放插件,croppic
- PHP-简单分页和文件上传类
- 利用php实现图片相似度搜索的简单原理
- 一个最简单的php上传图片示例
- PHP图片上传类带图片显示
- php随机显示图片的简单示例