Php image upload and watermarking (adding watermarks automatically) _ PHP Tutorial

Source: Internet
Author: User
Tags imagecopy mime file php file upload
Add watermarks to php image uploads (add watermarks automatically ). Php image upload and watermarking (automatic watermark adding) * This is a perfect php file upload code. after the image is uploaded successfully, it is automatically added to the image, this greatly improves the php image upload and watermarking (automatically adding watermarks)/* This is a perfect php file upload code. after the image is uploaded successfully, it is automatically added to the image, this greatly improves the watermark effect.

Php Tutorial: adding watermarks to image uploads (adding watermarks automatically)
/*
This is a perfect php file upload code. after the image is uploaded successfully, the image is automatically added with a watermark. This greatly improves the watermark effect by hand.
*/

Function upload ($ uploadfile, $ watermark = 1, $ watertype = 1, $ content ){
Foreach ($ uploadfile ['name'] as $ key => $ name) {// multifile Upload function
Uploadall ($ uploadfile, $ key, $ watermark, $ watertype, $ content );
}
}
Function uploadall ($ uploadfile, $ I, $ watermark, $ watertype, $ content ){
$ Watermark = $ watermark; // whether to add a watermark. (1 indicates adding a watermark, and others indicates not adding a watermark );
$ Watertype = $ watertype; // watermark type (1 is text, 2 is image)
$ Watercontent = $ content; // watermark content
If (empty ($ uploadfile ['name'] [$ I]) {
Die ("uploading files not selected ");
}
If ($ uploadfile ['error'] [$ I] = 2) {// verify the html judgment result
Die ("the uploaded file is too large ");
}
$ Allow_filemaxsize = 2048000; // 2 m
$ Filesize = $ uploadfile ['size'] [$ I];
If ($ filesize> $ allow_filemaxsize ){
Die ("the uploaded file is too large ");
}
$ Allow_filetypes = array ("jpeg", "gif", "png", "jpg", "pjpeg"); // The Uniform resource type of files that can be uploaded. mimetype
$ Allow = false; // not allowed by default
$ Mimetype = $ uploadfile ['type'] [$ I]; // mime file type of the uploaded file
Foreach ($ allow_filetypes as $ t ){
If (strpos ($ mimetype, $ t )! = False ){
$ Allow = true; // the file type that meets the upload conditions is found.
Break;
}
}
If ($ allow = false ){
Die ("the uploaded file type is not allowed ");
}
$ Result = is_uploaded_file ($ uploadfile ['tmp _ name'] [$ I]); // You can check whether the file is generated by the upload action.
If (! $ Result ){
Die ("the uploaded file is incorrect ");
}
$ Uploaddir = "img/"; // upload the file storage directory
If (! File_exists ($ uploaddir) mkdir ($ uploaddir, 0777, true); // recursively create
/** Rename the file **/
$ Filetype = explode (".", $ uploadfile ['name'] [$ I]);
$ Filetype = array_pop ($ filetype );
$ Uploadfilename = time (). ".". $ filetype;
$ _ Session ['filename'] = $ uploadfilename;
// End
$ Result = move_uploaded_file ($ uploadfile ['tmp _ name'] [$ I], $ uploaddir. $ uploadfilename );
If ($ result ){
Echo "file uploaded successfully ";
} Else {
Switch ($ uploadfile ['error'] [$ I]) {
Case 1: return "the uploaded file exceeds the maximum value set in php. ini"; break;
Case 2: return "the uploaded file exceeds the maximum value set in html"; break;
Case 3: return "only part of the file is uploaded"; break;
Case 4; return "no file is uploaded"; break;
Default: die ("file Upload failed ");
}
}
If ($ watermark = 1 ){
$ Iinfo = getimagesize ($ uploaddir. $ uploadfilename); // Obtain the image information and obtain the array.
$ Nimage = imagecreatetruecolor ($ iinfo [0], $ iinfo [1]);
$ White = imagecolorallocate ($ nimage, 255,255,255); // set the background color to white.
$ Black = imagecolorallocate ($ nimage, 0); // set the background color to black.
$ Red = imagecolorallocate ($ nimage, 255, 0, 0); // set the background color to red.
Imagefill ($ nimage, $ white); // The background is white.
Switch ($ iinfo [2]) {
Case 1:
$ Simage = imagecreatefromgif ($ uploaddir. $ uploadfilename );
Break;
Case 2:
$ Simage = imagecreatefromjpeg ($ uploaddir. $ uploadfilename );
Break;
Case 3:
$ Simage = imagecreatefrompng ($ uploaddir. $ uploadfilename );
Break;
Case 6:
$ Simage = imagecreatefromwbmp ($ uploaddir. $ uploadfilename );
Break;
Default:
Die ("unsupported file types ");
Exit;
}
Imagecopy ($ nimage, $ simage, 0, 0, 0, $ iinfo ['0'], $ iinfo ['1']);
Switch ($ watertype ){
Case 1: // add a watermark string
Imagestring ($ nimage, 5, $ iinfo ['0']/2-50, $ iinfo ['1']-30, $ watercontent, $ black );
Break;
Case 2: // add a watermark image
$ Simage1 = imagecreatefromgif ($ watercontent );
$ Size = getimagesize ($ watercontent );
Imagecopy ($ nimage, $ simage1, $ iinfo ['0']/2 + 50, $ iinfo ['1']-100,0, 0, $ size [0], $ size [1]);
Imagedestroy ($ simage1 );
Break;
}

Switch ($ iinfo [2]) {
Case 1:
Imagejpeg ($ nimage, $ uploaddir. $ uploadfilename); // Create a jpeg file with $ destination file name for the image $ nimage
Break;
Case 2:
Imagejpeg ($ nimage, $ uploaddir. $ uploadfilename );
Break;
Case 3:
Imagepng ($ nimage, $ uploaddir. $ uploadfilename );
Break;
Case 6:
Imagewbmp ($ nimage, $ uploaddir. $ uploadfilename );
Break;
}
Imagedestroy ($ nimage); // overwrite the original uploaded File
Imagedestroy ($ simage );
}
}
If (@ $ _ get ['act '] = "insert") {// parameter verification is not performed
// The four parameters of this function are: the name value of the upload control; whether to add a watermark (1 is added, and other numbers are not good );
// Watermark type (1 is a string, 2 is an image); watermark content, data written when a string, and image address;
$ Picture = upload ($ _ files ['picture '], "img/watermark.gif"); // upload the file and return the path name of the uploaded file

}

?>

Upload code


Http://www.bkjia.com/PHPjc/444863.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444863.htmlTechArticlephp image Upload watermark (automatically add watermark)/* This is a perfect php file upload code, the image uploaded successfully and automatically added to the image watermark, this is a good and fast improvement...

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.