| We will introduce the file upload and image processing classes developed by php. if you need them, refer to them.
Php implements simple file upload. if there is another efficient php file Upload class, it will be a huge addition. So, if there is another easy-to-use image processing class that can reduce the image size and add watermarks, isn't it even better? It's a must, haha.
Let's take a look at today's php Tutorial.
1. file upload code
File_name = $ file_name; // The Rename method indicates that the time is used. for others, the 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 the file to be uploaded ($ _ FILES [] from the form) * @ return boolean returns a boolean value */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 = 'exceeds php. file size in ini '; break; case 2: $ t His-> errmsg = 'exceeds the file size specified by the MAX_FILE_SIZE option '; break; case 3: $ this-> errmsg = 'only part of the file is upload'; break; case 4: $ this-> errmsg = 'file not upload'; break; case 5: $ this-> errmsg = 'File size 0 '; break; default: $ this-> errmsg = 'an error occurred while uploading the file! '; Break;} if ($ error = 0 & is_uploaded_file ($ tmp_name) {// check the file type if ($ this-> check_file_type ($ name) = FALSE) {return FALSE;} // check the file size. if ($ size> $ this-> max_size) {$ this-> errmsg = 'upload file '. $ name. 'too large, Max supported '. ceil ($ this-& gt; max_size/1024 ). 'kb file '; return FALSE;} $ this-> set_save_path (); // Set the 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; // the uploaded file name // move the 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: the file name to be checked * @ return. if the result is TRUE, FALSE and error messages are returned */ public function check_file_type ($ filename) {$ ext = $ this-> get_file_type ($ filename); $ this-> ext = $ ext; $ allow_types = explode ('|', $ this-> allow_types ); // split the file extensions that can be uploaded into arrays // echo $ ext; // check whether the file extension is in the file extension that can be uploaded if (in_array ($ ext, $ allow_types) {return TRUE;} else {$ this-> errmsg = 'upload file '. $ fil The ename. 'type is incorrect. only file types such as'. str_replace ('|', $ this-> allow_types). 'can be uploaded! '; Return FALSE ;}} /*** get file type ** @ access public * @ param string $ filename the target file name of the file type to be obtained * @ return string file type */public function get_file_type ($ filename) {$ info = pathinfo ($ filename); $ ext = $ info ['extension']; return $ ext ;} /*** set the file storage path after Upload */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 the directory does not exist, create the directory $ this-> set_dir ();}} /*** create directory ** @ access public * @ param string $ dir path of the directory to be created * @ return boolean returns error messages and FALSE */public function set_dir ($ dir = null) {// check whether the path exists if (! $ Dir) {$ dir = $ this-> save_path;} if (is_dir ($ dir) {$ this-> errmsg = 'the folder to be created 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 = 'Error when creating the directory'. $ d! ';}}Return true ;}}?>
2. image processing code
Mark_str) * 7 + 5. ** line adjustment * requires support from the GD Library. we recommend using the GD Library 2.0 + ** @ author for better use of this class. http://bbs.it-home.org **************************************** * *******/Class uploadImg extends uploadFile {public $ mark_str = 'bbs .it-home.org '; // watermark string public $ str_r = 0; // string color Rpublic $ str_g = 0; // string color Gpublic $ str_ B = 0; // string color Bpublic $ mark_ttf = '. /upload/SIMSUN. TTC '; // watermark text font file (including path) public $ mark_logo = '. /upload/logo.png '; // watermark image public $ resize_h; // Generate the Thumbnail height public $ resize_w; // Generate the thumbnail width public $ source_img; // the source image file public $ Dst_path = '. /upload/'; // Directory for storing Thumbnail files, if this parameter is left blank, the source image storage directory is used./The Image generated after the thumbnail is generated * @ access public * @ param integer $ w. The image width (px) is reduced) * @ param integer $ h the image height (px) * @ param string $ source_img source image (path + file name) */public function img_resized ($ w, $ h, $ source_img = NULL) {$ source_img = NULL? $ This-> uploaded: $ source_img; // get the source file address. if it is null, the default value is the image uploaded last time if (! Is_file ($ source_img) {// check whether the source image exists $ this-> errmsg = 'file '. $ source_img. 'Nonexistent '; return FALSE;} $ this-> source_img = $ source_img; $ img_info = getimagesize ($ source_img); $ source = $ this-> img_create ($ source_img ); // Create source image $ this-> resize_w = $ w; $ this-> resize_h = $ h; $ thumb = imagecreatetruecolor ($ w, $ h); imagecopyresized ($ thumb, $ source, 0, 0, 0, $ w, $ h, $ img_info [0], $ img_info [1]); // Generate a thumbnail image $ dst_path = $ this-> dst_path =''? $ This-> save_path: $ this-> dst_path; // get the target folder path $ dst_path = (preg_match ('// $/', $ dst_path ))? $ Dst_path: $ dst_path. '/'; // add/if (! Is_dir ($ dst_path) $ this-> set_dir ($ dst_path); // if the target folder does not exist, create $ dst_name = $ this-> set_newname ($ source_img ); $ this-> img_output ($ thumb, $ dst_name); // output image imagedestroy ($ source); imagedestroy ($ thumb );} /*** watermark ** @ access public * @ param string $ source_img source image path + file name * @ param integer $ mark_type watermark type (1 is an English string, 2 is a Chinese string, 3 is the image logo, the default is an 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 image */p Ublic function img_mark ($ source_img = NULL, $ mark_type = 1, $ mark_postion = 2) {$ source_img = NULL? $ This-> uploaded: $ source_img; // get the source file address. if it is null, the default value is the image uploaded last time if (! Is_file ($ source_img) {// check whether the source image exists $ this-> errmsg = 'file '. $ source_img. 'Nonexistent '; return FALSE;} $ this-> source_img = $ source_img; $ img_info = getimagesize ($ source_img); $ source = $ this-> img_create ($ source_img ); // create a source image $ mark_xy = $ this-> get_mark_xy ($ mark_postion); // Obtain the Watermark Position $ mark_color = imagecolorallocate ($ source, $ this-> str_r, $ this-> str_g, $ this-> str_ B); switch ($ mark_type) {case 1: // add an English string watermark $ str = $ this-> mark_st R; imagestring ($ source, 5, $ mark_xy [0], $ mark_xy [1], $ str, $ mark_color); $ this-> img_output ($ source, $ source_img); break; case 2: // add a Chinese string watermark if (! Is_file ($ this-> mark_ttf) {// check whether the font file exists $ this-> errmsg = 'watermark failed: font file '. $ this-> mark_ttf. 'does not exist! '; Return FALSE ;}$ str = $ this-> mark_str; // $ str = iconv ('gbk', 'utf-8', $ str ); // If GBK encoding is used, remove this line of annotation 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 an image watermark if (is_file ($ this-> mark_logo) {// if an image with a watermark logo exists, obtain the basic information of the logo image, exit $ logo_info = getimagesize ($ this-> mark_logo);} else {$ this-> errmsg = 'watermark failed: logo file '. $ this-> ma Rk_logo. 'does not exist! '; 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, exit $ this-> errmsg = 'watermark failed: source image '. $ this-> source_img. 'bi '. $ 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: // others are text images $ 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);}/*** obtain the watermark position ** @ access private *@ Param integer $ mark_postion watermark position (1 is in the lower left corner, 2 is in the lower right corner, 3 is in the upper left corner, 4 is in the upper right corner, and other is in the lower right corner) * @ return array $ mark_xy coordinate of the Watermark Position (index 0 is the coordinate X of the English string watermark, index 1 is the coordinate Y of the English string watermark, and * index 2 is the coordinate X of the Chinese string watermark, index 3 is the coordinate Y of the Chinese string watermark, index 4 is the coordinate X of the watermark image, and index 5 is the coordinate Y of the watermark image) */private function get_mark_xy ($ mark_postion) {$ img_info = getimagesize ($ this-> source_img); $ stre_w = strlen ($ this-> mark_str) * 9 + 5; // length of the watermark English string (px) (the English character size of the 5th character is about 9px plus 5px for aesthetic purposes) // (the Chinese character size of the 12th character is 12px, in utf8, the length of a Chinese character is 3 bytes and the size of a byte is 4 p. X and the length of an English character is about 9px. // to display the length of a byte in both Chinese and English, set it to the number of bytes * 7px) $ strc_w = strlen ($ this-> mark_str) * 7 + 5; // The length of the watermark Chinese string (px) if (is_file ($ this-> mark_logo )) {// if a watermark logo image exists, obtain the basic information of the logo image $ logo_info = getimagesize ($ this-> mark_logo );} // because the start position of the string is different between the imagestring function and the imagettftext function, the Y position of the English and Chinese strings is different. // The imagestring function starts from the upper left corner of the text. in the lower left corner of the text, refer to switch ($ mark_postion) {case 1: // the lower left corner of the position $ mark_xy [0] = 5; // The coordinate of the Watermark's English string X $ m Ark_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 image coordinate X $ mark_xy [5] = $ img_info [1]-$ logo_info [1]-5; // watermark image coordinate Ybreak; case 2: // $ mark_xy [0] = $ img_info [0]-$ stre_w in the lower right corner of the position; // The coordinate of the Watermark's English string X $ mark_xy [1] = $ img_info [1]-20; // 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 image coordinate X $ mark_xy [5] = $ img_info [1]-$ logo_info [1]-5; // watermark image coordinate Ybreak; case 3: // position $ 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 image coordinate X $ mark_xy [5] = 5; // watermark image coordinate Ybreak; case 4: // in the upper-right corner of the position, $ mark_xy [0] = $ img_info [0]-$ stre_w; // The coordinate of the watermark string X $ mark_xy [1] = 5; // watermark English string coordinate Y $ mark_xy [2] = $ img_info [0] -$ Strc_w; // coordinate of the watermark Chinese string X $ mark_xy [3] = 15; // watermark Chinese string coordinate Y $ mark_xy [4] = $ img_info [0]-$ logo_info [0]-5; // watermark image coordinate X $ mark_xy [5] = 5; // watermark image coordinate Ybreak; default: // others are in the lower right corner by default. $ mark_xy [0] = $ img_info [0]-$ stre_w; // coordinate of the watermark English string X $ mark_xy [1] = $ img_info [1]-5; // 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 image coordinate X $ mark_xy [5] = $ Img_info [1]-$ logo_info [1]-5; // watermark image coordinate Ybreak;} return $ mark_xy ;} /*** create a source image * @ access private * @ param string $ source_img source image (path + file name) * @ return img image created from the target file */private function img_create ($ source_img) {$ info = getimagesize ($ source_img); switch ($ info [2]) {case 1: if (! Function_exists ('authorization') {$ source = @ imagecreatefromjpeg ($ source_img);} else {$ source = @ imagecreatefromgif ($ source_img);} break; case 2: $ source = @ imagecreatefromjpeg ($ source_img); break; case 3: $ source = @ parse ($ source_img); break; case 6: $ source = @ imagecreatefromwbmp ($ source_img ); break; default: $ source = FALSE; break;} return $ source;}/*** rename the image * @ access private * @ param string $ s Ource_img source image path + file name * @ return string $ dst_name the renamed Image name (path + file name) */private function set_newname ($ sourse_img) {$ info = pathinfo ($ sourse_img); $ new_name = $ this-> resize_w. '_'. $ this-> resize_h. '_'. $ info ['basename']; // change the file name to wide _ height _ file name if ($ this-> dst_path = '') {// if the path for storing the thumbnail is empty, the default value is the source file in the same folder $ dst_name = str_replace ($ info ['basename '], $ new_name, $ sourse_img );} else {$ dst_name = $ this-> dst_path. $ new_name;} return $ dst_nam E;}/*** output image * @ access private * @ param $ im processed image * @ param $ dst_name name of the output image (path + file name) * @ return output image */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. you can take a look at the php file Upload principles to better understand the above code. |