ÒýÓãº
<?php include("UploadFile.php"); if ($_GET['action'] == 'save') { $up = new upload(); $up->set_dir(dirname(__FILE__).'/upload/','{y}/{m}'); $up->set_thumb(100,80); $up->set_watermark(dirname(__FILE__).'/jblog/images/watermark.png',6,90); $fs = $up->execute(); //var_dump($fs); } ?> <html> <head><title>test</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body style="margin:0;padding:0"> <form name="upload" method="post" action="?action=save" enctype="multipart/form-data" style="margin:0"> <input type="file" name="attach[]" /> <input type="file" name="attach[]" /> <input type="submit" name="submit" value="ÉÏ ´«" /> </form> </body> </html>ÀàÎļþ£º
<?php /** +---------------------------------------------------------- * franklin ÎļþÉÏ´«´¦ÀíÀà +---------------------------------------------------------- * ÎļþÃû³Æ UploadFile.php +---------------------------------------------------------- * ÎļþÃèÊö ÎļþÉÏ´«´¦ÀíÀà +---------------------------------------------------------- * ×÷ Õß franklin<franklin@co-insight.com> +---------------------------------------------------------- * ʱ ¼ä 2011-1-11 +---------------------------------------------------------- */ class upload { var $dir; //¸½¼þ´æ·ÅÎïÀíĿ¼ var $time; //×Ô¶¨ÒåÎļþÉÏ´«Ê±¼ä var $allow_types; //ÔÊÐíÉÏ´«¸½¼þÀàÐÍ var $field; //ÉÏ´«¿Ø¼þÃû³Æ var $maxsize; //×î´óÔÊÐíÎļþ´óС£¬µ¥Î»ÎªKB var $thumb_width; //ËõÂÔͼ¿í¶È var $thumb_height; //ËõÂÔͼ¸ß¶È var $watermark_file; //ˮӡͼƬµØÖ· var $watermark_pos; //ˮӡλÖà var $watermark_trans;//ˮӡ͸Ã÷¶È //¹¹Ô캯Êý //$types : ÔÊÐíÉÏ´«µÄÎļþÀàÐÍ , $maxsize : ÔÊÐí´óС , $field : ÉÏ´«¿Ø¼þÃû³Æ , $time : ×Ô¶¨ÒåÉÏ´«Ê±¼ä function upload($types = 'gif|jpg|png', $maxsize = 1024, $field = 'attach', $time = '') { $this->allow_types = explode('|',$types); $this->maxsize = $maxsize * 1024; $this->field = $field; $this->time = $time ? $time : time(); } //ÉèÖò¢´´½¨Îļþ¾ßÌå´æ·ÅµÄĿ¼ //$basedir : »ùĿ¼£¬±ØÐëΪÎïÀí·¾¶ //$filedir : ×Ô¶¨Òå×ÓĿ¼£¬¿ÉÓòÎÊý{y}¡¢{m}¡¢{d} function set_dir($basedir,$filedir = '') { $dir = $basedir; !is_dir($dir) && @mkdir($dir,0777); if (!emptyempty($filedir)) { $filedir = str_replace(array('{y}','{m}','{y}'),array(date('Y',$this->time),date('m',$this->time),date('d',$this->time)),strtolower($filedir)); $dirs = explode('/',$filedir); foreach ($dirs as $d) { !emptyempty($d) && $dir .= $d.'/'; !is_dir($dir) && @mkdir($dir,0777); } } $this->dir = $dir; } //ͼƬËõÂÔͼÉèÖã¬Èç¹û²»Éú³ÉËõÂÔͼÔò²»ÓÃÉèÖà //$width : ËõÂÔͼ¿í¶È , $height : ËõÂÔͼ¸ß¶È function set_thumb ($width = 0, $height = 0) { $this->thumb_width = $width; $this->thumb_height = $height; } //ͼƬˮӡÉèÖã¬Èç¹û²»Éú³ÉÌí¼ÓˮӡÔò²»ÓÃÉèÖà //$file : ˮӡͼƬ , $pos : ˮӡλÖà , $trans : ˮӡ͸Ã÷¶È function set_watermark ($file, $pos = 6, $trans = 80) { $this->watermark_file = $file; $this->watermark_pos = $pos; $this->watermark_trans = $trans; } /*---------------------------------------------------------------- Ö´ÐÐÎļþÉÏ´«£¬´¦ÀíÍê·µ»ØÒ»¸ö°üº¬ÉÏ´«³É¹¦»òʧ°ÜµÄÎļþÐÅÏ¢Êý×飬 ÆäÖУºname ΪÎļþÃû£¬ÉÏ´«³É¹¦Ê±ÊÇÉÏ´«µ½·þÎñÆ÷ÉϵÄÎļþÃû£¬ÉÏ´«Ê§°ÜÔòÊDZ¾µØµÄÎļþÃû dir Ϊ·þÎñÆ÷ÉÏ´æ·Å¸Ã¸½¼þµÄÎïÀí·¾¶£¬ÉÏ´«Ê§°Ü²»´æÔÚ¸ÃÖµ size Ϊ¸½¼þ´óС£¬ÉÏ´«Ê§°Ü²»´æÔÚ¸ÃÖµ flag Ϊ״̬±êʶ£¬1±íʾ³É¹¦£¬-1±íʾÎļþÀàÐͲ»ÔÊÐí£¬-2±íʾÎļþ´óС³¬³ö -----------------------------------------------------------------*/ function execute() { $files = array(); //³É¹¦ÉÏ´«µÄÎļþÐÅÏ¢ $field = $this->field; $keys = array_keys($_FILES[$field]['name']); foreach ($keys as $key) { if (!$_FILES[$field]['name'][$key]) continue; $fileext = $this->fileext($_FILES[$field]['name'][$key]); //»ñÈ¡ÎļþÀ©Õ¹Ãû $filename = $this->time.mt_rand(100,999).'.'.$fileext; //Éú³ÉÎļþÃû $filedir = $this->dir; //¸½¼þʵ¼Ê´æ·ÅĿ¼ $filesize = $_FILES[$field]['size'][$key]; //Îļþ´óС //ÎļþÀàÐͲ»ÔÊÐí if (!in_array($fileext,$this->allow_types)) { $files[$key]['name'] = $_FILES[$field]['name'][$key]; $files[$key]['flag'] = -1; continue; } //Îļþ´óС³¬³ö if ($filesize > $this->maxsize) { $files[$key]['name'] = $_FILES[$field]['name'][$key]; $files[$key]['flag'] = -2; continue; } $files[$key]['name'] = $filename; $files[$key]['dir'] = $filedir; $files[$key]['size'] = $filesize; //±£´æÉÏ´«Îļþ²¢É¾³ýÁÙʱÎļþ if (is_uploaded_file($_FILES[$field]['tmp_name'][$key])) { move_uploaded_file($_FILES[$field]['tmp_name'][$key],$filedir.$filename); echo $filedir.$filename; @unlink($_FILES[$field]['tmp_name'][$key]); $files[$key]['flag'] = 1; //¶ÔͼƬ½øÐмÓˮӡºÍÉú³ÉËõÂÔͼ if (in_array($fileext,array('jpg','png','gif'))) { if ($this->thumb_width) { if ($this->create_thumb($filedir.$filename,$filedir.'thumb_'.$filename)) { $files[$key]['thumb'] = 'thumb_'.$filename; //ËõÂÔͼÎļþÃû } } $this->create_watermark($filedir.$filename); } } } return $files; } //´´½¨ËõÂÔͼ,ÒÔÏàͬµÄÀ©Õ¹ÃûÉú³ÉËõÂÔͼ //$aspx_file : À´Ô´Í¼Ïñ·¾¶ , $thumb_file : ËõÂÔͼ·¾¶ function create_thumb($aspx_file,$thumb_file) { $t_width = $this->thumb_width; $t_height = $this->thumb_height; if (!file_exists($aspx_file)) return false; $aspx_info = getimagesize($aspx_file); //Èç¹ûÀ´Ô´Í¼ÏñСÓÚ»òµÈÓÚËõÂÔͼÔò¿½±´Ô´Í¼Ïñ×÷ΪËõÂÔͼ if ($aspx_info[0] <= $t_width && $aspx_info[1] <= $t_height) { if (!copy($aspx_file,$thumb_file)) { return false; } return true; } //°´±ÈÀý¼ÆËãËõÂÔͼ´óС if ($aspx_info[0] - $t_width > $aspx_info[1] - $t_height) { $t_height = ($t_width / $aspx_info[0]) * $aspx_info[1]; } else { $t_width = ($t_height / $aspx_info[1]) * $aspx_info[0]; } //È¡µÃÎļþÀ©Õ¹Ãû $fileext = $this->fileext($aspx_file); switch ($fileext) { case 'jpg' : $aspx_img = imagecreatefromjpeg($aspx_file); break; case 'png' : $aspx_img = imagecreatefrompng($aspx_file); break; case 'gif' : $aspx_img = imagecreatefromgif($aspx_file); break; } //´´½¨Ò»¸öÕæ²ÊÉ«µÄËõÂÔͼÏñ $thumb_img = @imagecreatetruecolor($t_width,$t_height); //imagecopyresampledº¯Êý¿½±´µÄͼÏñƽ»¬¶È½ÏºÃ£¬ÓÅÏÈ¿¼ÂÇ if (function_exists('imagecopyresampled')) { @imagecopyresampled($thumb_img,$aspx_img,0,0,0,0,$t_width,$t_height,$aspx_info[0],$aspx_info[1]); } else { @imagecopyresized($thumb_img,$aspx_img,0,0,0,0,$t_width,$t_height,$aspx_info[0],$aspx_info[1]); } //Éú³ÉËõÂÔͼ switch ($fileext) { case 'jpg' : imagejpeg($thumb_img,$thumb_file); break; case 'gif' : imagegif($thumb_img,$thumb_file); break; case 'png' : imagepng($thumb_img,$thumb_file); break; } //Ïú»ÙÁÙʱͼÏñ @imagedestroy($aspx_img); @imagedestroy($thumb_img); return true; } //ΪͼƬÌí¼Óˮӡ //$file : ÒªÌí¼ÓˮӡµÄÎļþ function create_watermark ($file) { //Îļþ²»´æÔÚÔò·µ»Ø if (!file_exists($this->watermark_file) || !file_exists($file)) return; if (!function_exists('getimagesize')) return; //¼ì²éGDÖ§³ÖµÄÎļþÀàÐÍ $gd_allow_types = array(); if (function_exists('imagecreatefromgif')) $gd_allow_types['image/gif'] = 'imagecreatefromgif'; if (function_exists('imagecreatefrompng')) $gd_allow_types['image/png'] = 'imagecreatefrompng'; if (function_exists('imagecreatefromjpeg')) $gd_allow_types['image/jpeg'] = 'imagecreatefromjpeg'; //»ñÈ¡ÎļþÐÅÏ¢ $fileinfo = getimagesize($file); $wminfo = getimagesize($this->watermark_file); if ($fileinfo[0] < $wminfo[0] || $fileinfo[1] < $wminfo[1]) return; if (array_key_exists($fileinfo['mime'],$gd_allow_types)) { if (array_key_exists($wminfo['mime'],$gd_allow_types)) { //´ÓÎļþ´´½¨Í¼Ïñ $temp = $gd_allow_types[$fileinfo['mime']]($file); $temp_wm = $gd_allow_types[$wminfo['mime']]($this->watermark_file); //ˮӡλÖà switch ($this->watermark_pos) { case 1 : //¶¥²¿¾Ó×ó $dst_x = 0; $dst_y = 0; break; case 2 : //¶¥²¿¾ÓÖÐ $dst_x = ($fileinfo[0] - $wminfo[0]) / 2; $dst_y = 0; break; case 3 : //¶¥²¿¾ÓÓÒ $dst_x = $fileinfo[0]; $dst_y = 0; break; case 4 : //µ×²¿¾Ó×ó $dst_x = 0; $dst_y = $fileinfo[1]; break; case 5 : //µ×²¿¾ÓÖÐ $dst_x = ($fileinfo[0] - $wminfo[0]) / 2; $dst_y = $fileinfo[1]; break; case 6 : //µ×²¿¾ÓÓÒ $dst_x = $fileinfo[0]-$wminfo[0]; $dst_y = $fileinfo[1]-$wminfo[1]; break; default : //Ëæ»ú $dst_x = mt_rand(0,$fileinfo[0]-$wminfo[0]); $dst_y = mt_rand(0,$fileinfo[1]-$wminfo[1]); } if (function_exists('imagealphablending')) imagealphablending($temp_wm,True); //É趨ͼÏñµÄ»ìɫģʽ if (function_exists('imagesavealpha')) imagesavealpha($temp_wm,True); //±£´æÍêÕûµÄ alpha ͨµÀÐÅÏ¢ //ΪͼÏñÌí¼Óˮӡ if (function_exists('imagecopymerge')) { imagecopymerge($temp,$temp_wm,$dst_x,$dst_y,0,0,$wminfo[0],$wminfo[1],$this->watermark_trans); } else { imagecopymerge($temp,$temp_wm,$dst_x,$dst_y,0,0,$wminfo[0],$wminfo[1]); } //±£´æͼƬ switch ($fileinfo['mime']) { case 'image/jpeg' : @imagejpeg($temp,$file); break; case 'image/png' : @imagepng($temp,$file); break; case 'image/gif' : @imagegif($temp,$file); break; } //Ïú»ÙÁãʱͼÏñ @imagedestroy($temp); @imagedestroy($temp_wm); } } } //»ñÈ¡ÎļþÀ©Õ¹Ãû function fileext($filename) { return strtolower(substr(strrchr($filename,'.'),1,10)); } } ?>
±¾´úÂë²âÊÔͨ¹ý: ÏÂÔØÉÏ´«´¦ÀíÀà´úÂë
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- php³£ÓÃÕýÔò±í´ïʽ
- phpÐÔÄܼà²âÄ£¿éXHProf
- ÈÃCI¿ò¼ÜÖ§³Öservice²ã
- ʹÓÃPHPÉú³É´øLOGOµÄ¸öÐÔ»¯¶þάÂëͼÏñ
- ¹ØÓÚCodeIgniterÄã¿ÉÄܲ»ÖªµÀµÄ5¸ö֪ʶµã
- Memcache ºÁÃ뼶³¬Ê±¼°ÆäËû³£¼ûÎÊÌâ»ã×Ü
- [PHP±Ê¼Ç]PHPQueryÒ»¸ö´¦ÀíDOMµÄÀûÆ÷
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- PHP»ñÈ¡Óû§µÄÕæʵIP£¬²¢ÅжÏÊÇ·ñÄÚÍøIP
- PHP ´íÎóÈÕÖ¾ error_log
- ÀûÓÃbigpipe»úÖÆʵÏÖÒ³ÃæÄ£¿éµÄÒì²½äÖȾ chunked¼¼Êõ
- php¿ØÖÆÎļþÏÂÔØËÙ¶È
- js + php ¶ÁÈ¡¡¢²¥·ÅÊÓƵÁ÷ ¼æÈÝfirefox£¬c....
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- UNICODE Óë UTF-8 µÄ¹Øϵ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- phpʹÓÃmb_detect_encoding¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ