Php image upload class can also generate thumbnails and watermarks _ PHP Tutorial

Source: Internet
Author: User
The php image upload class can generate thumbnails and watermarks at the same time. The php image upload class can also generate thumbnails and watermarks. this image upload code can add watermarks to uploaded images, generate small images, and generate text watermarks. Php Tutorial graph php image upload class can also generate thumbnails and watermarks. this image upload code can add watermarks to uploaded images, generate small images, and generate text watermarks.

Php Tutorial image upload class can generate thumbnails and watermarks at the same time
This image upload code adds a watermark to the uploaded image, generates a small image, and generates a text watermark.

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 = $ folder;
$ This-> smallfolder = $ smallfolder;
$ This-> fonttype = $ includefolder. "/04b_08 _. 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 tutorial
$ Uploadfile = $ this-> Folder 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 folder. "/". $ this-> smallfolder. "/". $ newname) @ unlink ($ this-> Folder 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 folder. "/". $ this-> markfolder. "/". $ newname) @ unlink ($ this-> Folder 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;
}
}

Bytes. Php Tutorial diagram...

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.