phpファイルのアップロードコード


 
<?php
/**
*
*/
class uploadFile {
public $max_size = '1000000';//
public $file_name = 'date';// ,
public $allow_types;// , “|”
public $errmsg = '';//
public $uploaded = '';// ( )
public $save_path;//
private $files;//
private $file_type = array();//
private $ext = '';//
/**
* ,
* @access public
* @param string $file_name
* @param string $save_path
*/
public function __construct($save_path = './upload/',$file_name = 'date',$allow_types = '') {
$this->file_name = $file_name;// ,
$this->save_path = (preg_match('/\/$/',$save_path)) ? $save_path : $save_path . '/';
$this->allow_types = $allow_types == '' ? 'jpg|gif|png|zip|rar' : $allow_types;
}
/**
*
* @access public
* @param $files ( $_FILES[])
* @return boolean
*/
public function upload_file($files) {
$name = $files['name'];
$type = $files['type'];
$size = $files['size'];
$tmp_name = $files['tmp_name'];
$error = $files['error'];
switch ($error) {
case 0 : $this->errmsg = '';
break;
case 1 : $this->errmsg = ' php.ini ';
break;
case 2 : $this->errmsg = ' MAX_FILE_SIZE ';
break;
case 3 : $this->errmsg = ' ';
break;
case 4 : $this->errmsg = ' ';
break;
case 5 : $this->errmsg = ' 0';
break;
default : $this->errmsg = ' !';
break;
}
if($error == 0 && is_uploaded_file($tmp_name)) {
//
if($this->check_file_type($name) == FALSE){
return FALSE;
}
//
if($size > $this->max_size){
$this->errmsg = ' <font color=red>'.$name.'</font> , <font color=red>'.ceil($this->max_size/1024).'</font>kb ';
return FALSE;
}
$this->set_save_path();//
$new_name = $this->file_name != 'date' ? $this->file_name.'.'.$this->ext : date('YmdHis').'.'.$this->ext;//
$this->uploaded = $this->save_path.$new_name;//
//
if(move_uploaded_file($tmp_name,$this->uploaded)){
$this->errmsg = ' <font color=red>'.$this->uploaded.'</font> !';
return TRUE;
}else{
$this->errmsg = ' <font color=red>'.$this->uploaded.'</font> !';
return FALSE;
}
}
}
/**
*
* @access public
* @param string $filename
* @return TRUE FALSE
*/
public function check_file_type($filename){
$ext = $this->get_file_type($filename);
$this->ext = $ext;
$allow_types = explode('|',$this->allow_types);//
//echo $ext;
//
if(in_array($ext,$allow_types)){
return TRUE;
}else{
$this->errmsg = ' <font color=red>'.$filename.'</font> , <font color=red>'.str_replace('|',',',$this->allow_types).'</font> !';
return FALSE;
}
}
/**
*
* @access public
* @param string $filename
* @return string
*/
public function get_file_type($filename){
$info = pathinfo($filename);
$ext = $info['extension'];
return $ext;
}
/**
*
*/
public function set_save_path(){
$this->save_path = (preg_match('/\/$/',$this->save_path)) ? $this->save_path : $this->save_path . '/';
if(!is_dir($this->save_path)){
// ,
$this->set_dir();
}
}
/**
*
* @access public
* @param string $dir
* @return boolean FALSE
*/
public function set_dir($dir = null){
//
if(!$dir){
$dir = $this->save_path;
}
if(is_dir($dir)){
$this->errmsg = ' !';
}
$dir = explode('/', $dir);
foreach($dir as $v){
if($v){
$d .= $v . '/';
if(!is_dir($d)){
$state = mkdir($d, 0777);
if(!$state)
$this->errmsg = ' <font color=red>' . $d . ' !';
}
}
}
return true;
}
}
/*************************************************
*
*
* ,
* UTF8 GBK img_mark iconv
*
* UTF8 ( ) ,
* , get_mark_xy $strc_w = strlen($this->mark_str)*7+5
*
* GD , GD 2.0+
*
* @author kickflip@php100 QQ263340607
*************************************************/
class uploadImg extends uploadFile {
public $mark_str = 'kickflip@php100'; //
public $str_r = 0; // R
public $str_g = 0; // G
public $str_b = 0; // B
public $mark_ttf = './upload/SIMSUN.TTC'; // ( )
public $mark_logo = './upload/logo.png'; //
public $resize_h;//
public $resize_w;//
public $source_img;//
public $dst_path = './upload/';// ,
/**
*
* @access public
* @param integer $w (px)
* @param integer $h (px)
* @param string $source_img ( + )
*/
public function img_resized($w,$h,$source_img = NULL){
$source_img = $source_img == NULL ? $this->uploaded : $source_img;// ,
if(!is_file($source_img)) { //
$this->errmsg = ' '.$source_img.' ';
return FALSE;
}
$this->source_img = $source_img;
$img_info = getimagesize($source_img);
$source = $this->img_create($source_img); //
$this->resize_w = $w;
$this->resize_h = $h;
$thumb = imagecreatetruecolor($w,$h);
imagecopyresized($thumb,$source,0,0,0,0,$w,$h,$img_info[0],$img_info[1]);//
$dst_path = $this->dst_path == '' ? $this->save_path : $this->dst_path; //
$dst_path = (preg_match('/\/$/',$dst_path)) ? $dst_path : $dst_path . '/';// /
if(!is_dir($dst_path)) $this->set_dir($dst_path); //
$dst_name = $this->set_newname($source_img);
$this->img_output($thumb,$dst_name);//
imagedestroy($source);
imagedestroy($thumb);
}
/**
*
*@access public
*@param string $source_img +
*@param integer $mark_type (1 ,2 ,3 logo, )
*@param integer $mark_postion (1 ,2 ,3 ,4 , );
*@return
*/
public function img_mark($source_img = NULL,$mark_type = 1,$mark_postion = 2) {
$source_img = $source_img == NULL ? $this->uploaded : $source_img;// ,
if(!is_file($source_img)) { //
$this->errmsg = ' '.$source_img.' ';
return FALSE;
}
$this->source_img = $source_img;
$img_info = getimagesize($source_img);
$source = $this->img_create($source_img); //
$mark_xy = $this->get_mark_xy($mark_postion);//
$mark_color = imagecolorallocate($source,$this->str_r,$this->str_g,$this->str_b);
switch($mark_type) {
case 1 : //
$str = $this->mark_str;
imagestring($source,5,$mark_xy[0],$mark_xy[1],$str,$mark_color);
$this->img_output($source,$source_img);
break;
case 2 : //
if(!is_file($this->mark_ttf)) { //
$this->errmsg = ' : '.$this->mark_ttf.' !';
return FALSE;
}
$str = $this->mark_str;
//$str = iconv('gbk','utf-8',$str);// GBK
imagettftext($source,12,0,$mark_xy[2],$mark_xy[3],$mark_color,$this->mark_ttf,$str);
$this->img_output($source,$source_img);
break;
case 3 : //
if(is_file($this->mark_logo)){ // logo logo ,
$logo_info = getimagesize($this->mark_logo);
}else{
$this->errmsg = ' :logo '.$this->mark_logo.' !';
return FALSE;
}
$logo_info = getimagesize($this->mark_logo);
if($logo_info[0]>$img_info[0] || $logo_info[1]>$img_info[1]) { // logo
$this->errmsg = ' : '.$this->source_img.' '.$this->mark_logo.' !';
return FALSE;
}
$logo = $this->img_create($this->mark_logo);
imagecopy ( $source, $logo, $mark_xy[4], $mark_xy[5], 0, 0, $logo_info[0], $logo_info[1]);
$this->img_output($source,$source_img);
break;
default: //
$str = $this->mark_str;
imagestring($source,5,$mark_xy[0],$mark_xy[1],$str,$mark_color);
$this->img_output($source,$source_img);
break;
}
imagedestroy($source);
}
/**
*
* @access private
* @param integer $mark_postion (1 ,2 ,3 ,4 , )
* @return array $mark_xy ( 0 X, 1 Y,
* 2 X, 3 Y, 4 X, 5 Y)
*/
private function get_mark_xy($mark_postion){
$img_info = getimagesize($this->source_img);
$stre_w = strlen($this->mark_str)*9+5 ; // (px)(5 9px 5px)
//(12 12px, utf8 3 4px 9px
// *7px)
$strc_w = strlen($this->mark_str)*7+5 ; // (px)
if(is_file($this->mark_logo)){ // logo logo
$logo_info = getimagesize($this->mark_logo);
}
// imagestring imagettftext Y
//imagestring imagettftext
switch($mark_postion){
case 1: //
$mark_xy[0] = 5; // X
$mark_xy[1] = $img_info[1]-20;// Y
$mark_xy[2] = 5; // X
$mark_xy[3] = $img_info[1]-5;// Y
$mark_xy[4] = 5;// X
$mark_xy[5] = $img_info[1]-$logo_info[1]-5;// Y
break;
case 2: //
$mark_xy[0] = $img_info[0]-$stre_w; // X
$mark_xy[1] = $img_info[1]-20;// Y
$mark_xy[2] = $img_info[0]-$strc_w; // X
$mark_xy[3] = $img_info[1]-5;// Y
$mark_xy[4] = $img_info[0]-$logo_info[0]-5;// X
$mark_xy[5] = $img_info[1]-$logo_info[1]-5;// Y
break;
case 3: //
$mark_xy[0] = 5; // X
$mark_xy[1] = 5;// Y
$mark_xy[2] = 5; // X
$mark_xy[3] = 15;// Y
$mark_xy[4] = 5;// X
$mark_xy[5] = 5;// Y
break;
case 4: //
$mark_xy[0] = $img_info[0]-$stre_w; // X
$mark_xy[1] = 5;// Y
$mark_xy[2] = $img_info[0]-$strc_w; // X
$mark_xy[3] = 15;// Y
$mark_xy[4] = $img_info[0]-$logo_info[0]-5;// X
$mark_xy[5] = 5;// Y
break;
default : //
$mark_xy[0] = $img_info[0]-$stre_w; // X
$mark_xy[1] = $img_info[1]-5;// Y
$mark_xy[2] = $img_info[0]-$strc_w; // X
$mark_xy[3] = $img_info[1]-15;// Y
$mark_xy[4] = $img_info[0]-$logo_info[0]-5;// X
$mark_xy[5] = $img_info[1]-$logo_info[1]-5;// Y
break;
}
return $mark_xy;
}
/**
*
* @access private
* @param string $source_img ( + )
* @return img
*/
private function img_create($source_img) {
$info = getimagesize($source_img);
switch ($info[2]){
case 1:
if(!function_exists('imagecreatefromgif')){
$source = @imagecreatefromjpeg($source_img);
}else{
$source = @imagecreatefromgif($source_img);
}
break;
case 2:
$source = @imagecreatefromjpeg($source_img);
break;
case 3:
$source = @imagecreatefrompng($source_img);
break;
case 6:
$source = @imagecreatefromwbmp($source_img);
break;
default:
$source = FALSE;
break;
}
return $source;
}
/**
*
* @access private
* @param string $source_img +
* @return string $dst_name ( + )
*/
private function set_newname($sourse_img) {
$info = pathinfo($sourse_img);
$new_name = $this->resize_w.'_'.$this->resize_h.'_'.$info['basename'];// : _ _
if($this->dst_path == ''){ //
$dst_name = str_replace($info['basename'],$new_name,$sourse_img);
}else{
$dst_name = $this->dst_path.$new_name;
}
return $dst_name;
}
/**
*
* @access private
* @param $im
* @param $dst_name ( + )
* @return
*/
public function img_output($im,$dst_name) {
$info = getimagesize($this->source_img);
switch ($info[2]){
case 1:
if(!function_exists('imagegif')){
imagejpeg($im,$dst_name);
}else{
imagegif($im, $dst_name);
}
break;
case 2:
imagejpeg($im,$dst_name);
break;
case 3:
imagepng($im,$dst_name);
break;
case 6:
imagewbmp($im,$dst_name);
break;
}
}
}
?>
phpファイルのアップロードのソースコードの包装