PHP image upload Watermark (automatically add watermark) _php Tutorial

Source: Internet
Author: User
Tags imagecopy imagejpeg mime file php file upload
PHP image upload Watermark (automatically add watermark)//* This is a perfect php file upload code, the image upload success and automatically add water to the picture, so that a good quick to improve the effect of a single sheet to increase the watermark.

PHP Tutorial Image upload watermark (automatically add watermark)
/*
This is a perfect php file upload code, the image upload success and automatically add water to the picture, so it is very good to improve the speed of a manual sheet to increase the watermark effect.
*/

function upload ($uploadfile, $watermark =1, $watertype =1, $content) {
foreach ($uploadfile [' name '] as $key = + $name) {//multi-File Upload function
Uploadall ($uploadfile, $key, $watermark, $watertype, $content);
}
}
function Uploadall ($uploadfile, $i, $watermark, $watertype, $content) {
$watermark = $watermark; Whether additional watermark (1 is watermark, the other is not watermark);
$watertype = $watertype; Watermark Type (1 for text, 2 for picture)
$watercontent = $content; Content of the watermark
if (Empty ($uploadfile [' name '] [$i])) {
Die ("File Upload not selected");
}
if ($uploadfile [' Error '] [$i] = = 2) {//Validate HTML judgment result
Die ("The uploaded file is too large");
}
$allow _filemaxsize = 2048000; 2m
$filesize = $uploadfile [' Size '] [$i];
if ($filesize > $allow _filemaxsize) {
Die ("The uploaded file is too large");
}
$allow _filetypes = Array ("JPEG", "GIF", "png", "JPG", "pjpeg");//The file that is allowed to be uploaded Uniform resource type mimetype
$allow = false; Default is not allowed
$mimetype = $uploadfile [' type '] [$i]; MIME file types for uploading files
foreach ($allow _filetypes as $t) {
if (Strpos ($mimetype, $t)!== false) {
$allow = true; Found a file type that meets the upload criteria
Break
}
}
if ($allow = = False) {
Die ("The uploaded file type is not allowed");
}
$result = Is_uploaded_file ($uploadfile [' tmp_name '] [$i]); Determines whether the upload action is generated
if (! $result) {
Die ("uploaded file is wrong");
}
$uploaddir = "img/"; Upload File Save Directory
if (!file_exists ($uploaddir)) mkdir ($uploaddir, 0777, true);//If the upload save directory does not exist, the recursive creation
/** Renaming files **/
$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 upload succeeded";
}else{
Switch ($uploadfile [' Error '] [$i]} {
Case 1:return "The uploaded file exceeds the maximum value set in PHP.ini";
Case 2:return "The uploaded file exceeds the maximum value set in HTML";
Case 3:return "file is only partially uploaded";
Case 4;return "No files were uploaded";
Default:die ("File upload failed");
}
}
if ($watermark ==1) {
$iinfo =getimagesize ($uploaddir. $uploadfilename); Get the information about the picture and get the array
$nimage =imagecreatetruecolor ($iinfo [0], $iinfo [1]);
$white =imagecolorallocate ($nimage, 255,255,255); Set the background color to white
$black =imagecolorallocate ($nimage, 0,0,0); Set the background color to black
$red =imagecolorallocate ($nimage, 255,0,0); Set the background color to red
Imagefill ($nimage, 0,0, $white); Background fill 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 type");
Exit
}
Imagecopy ($nimage, $simage, 0,0,0,0, $iinfo [' 0 '], $iinfo [' 1 ']);
Switch ($watertype) {
Case 1://Add watermark String
Imagestring ($nimage, 5, $iinfo [' 0 ']/2-50, $iinfo [' 1 ']-30, $watercontent, $black);
Break
Case 2://Add watermark Picture
$simage 1 =imagecreatefromgif ($watercontent);
$size = getimagesize ($watercontent);
Imagecopy ($nimage, $simage 1, $iinfo [' 0 ']/2+50, $iinfo [' 1 ']-100,0,0, $size [0], $size [1]);
Imagedestroy ($simage 1);
Break
}

Switch ($iinfo [2]) {
Case 1:
Imagejpeg ($nimage, $uploaddir. $uploadfilename); Create a JPEG format file with the $destination file name in 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 upload file
Imagedestroy ($simage);
}
}
if (@$_get[' act '] = = "Insert") {//no parameter check
The four parameters of the function are: the name value of the upload control, whether watermark (1 is plus, the other number is poor);
The type of watermark (1 is a string, 2 is a picture), the content of the watermark, the string when the data is written, the image when the address of the picture;
$picture = Upload ($_files[' picture '],1,2, "img/watermark.gif");//upload the file and return the file path name after uploading

}

?>

Upload Code


http://www.bkjia.com/PHPjc/444863.html www.bkjia.com true http://www.bkjia.com/PHPjc/444863.html techarticle php image upload watermark (automatically add watermark)//* This is a perfect php file upload code, the image upload successfully and automatically add water to the picture, so it is very good to improve quickly ...

  • 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.