An extremely useful php Image Upload class (random name, thumbnail, and watermark)

Source: Internet
Author: User
Tags imagejpeg

Upimages. class. php Upload class
Copy codeThe 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, $ text [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-> composer folder. "/". $ this-> markFolder. "/". $ newName );
Return $ this-> exist folder. "/". $ this-> markFolder. "/". $ newName;
} Else {
ImageJPEG ($ newImg );
}
ImageDestroy ($ newImg );
ImageDestroy ($ img );
Return $ newName;
}
}
?>

Usage
Copy 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.

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.