| For everyone to introduce the PHP development of file upload class and image processing class, there is a need for friends, you can refer to the study.
PHP implementation of the file upload is relatively simple, if there is an efficient PHP file upload class, it is really powerful, do not want to fly. Well, then, if there is another good image processing class, you can reduce the picture, you can add watermarks, is not more cool? Whisper to you, that's a must, haha.
Let's take a look at today's PHP tutorial.
1, the File upload class code
file_name = $file _name;//Rename is represented by time, others use the given name $this->save_path = (Preg_match ('/\/$/', $save _path))? $save _path: $save _path. '/'; $this->allow_types = $allow _types = = '? ' Jpg|gif|png|zip|rar ': $allow _types;} /*** Upload file * @access public* @param $files Wait for the file to be uploaded ($_files[from the form) * @return Boolean returns a Boolean value */public function Upload_file ($fil ES) {$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 = ' exceeds the file size in php.ini '; Break;case 2 : $this->errmsg = ' exceeds the file size specified by the max_file_size option '; Break;case 3: $this->errmsg = ' file is only partially uploaded '; break;case 4: $this->e rrmsg = ' No files are uploaded '; Break;case 5: $this->errmsg = ' upload file size 0 '; Break;default: $this->errmsg = ' upload file failed! '; break;} if ($error = = 0 && is_uploaded_file ($tmp _name)) {//Detect file type if ($this->check_file_type ($name) = = FALSE) {return FALSE;} Detect file Size if ($size > $this->max_size) {$this->errmsg = ' upload file '. $name. ' Too big, maximum support '. Ceil ($this->max_size/1024). ' KB of files '; return FALSE;} $this->set_save_path ();//Set file storage path $new_name = $this->file_name! = ' Date '? $this->file_name. $this->ext:date (' Ymdhis '). $this->ext;//Set the new file name $this->uploaded = $this->save_path. $new _name;//file name after upload//move file if (Move_uploaded_file ($ Tmp_name, $this->uploaded) {$this->errmsg = ' file '. $this->uploaded. ' Upload successful! '; return TRUE;} else{$this->errmsg = ' file '. $this->uploaded. ' Upload failed! '; return FALSE;}}} /*** Check the upload file type * @access public* @param string $filename Wait for the file name to be checked * @return return FALSE if checked by returning true and error message */public function ch Eck_file_type ($filename) {$ext = $this->get_file_type ($filename); $this->ext = $ext; $allow _types = explode (' | ', $ This->allow_types);//split the file extension allowed to upload an array of//echo $ext;//Check to see if the upload file extension is in the allowed file extension if (In_array ($ext, $allow _types)) { return TRUE;} else{$this->errmsg = ' upload file ' $filename. ' type error, only supports upload '. Str_replace (' | ', ', ', $this->allow_types). ' and other file Types! '; return FALSE;}} /*** Get file type * @access public* @param string $filename The target file name to get the files of type * @return string file type */public function Get_file_type ($filename) {$info = Pathinf O ($filename); $ext = $info [' extension '];return $ext;} /*** set File Upload save path */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)) {//If directory does not exist, create directory $this->set_dir ();}} /*** Create directory * @access public* @param string $dir path to create directory * @return Boolean failure returns an error message and False*/public function set_dir ($dir = Nu LL) {//check if the path exists if (! $dir) {$dir = $this->save_path;} if (Is_dir ($dir)) {$this->errmsg = ' The folder you need to create already exists! ';} $dir = explode ('/', $dir), foreach ($dir as $v) {if ($v) {$d. = $v. '/', if (!is_dir ($d)) {$state = mkdir ($d, 0777), if (! $state) $this->errmsg = ' in Create directory '. $d. ' Error! ';}}} return true;}}? >
2, the Image processing class code
MARK_STR) *7+5 * Line adjustment * requires GD library support, in order to better use this class recommended to use the GD library 2.0+** @author http://bbs.it-home.org********************************** /class Uploadimg extends UploadFile {public $mark _str = ' bbs.it-home.org ';//watermark string public $str _r = 0;//String Yan Color rpublic $str _g = 0; String color gpublic $str _b = 0; String color bpublic $mark _ttf = './upload/simsun. TTC '; Watermark Text font file (contains path) public $mark _logo = './upload/logo.png '; Watermark Picture public $resize _h;//generate thumbnail high public $resize _w;//generate thumbnail wide public $source _img;//source picture file public $dst _path = './upload/';// Thumbnail file directory, do not fill in the source image of the directory/*** generated thumbnail image * @access public* @param integer $w The width of the image (px) * @param integer $h The height of the image (PX) * @para M string $source _img source Picture (path + file name) */public function img_resized ($w, $h, $source _img = null) {$source _img = $source _img = NULL ? $this->uploaded: $source _img;//Get the address of the source file, if empty it defaults to the last uploaded picture if (!is_file ($source _img) {//check if the source picture exists $this->errmsg = ' file '. $source _img. ' does not exist '; return FALSE;} $this->source_img = $source _img; $img _info = getimagesize ($source _img); $source = $this->iMg_create ($source _img); Create source picture $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]);//generate thumbnail image $dst_path = $this->dst_path = = "? $this->save_path: $this->dst_path; Get destination Folder path $dst_path = (Preg_match ('/\/$/', $dst _path))? $DST _path: $dst _path. '/';//The target folder is added with/if (!is_dir ($dst _path)) $this->set_dir ($dst _path); If the destination folder does not exist, create $dst_name = $this->set_newname ($source _img); $this->img_output ($thumb, $dst _name);// Output picture Imagedestroy ($source); Imagedestroy ($THUMB);} /*** * @access public* @param string $source _img source picture path + filename * @param integer $mark _type watermark type (1 is English string, 2 is Chinese string, 3 is picture logo, The default is the English string) * @param integer $mark _postion watermark Position (1 is the lower left corner, 2 is the lower-right corner, 3 is the upper-left corner, 4 is the upper-right corner, the default is the lower-right corner); * @return watermark picture */public function Img_ Mark ($source _img = null, $mark _type = 1, $mark _postion = 2) {$source _img = $source _img = = null? $this->uploaded: $SOURC e_img;//gets the address of the source file and, if empty, defaults to the last uploaded picture if (!is_file ($source _img)) {//check whether the source picture is savedIn $this->errmsg = ' file '. $source _img ' does not exist '; return FALSE;} $this->source_img = $source _img; $img _info = getimagesize ($source _img); $source = $this->img_create ($source _img) ; Create source Picture $mark_xy = $this->get_mark_xy ($mark _postion);//get watermark position $mark_color = imagecolorallocate ($source, $this Str_r, $this->str_g, $this->str_b), switch ($mark _type) {Case 1://Add english string watermark $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:// Add Chinese string watermark if (!is_file ($this->mark_ttf)) {//check if the font file exists $this->errmsg = ' water stamp failed: Font file '. $this->mark_ttf. ' Not present! '; return FALSE;} $str = $this->mark_str;//$str = iconv (' GBK ', ' utf-8 ', $str);//Convert character encoding if you use GBK encoding, remove this line comment 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://Add Picture watermark if (Is_file ($this->mark_logo)) {//If there is a watermark logo image to obtain the basic information of the logo image, does not exist then exit $logo_info = GetImageSize ($ This->mark_logo);} Else{$this->errmsg = ' Water stamp failed: Logo file '. $this->mark_logo. ' Not there! '; return FALSE;} $logo _info = getimagesize ($this->mark_logo), if ($logo _info[0]> $img _info[0] | | $logo _info[1]> $img _info[1]) { If the source image is smaller than the logo size then exit $this->errmsg = ' water stamp failed: Source picture '. $this->source_img. $this->mark_logo. ' Small! '; 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://Other text picture $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);} /*** Get watermark Position * @access private* @param integer $mark _postion watermark position (1 is lower left, 2 is lower right corner, 3 is upper left corner, 4 is upper right corner, other is lower right corner) * @return Array $mark _xy The coordinates of the watermark position (index 0 is the English string watermark coordinates x, index 1 is the English string watermark coordinates y,* index 2 is the Chinese string watermark coordinates x, index 3 is the Chinese string watermark coordinates y, index 4 is the watermark picture coordinates x, index 5 is the watermark picture coordinates y) */private function Get_mark_xy ($mark _postion) {$img _info = getimagesize ($this->source_img); $stre _w = strlen($this->mark_str) *9+5; The length of the watermark English string (px) (5th characters of the English character size is about 9px for beauty and 5px)//(12th characters of the Chinese character size is 12px, in utf8 a character length of 3 bytes A byte 4px and an English character length of a byte size of about 9px// In order to display in the case of mixed Chinese and English completely set its length to the number of bytes *7px) $strc _w = strlen ($this->mark_str) *7+5; Watermark Chinese string length (px) if (Is_file ($this->mark_logo)) {//If there is a watermark logo image then get the basic information of the logo image $logo_info = getimagesize ($this- >mark_logo);} Because the imagestring function and the Imagettftext function differ in the starting position of the string, the Y position of the English and Chinese strings is different//imagestring function is referenced from the upper left corner of the text The Imagettftext function is from the lower left corner of the text as the reference switch ($mark _postion) {Case 1://Position lower left corner $mark_xy[0] = 5;//watermark English string coordinates x$mark_xy[1] = $img _info[1]- 20;//watermark English string coordinate y$mark_xy[2] = 5; Watermark Chinese string coordinate x$mark_xy[3] = $img _info[1]-5;//watermark Chinese string coordinate y$mark_xy[4] = 5;//watermark picture coordinates x$mark_xy[5] = $img _info[1]-$logo _info[ 1]-5;//watermark Picture Coordinates ybreak;case 2://Position lower right corner $mark_xy[0] = $img _info[0]-$stre _w; Watermark English string coordinate x$mark_xy[1] = $img _info[1]-20;//watermark English string coordinate y$mark_xy[2] = $img _info[0]-$strc _w; Watermark Chinese string coordinate x$mark_xy[3] = $img _info[1]-5;//watermark Chinese string coordinate y$mark_xy[4] = $img _info[0]-$logo _info[0]-5;//watermark Picture coordinates x$mark_xy[ 5] = $img _info[1]-$logo _info[1]-5;//watermark Picture Coordinates ybreak;case 3://Position upper left corner $mark_xy[0] = 5; Watermark English string coordinate x$mark_xy[1] = 5;//watermark English string coordinate y$mark_xy[2] = 5; Watermark Chinese string coordinate x$mark_xy[3] = 15;//watermark Chinese string coordinate y$mark_xy[4] = 5;//watermark picture coordinate x$mark_xy[5] = 5;//watermark Picture coordinates ybreak;case 4://Position upper right corner $mark_ Xy[0] = $img _info[0]-$stre _w; Watermark English string coordinate x$mark_xy[1] = 5;//watermark English string coordinate y$mark_xy[2] = $img _info[0]-$strc _w; Watermark Chinese string coordinate x$mark_xy[3] = 15;//watermark Chinese string coordinate y$mark_xy[4] = $img _info[0]-$logo _info[0]-5;//watermark picture coordinates x$mark_xy[5] = 5;// Watermark Picture coordinates Ybreak;default://Other default is lower right corner $mark_xy[0] = $img _info[0]-$stre _w; Watermark English string coordinate x$mark_xy[1] = $img _info[1]-5;//watermark English string coordinate y$mark_xy[2] = $img _info[0]-$strc _w; Watermark Chinese string coordinate x$mark_xy[3] = $img _info[1]-15;//watermark Chinese string coordinate y$mark_xy[4] = $img _info[0]-$logo _info[0]-5;//watermark Picture coordinates x$mark_xy [5] = $img _info[1]-$logo _info[1]-5;//watermark picture coordinates ybreak;} return $mark _xy;} /*** Create source Picture * @access private* @param string $source _img source Picture (path + file name) * @return img New image from Target file */private function img_create ($s OURCE_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;} /*** Rename picture * @access private* @param string $source _img Source picture path + file name * @return string $dst _name renamed picture name (path + file name) */private funct Ion Set_newname ($sourse _img) {$info = PathInfo ($sourse _img); $new _name = $this->resize_w. ' _ '. $this->resize_h. ' _ '. $info [' basename '];//to modify the file name to: Width _ high _ filename if ($this->dst_path = = ") {//If the thumbnail path is empty, the default is the source file same folder $dst_name = Str_replace ( $info [' basename '], $new _name, $sourse _img);} else{$dst _name = $this->dst_path. $new _name;} return $DST _name;} /*** Output Picture * @access private* @param $im processed picture * @param $dst _name output Image Name (path + file name) * @return Output picture */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;}}? >
That's all, about the php file upload principle, you can have time to see, to better understand the above code.
|