Upimages. Class. php PHP Upload class
CopyCode The Code is as follows: <? PHP
Class upimages {
VaR $ attachment folder = "Upload"; // The attachment storage point. The default value is annex.
VaR $ smallfolder = "small"; // path for storing thumbnails. Note: The path must be a subdirectory under $ folder. The default value is smallimg.
VaR $ markfolder = "mark"; // watermark image storage location
VaR $ upfiletype = "jpg gif png"; // the upload type. Default Value: jpg gif png rar zip
VaR $ upfilemax = 1024; // The maximum upload size. The unit is "kb". The default value is kb.
VaR $ fonttype; // font
VaR $ maxwidth = 500; // maximum Image Width
VaR $ maxheight = 600; // maximum Image Height
Function upimages ($ inclufolder, $ smallfolder, $ includefolder ){
$ This-> folder = $ folder;
$ This-> smallfolder = $ smallfolder;
$ This-> fonttype = $ includefolder. "/04b_082.16.ttf ";
}
Function upload ($ inputname ){
$ Imagename = Time (); // set the current time to the image name
If (@ empty ($ _ FILES [$ inputname] ["name"]) Die ("no image information is uploaded, please confirm ");
$ Name = explode (".", $ _ FILES [$ inputname] ["name"]); // separate the files before upload with "." To obtain the file type
$ Imgcount = count ($ name); // obtain the number of interceptions.
$ Imgtype = $ name [$ imgCount-1]; // type of the retrieved File
If (strpos ($ this-> upfiletype, $ imgtype) === false) Die (error ("only supported File Upload types ". $ this-> upfiletype. "Not Supported ". $ imgtype ));
$ Photo = $ imagename. ".". $ imgtype; // name of the file written to the database
$ Uploadfile = $ this-> folder. "/". $ photo; // name of the uploaded file
$ Upfileok = move_uploaded_file ($ _ FILES [$ inputname] ["tmp_name"], $ uploadfile );
If ($ upfileok ){
$ Imgsize = $ _ FILES [$ inputname] ["size"];
$ Ksize = round ($ imgsize/1024 );
If ($ ksize> ($ this-> upfilemax * 1024 )){
@ Unlink ($ uploadfile );
Die (error ("uploaded file exceeds". $ this-> upfilemax. "kb "));
}
} Else {
Die (error ("failed to upload the image, please make sure that your upload file does not exceed $ upfilemax kb or the upload time has timed out "));
}
Return $ photo;
}
Function getinfo ($ photo ){
$ Photo = $ this-> specify folder. "/". $ photo;
$ Imageinfo = getimagesize ($ photo );
$ Imginfo ["width"] = $ imageinfo [0];
$ Imginfo ["height"] = $ imageinfo [1];
$ Imginfo ["type"] = $ imageinfo [2];
$ Imginfo ["name"] = basename ($ photo );
Return $ imginfo;
}
Function smallimg ($ photo, $ width = 128, $ Height = 128 ){
$ Imginfo = $ this-> getinfo ($ photo );
$ Photo = $ this-> getting folder. "/". $ photo; // obtain the image source
$ Newname = substr ($ imginfo ["name"], 0, strrpos ($ imginfo ["name"], "."). "_thumb.jpg"; // new image name
If ($ imginfo ["type"] = 1 ){
$ IMG = imagecreatefromgif ($ photo );
} Elseif ($ imginfo ["type"] = 2 ){
$ IMG = imagecreatefromjpeg ($ photo );
} Elseif ($ imginfo ["type"] = 3 ){
$ IMG = imagecreatefrompng ($ photo );
} Else {
$ IMG = "";
}
If (empty ($ IMG) return false;
$ Width = ($ width> $ imginfo ["width"])? $ Imginfo ["width"]: $ width;
$ Height = ($ height> $ imginfo ["height"])? $ Imginfo ["height"]: $ height;
$ Srcw = $ imginfo ["width"];
$ Srch = $ imginfo ["height"];
If ($ srcw * $ width> $ srch * $ height ){
$ Height = round ($ srch * $ width/$ srcw );
} Else {
$ Width = round ($ srcw * $ height/$ srch );
}
If (function_exists ("imagecreatetruecolor ")){
$ Newimg = imagecreatetruecolor ($ width, $ height );
Imagecopyresampled ($ newimg, $ IMG, 0, 0, 0, $ width, $ height, $ imginfo ["width"], $ imginfo ["height"]);
} Else {
$ Newimg = imagecreate ($ width, $ height );
Imagecopyresized ($ newimg, $ IMG, 0, 0, 0, $ width, $ height, $ imginfo ["width"], $ imginfo ["height"]);
}
If ($ this-> tofile ){
If (file_exists ($ this-> folder. "/". $ this-> smallfolder. "/". $ newname) @ unlink ($ this-> folder. "/". $ this-> smallfolder. "/". $ newname );
Imagejpeg ($ newimg, $ this-> exist folder. "/". $ this-> smallfolder. "/". $ newname );
Return $ this-> revoke folder. "/". $ this-> smallfolder. "/". $ newname;
} Else {
Imagejpeg ($ newimg );
}
Imagedestroy ($ newimg );
Imagedestroy ($ IMG );
Return $ newname;
}
Function watermark ($ photo, $ text ){
$ Imginfo = $ this-> getinfo ($ photo );
$ Photo = $ this-> specify folder. "/". $ photo;
$ Newname = substr ($ imginfo ["name"], 0, strrpos ($ imginfo ["name"], "."). "_mark.jpg ";
Switch ($ imginfo ["type"]) {
Case 1:
$ IMG = imagecreatefromgif ($ photo );
Break;
Case 2:
$ IMG = imagecreatefromjpeg ($ photo );
Break;
Case 3:
$ IMG = imagecreatefrompng ($ photo );
Break;
Default:
Return false;
}
If (empty ($ IMG) return false;
$ Width = ($ this-> maxwidth> $ imginfo ["width"])? $ Imginfo ["width"]: $ this-> maxwidth;
$ Height = ($ this-> maxheight> $ imginfo ["height"])? $ Imginfo ["height"]: $ this-> maxheight;
$ Srcw = $ imginfo ["width"];
$ Srch = $ imginfo ["height"];
If ($ srcw * $ width> $ srch * $ height ){
$ Height = round ($ srch * $ width/$ srcw );
} Else {
$ Width = round ($ srcw * $ height/$ srch );
}
If (function_exists ("imagecreatetruecolor ")){
$ Newimg = imagecreatetruecolor ($ width, $ height );
Imagecopyresampled ($ newimg, $ IMG, 0, 0, 0, $ width, $ height, $ imginfo ["width"], $ imginfo ["height"]);
} Else {
$ Newimg = imagecreate ($ width, $ height );
Imagecopyresized ($ newimg, $ IMG, 0, 0, 0, $ width, $ height, $ imginfo ["width"], $ imginfo ["height"]);
}
$ white = imagecolorallocate ($ newimg, 255,255,255);
$ black = imagecolorallocate ($ newimg, 0, 0, 0 );
$ alpha = imagecolorallocatealpha ($ newimg, 230,230,230, 40);
imagefilledrectangle ($ newimg, 0, $ height-26, $ width, $ height, $ alpha);
imagefilledrectangle ($ newimg, 13, $ height-20, 15, $ height-7, $ black);
imagettftext ($ newimg, 4.9, 0, 20, $ height-14, $ black, $ this-> fonttype, $ Tex T [0]);
imagettftext ($ newimg, 4.9, 0, 20, $ height-6, $ black, $ this-> fonttype, $ text [1]);
if ($ this-> tofile) {
If (file_exists ($ this-> folder. "/". $ this-> markfolder. "/". $ newname) @ unlink ($ this-> folder. "/". $ this-> markfolder. "/". $ newname);
imagejpeg ($ newimg, $ this-> extends folder. "/". $ this-> markfolder. "/". $ newname);
return $ this-> revoke folder. "/". $ this-> markfolder. "/". $ newname;
}else {
imagejpeg ($ newimg);
}< br> imagedestroy ($ newimg);
imagedestroy ($ IMG );
return $ newname;
}< BR >?>
UsageCopy codeThe Code is as follows: Include 'upimages. Class. php ';
$ Max = "Upload"; // File Upload path
$ Mix = "small"; // thumbnail path (must be created under upload)
$ Mark = "mark"; // path for storing watermarked Images
$ Text = array ("oktang", "2012"); // watermark content
$ IMG = new upimages ($ Max, $ mix, $ max); // instantiate a Class Object
$ Photo = $ IMG-> upload ("file"); // uploaded file domain
$ IMG-> maxwidth = $ IMG-> maxheight = 600; // set height, width, and
$ IMG-> tofile = true;
$ Newsmallimg = $ IMG-> smallimg ($ photo );
$ Newmark = $ IMG-> watermark ($ photo, $ text );
Echo $ newsmallimg;
Echo $ Newmark;
Echo " <br> ";
Echo " <br> ";
Note that there is a font file, which can be downloaded from the Internet.