Home >Backend Development >PHP Tutorial >Add watermark to php image upload (automatically add watermark)_PHP tutorial

Add watermark to php image upload (automatically add watermark)_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:08:441194browse

PHP image upload and watermark (automatically add watermark)/*This is a perfect PHP file upload code. After the image is uploaded successfully, it will automatically add a watermark to the image. This is very good and fast. You have to add it one by one manually. watermark effect.

php tutorial picture upload and watermark (automatically add watermark)
/*
This is a perfect php file upload code. After the picture is uploaded successfully, it will automatically add a watermark to the picture, which is very fast and fast. To improve, you have to manually add the watermark effect one by one.
*/

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 to attach a watermark (1 is watermark, others are not watermarked);
$watertype=$watertype; //Watermark type (1 is text, 2 is the picture)
$watercontent=$content; //The content of the watermark
if(empty($uploadfile['name'][$i])) {
die("No file uploaded" );
}
if($uploadfile['error'][$i] == 2) { //Verify the 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");//Unified resources for files allowed to be uploaded Type mimetype
$allow = false; $t) {
if(strpos($mimetype, $t) !== false) {
$allow = true; //File type that meets the upload conditions was found
break;
}
}
if($allow == false) {
die("The uploaded file type is not allowed");
}
$result = is_uploaded_file($uploadfile['tmp_name' ][$i]); //Determine whether it is generated by the upload action
if(!$result) {
die("The uploaded file is incorrect");
}
$uploaddir = "img/"; //Upload file saving directory
if(!file_exists($uploaddir)) mkdir($uploaddir, 0777, true);//If the upload saving directory does not exist, create it recursively
/* *Rename 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 was uploaded";break;
case 4;return "No file was uploaded";break;
default:die("File upload failed");
}
}
if($watermark==1) {
$iinfo=getimagesize($uploaddir.$uploadfilename); //Get the relevant information of the image 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); //The background is filled with 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: // Watermark string
imagestring($nimage,5,$iinfo['0']/2-50,$iinfo['1']-30,$watercontent,$black ; $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); //Change the image $nimage creates a jpeg format file with the $destination file name
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" ) { //No parameter verification is done
//The four parameters of this function are: the name value of the upload control; whether to add a watermark (1 is plus, other numbers are bad);
//Watermark Type (1 is a string, 2 is a picture); the content of the watermark, write the data when it is a string, and write the address of the picture when it is a picture;
$picture = upload($_files['picture'],1,2, "img/watermark.gif");//Upload the file and return the uploaded file path name

}

?>

Upload code



Product picture:

      
                             ;td colspan="2" align="center">









http://www.bkjia.com/PHPjc/444863.html

www.bkjia.com
true

http: //www.bkjia.com/PHPjc/444863.html

php image upload with watermark (automatically add watermark)/*This is a perfect php file upload code, picture After the upload is successful, a watermark will be automatically added to the image, which will improve the quality quickly...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn