Php image upload class

Source: Internet
Author: User
Tags rand transparent image unsupported

Class upload_class {
Private $ ptname; // The name of the upload form;
Private $ udname; // whether to create a subdirectory by month (0 indicates no, others are true );
Private $ ufname; // whether to create a file name by time (0 indicates no, others are true );
Private $ ultype; // The type of the uploaded file;
Private $ ulsize; // The size of the uploaded file;
Private $ ulname; // name of the output file;
Private $ ulpath; // output file path;
Private $ wm; // add a watermark (0 is not added, and others are added );
Private $ wmtype; // watermark type (0 indicates text, and others indicates Image );
Private $ wmpic; // watermark image;
Private $ wmpicquality; // image quality;
Private $ wmpictrans; // Transparent image;
Private $ wmstr; // watermark character;
Private $ wmstrsize; // character size;
Private $ wmstrfont; // character font;
Private $ wmstrcolor; // character color;
Private $ wmpos; // watermark position (1: Top left, 2: top center, 3: Top right ......);
Function _ construct ($ ptname = 'upfile', $ udname = 1, $ ufname = 1, $ ultype = array ('image/jpg ', 'image/jpeg ', 'image/png ', 'image/pjpeg', 'image/GIF', 'image/bmp ', 'image/x-png'), $ wm = 1, $ wmtype = 1, $ wmpic = 'Images/wm.gif ', $ ulsize = 2097152, $ ulpath = 'Images/temp/', $ wmpictrans = 20, $ wmpicquality = 80, $ wmstr = 'donline', $ wmstrsize = 5, $ wmstrfont = '. /font/cour. ttf', $ wmstrcolor = '# ff0000', $ wmpos = 9 ){
$ This-> ptname = $ _ FILES [$ ptname];
$ This-> udname = $ udname;
$ This-> ufname = $ ufname;
$ This-> ultype = $ ultype;
$ This-> ulsize = $ ulsize;
$ This-> ulpath = $ ulpath;
$ This-> wm = $ wm;
$ This-> wmtype = $ wmtype;
$ This-> wmpic = $ wmpic;
$ This-> wmpicquality = $ wmpicquality;
$ This-> wmpictrans = $ wmpictrans;
$ This-> wmstr = $ wmstr;
$ This-> wmstrsize = $ wmstrsize;
$ This-> wmstrfont = $ wmstrfont;
$ This-> wmstrcolor = $ wmstrcolor;
$ This-> wmpos = $ wmpos;
}
Function uploadfun (){
If ($ _ SERVER ['request _ method'] = 'post '){
If (! Is_uploaded_file ($ this-> ptname ['tmp _ name']) $ this-> errorfun ('upload failed! ');
If (! In_array ($ this-> ptname ['type'], $ this-> ultype) $ this-> errorfun ('unsupported file type! ');
If ($ this-> ulsize <$ this-> ptname ['size']) $ this-> errorfun ('file is too large! ');
If ($ this-> udname) {date_default_timezone_set ('utc'); $ this-> ulpath = $ this-> ulpath. 'month _'. date ('ym '). '/';}
Else {$ this-> ulpath = $ this-> ulpath ;}
$ This-> createfun ($ this-> ulpath );
If ($ this-> ufname) {$ t = pathinfo ($ this-> ptname ['name']); $ this-> ulname = $ this-> ulpath. time (). '. '. $ t ['extension'];}
Else {$ this-> ulname = $ this-> ulpath. $ this-> ptname ['name'];}
If (file_exists ($ this-> ulname) $ this-> errorfun ('This file already exists! ');
If (! Move_uploaded_file ($ this-> ptname ['tmp _ name'], $ this-> ulname) $ this-> errorfun ('file moving error! ');
$ This-> wmfun ();
$ This-> errorfun ('upload successful! ');
  }
}
Function createfun ($ d ){
If (! File_exists ($ d) {$ this-> createfun (dirname ($ d); mkdir ($ d );}
}
Function wmfun (){
If ($ this-> wm ){
If (file_exists ($ this-> ulname )){
$ Groundimg = getimagesize ($ this-> ulname );
$ Ow = $ groundimg [0];
$ Oh = $ groundimg [1];
Switch ($ groundimg [2]) {
Case 1: $ g = imagecreatefromgif ($ this-> ulname); break;
Case 2: $ g = imagecreatefromjpeg ($ this-> ulname); break;
Case 3: $ g = imagecreatefrompng ($ this-> ulname); break;
Case 4: $ g = imagecreatefromwbmp ($ this-> ulname); break;
Default: $ this-> errorfun ('unsupported background image type! ');
    }
   }
Else {$ this-> errorfun ('The background image does not exist! ');}
If (file_exists ($ this-> wmpic )){
$ Wmimg = getimagesize ($ this-> wmpic );
$ Ww = $ wmimg [0];
$ Wh = $ wmimg [1];
Switch ($ wmimg [2]) {
Case 1: $ w = imagecreatefromgif ($ this-> wmpic); break;
Case 2: $ w = imagecreatefromjpeg ($ this-> wmpic); break;
Case 3: $ w = imagecreatefrompng ($ this-> wmpic); break;
Case 4: $ w = imagecreatefromwbmp ($ this-> wmpic); break;
Default: $ this-> errorfun ('unsupported watermark image type! ');
    }
   }
Else {$ this-> errorfun ('The watermark image does not exist! ');}
Switch ($ this-> wmtype ){
Case 0: $ tp = imagettfbbox (ceil ($ this-> wmstrsize * 2.5), 0, $ this-> wmstrfont, $ this-> wmstr ); $ ww = $ tp [2]-$ tp [6]; $ wh = $ tp [3]-$ tp [7]; unset ($ tp); break;
Case 1: $ ww = $ ww; $ wh = $ wh; break;
Default: $ ww = $ ww; $ wh = $ wh; break;
   }
If ($ ow <$ ww | $ oh <$ wh) $ this-> errorfun ('The background image is too small! The watermark cannot be generated! ');
Switch ($ this-> wmpos ){
Case 0: $ x = rand (0, ($ ow-$ ww); $ y = rand (0, ($ oh-$ wh); break; // random
Case 1: $ x = 0; $ y = 0; break; // 1 is the top left
Case 2: $ x = ($ ow-$ ww)/2; $ y = 0; break; // 2 center the top
Case 3: $ x = $ ow-$ ww; $ y = 0; break; // 3: top right
Case 4: $ x = 0; $ y = ($ oh-$ wh)/2; break; // 4 is left in the middle
Case 5: $ x = ($ ow-$ ww)/2; $ y = ($ oh-$ wh)/2; break; // 5 is center
Case 6: $ x = $ ow-$ ww; $ y = ($ oh-$ wh)/2; break; // 6 is left in the middle
Case 7: $ x = 0; $ y = $ oh-$ wh; break; // 7 is left at the bottom
Case 8: $ x = ($ ow-$ ww)/2; $ y = $ oh-$ wh; break; // 8 is centered at the bottom
Case 9: $ x = $ ow-$ ww; $ y = $ oh-$ wh; break; // 9 is the bottom right
Default: $ x = rand (0, ($ ow-$ ww); $ y = rand (0, ($ oh-$ wh); break; // random
      }
Imagealphablending ($ g, true );
Switch ($ this-> wmtype ){
Case 0:
If ($ this-> wmstrcolor) {$ R = hexdec (substr ($ this-> wmstrcolor, 1, 2); $ G = hexdec (substr ($ this-> wmstrcolor, 3, 2); $ B = hexdec (substr ($ this-> wmstrcolor, 5 ));}
Else {$ this-> errorfun ('The watermark text color does not exist! ');}
Imagestring ($ g, $ this-> wmstrfont, $ x, $ y, $ this-> wmstr, imagecolorallocate ($ g, $ R, $ G, $ B )); break;
Case 1; imagecopymerge ($ g, $ w, $ x, $ y, 0, $ ww, $ wh, $ this-> wmpictrans); break;
Default: imagecopymerge ($ g, $ w, $ x, $ y, 0, $ ww, $ wh, $ this-> wmpictrans); break;
   }
@ Unlink ($ this-> ulname );
Switch ($ groundimg [2]) {
Case 1: imagegif ($ g, $ this-> ulname); break;
Case 2: imagejpeg ($ g, $ this-> ulname, $ this-> wmpicquality); break;
Case 3: imagepng ($ g, $ this-> ulname); break;
Case 4: imagewbmp ($ g, $ this-> ulname); break;
Default: $ this-> errorfun ('image generation failed! ');
   }
If (isset ($ wmimg) unset ($ wmimg );
If (isset ($ w) imagedestroy ($ w );
Unset ($ groundimg );
Imagedestroy ($ g );
  }
}
Function errorfun ($ e = 'unknown error! '){
$ Msg = '<script language = "javascript" type = "text/javascript"> ';
$ Msg. = 'alert ("'. $ e .'");';
$ Msg. = 'history. back ();';
$ Msg. = '</script> ';
Echo $ msg;
Exit;
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.