<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/> <TITLE>UPLOADX demo</title>
<body> <form enctype= "Multipart/form-data" action= "uploadx.php" method= "POST" > <input name= "uploadx[]" type= "file" > <br/> <input name= "uploadx[]" type= "file" > <br/> <input name= "uploadx[]" type= "file" > <br/> <input name= "uploadx[]" type= "file" > <br/> <br/> <input type= "Submit" value= "Upload selected Files" > </form> </body>
<?php Require ('./classes/uploadx.class.php '); $UPLOADX = new Uploadx; $UPLOADX->save = './temp '; $UPLOADX->name = ' auto '; $UPLOADX->mini = ' 200,200,mini '; $UPLOADX->mark = './images/logo.png,0,60 '; Print_r ($uploadx->mini ('./temp/2.jpg ')); uploadx.class.php <?php Class UPLOADX { var $form = ' UPLOADX '; var $save = './'; var $size = ' 1024 '; var $type = ' gif,bmp,png,jpg,jpeg,swf,flv,mp3,wma,rar,zip,7z,doc,docx,ppt,pptx,xls,xlsx,txt,pdf '; var $name = null; var $mini = null; var $mark = null; var $version = ' 2.0 '; The public function is ($type = True) { foreach ($this->files () as $key => $val) { $file = $mini = null; $file = $this->saves ($val [' name '], $val [' type '], $val [' Tmp_name '], $val [' size '], $val [' Error ']]; $file [' Code '] | | $file [' path '] = RTrim ($this->save, '/'). ' /'. $file [' name ']; $file [' Code '] | | $file [' mini '] = $this->mini ($file [' path ']); $file [' Code '] | | $file [' mark '] = $this->mark ($file [' path ']); $file [' Code '] && $file [' error '] = $this->error ($file [' Code ']); $type? $files [] = $file: ($file [' Code '] | | | $files [] = $file); } return Isset ($files)? $files: Array (); } Private function files () { if (count ($_files[$this->form]) <1) return array (); if (Is_array ($_files[$this->form][' name ')) { For ($i =0 $i <count ($_files[$this->form][' name '); $i + +) { if ($_files[$this->form][' error '] [$i]==4) continue; $files [] = Array ( ' Name ' =>$_files[$this->form][' name ' [$i], ' Type ' =>$_files[$this->form][' type ' [$i], ' Tmp_name ' =>$_files[$this->form][' tmp_name ' [$i], ' Error ' =>$_files[$this->form][' ERROR ' [$i], ' Size ' =>$_files[$this->form][' size '] [$i]); } }else{ $files [] = $_files[$this->form]; } return $files; } Private function saves ($name, $type, $temp, $size, $error) { if ($error) return Array (' name ' => $name, ' code ' => $error); $prefix = Strtolower (PathInfo ($name, pathinfo_extension)); if (!in_array ($prefix, Explode (', ', Strtolower ($this->type))) { Return Array (' name ' => $name, ' Code ' => '-1 '); } if ($size/1024> $this->size) { Return Array (' name ' => $name, ' Code ' => '-2 '); } if (!is_dir ($this->save)) { if (!mkdir ($this->save, 0777, TRUE)) { Return Array (' name ' => $name, ' Code ' => '-3 '); } } $filename = $this->name? ($this->name== ' auto ' uniqid (): $this->name): Trim (basename ($name, $prefix), '. '); $savefile = Trim ($this->save, '/'). ' /'. $filename. '. ' $prefix; if (! @move_uploaded_file ($temp, $savefile)) { Return Array (' name ' => $name, ' Code ' => '-4 '); } @chmod ($savefile, 0777); Return Array (' name ' => $filename. '. ') $prefix, ' code ' =>0); } Public Function Mini ($file = null) { if (! $file | |! $this->mini) return false; if (!is_file ($file)) return $this->error ( -5, $file); List ($width, $height, $extends) = Explode (', ', $this->mini); $types = Array (' gif ', ' png ', ' jpg ', ' jpeg '); $type = PathInfo ($file, pathinfo_extension); if (!in_array ($type, $types)) return $this->error (-6); if (!is_file ($file)) return $this->error ( -5, $file); $mini = $extends? BaseName ($file, $type). $extends. $type: Trim (basename ($file), '. '); $image = imagecreatefromstring (file_get_contents ($file)); $imagex = Imagesx ($image); $imagey = Imagesy ($image); $scale = $width/$imagex; if ($width > $imagex) { $mini _width = $imagex; $mini _height = $imagey; }else{ $mini _width = $width; $mini _height = Round ($scale * $imagey); } if (function_exists (' Imagecreatetruecolor ') && function_exists (' imagecopyresampled ')) { $temp = Imagecreatetruecolor ($mini _width, $mini _height); Imagecopyresampled ($temp, $image, 0,0,0,0, $mini _width, $mini _height, $imagex, $imagey); }else{ $temp = Imagecreate ($mini _width, $mini _height); Imagecopyresized ($temp, $image, 0,0,0,0, $mini _width, $mini _height, $imagex, $imagey); } Imagejpeg ($temp, RTrim ($this->save, '/'). ' /'. $mini, 100); Imagedestroy ($temp); Imagedestroy ($image); Return Is_file (RTrim ($this->save, '/'). ' /'. $mini)? $mini: false; } Public Function mark ($file = null) { if (! $file | |! $this->mark) return false; List ($watermark, $position, $opacity) = Explode (', ', $this->mark); if (!is_file ($file) | | |!is_file ($watermark)) return $this->error ( -5, ' file= '. $file. ' | | Watermark= '. $watermark); $type = PathInfo ($file, pathinfo_extension); $types = Array (' gif ', ' png ', ' jpg ', ' jpeg '); if (!in_array ($type, $types)) return $this->error ( -6, $file); $opacity = min ($opacity, 100); $file _data = imagecreatefromstring (file_get_contents ($file)); $file _width = imagesx ($file _data); $file _height = Imagesy ($file _data); if (In_array (PathInfo ($watermark, pathinfo_extension), array (' GIF ', ' png ')) { $mark _data = imagecreatefromstring (file_get_contents ($watermark)); $mark _width = imagesx ($mark _data); $mark _height = Imagesy ($mark _data); Switch ($position) { Case 1: $x = 5; $y = 5; Break Case 2: $x = ($file _width-$mark _width)/2; $y = $mark _height; Break Case 3: $x = ($file _width-$mark _width)-5; $y = $mark _height; Break Case 4: $x = 5; $y = ($file _height-$mark _height)/2; Break Case 5: $x = ($file _width-$mark _width)/2; $y = ($file _height-$mark _height)/2; Break Case 6: $x = ($file _width-$mark _width)-5; $y = ($file _height-$mark _height)/2; Break Case 7: $x = 5; $y = ($file _height-$mark _height)-5; Break Case 8: $x = ($file _width-$mark _width)/2; $y = ($file _height-$mark _height)-5; Break Case 9: $x = ($file _width-$mark _width)-5; $y = ($file _height-$mark _height)-5; Break Default: $x = rand (0, ($file _width-$mark _width)); $y = rand (0, $file _height-$mark _height)); } $temp = Imagecreatetruecolor ($mark _width, $mark _height); Imagecopy ($temp, $file _data, 0, 0, $x, $y, $mark _width, $mark _height); Imagecopy ($temp, $mark _data, 0, 0, 0, 0, $mark _width, $mark _height); Imagecopymerge ($file _data, $temp, $x, $y, 0, 0, $mark _width, $mark _height, $opacity); Imagejpeg ($file _data, $file, 100); Imagedestroy ($temp); Imagedestroy ($file _data); Imagedestroy ($mark _data); return true; }else{ return $this->error ( -6, $watermark); } www.111cn.net } Private Function error ($code = 0, $extends = ') { if ($code) { Switch ($code) { Case 6: $error = ' Write temporary folder failed '; Break Case 5: $error = ' Write system Temp folder error '; Break Case 4: $error = ' No files are uploaded please check the form '; Break Case 3: $error = ' Error uploading on file is incomplete '; Break Case 2: $error = ' file size exceeds the form limit '; Break Case 1: $error = ' file size exceeds system limit '; Break Case-1: $error = ' upload file type is illegal '; Break Case-2: $error = ' upload file size exceeds background limit '; Break Case-3: $error = ' failed to create file save path '; Break Case-4: $error = ' Save file failed please check path '; Break Case-5: $error = ' Error reading file '; Break Case-6: $error = ' does not support this operation '; Break Default: $error = ' unknown error '; } Return ' ['. $code. ']: '. $error $extends; }else{ return false; } } } |