php to achieve multiple image upload watermarking skills

  • 2020-06-01 08:23:42
  • OfStack

 
<?php 
function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$textColor="#FF0000") 
{ 
$isWaterImage = FALSE; 
$formatMsg = " The file format is not supported, please use the image processing software to convert the image to GIF , JPG , PNG Format. "; 
// Read watermark file  
if(!empty($waterImage) && file_exists($waterImage)) 
{ 
$isWaterImage = TRUE; 
$water_info = getimagesize($waterImage); // Get is 1 An array  
$water_w = $water_info[0];// Gets the width of the watermark image  
$water_h = $water_info[1];// Get the watermark image high  
switch($water_info[2])// Get the watermark image format  
{ 
case 1:$water_im = imagecreatefromgif($waterImage);break; // Convert the image to PHP An identifiable coding process  
case 2:$water_im = imagecreatefromjpeg($waterImage);break; // Convert the image to PHP An identifiable coding process  
case 3:$water_im = imagecreatefrompng($waterImage);break; // Convert the image to PHP An identifiable coding process  
default:die($formatMsg); 
} 
} 
// Read background picture  
if(!empty($groundImage) && file_exists($groundImage)) 
{ 
$ground_info = getimagesize($groundImage); 
$ground_w = $ground_info[0];// Gets the width of the background image  
$ground_h = $ground_info[1];// Get the height of the background image  
switch($ground_info[2])// Gets the format of the background image  
{ 
case 1:$ground_im = imagecreatefromgif($groundImage);break; 
case 2:$ground_im = imagecreatefromjpeg($groundImage);break; 
case 3:$ground_im = imagecreatefrompng($groundImage);break; 
default:die($formatMsg); 
} 
} 
else 
{ 
die(" Need to add watermark image does not exist! "); 
} 
// The watermark position  
if($isWaterImage)// Image watermarking  
{ 
$w = $water_w; 
$h = $water_h; 
$label = " The image "; 
} 
else// Text watermarking  
{ 
$temp = imagettfbbox(ceil($textFont*2.5),0,"c:/windows/fonts/stcaiyun.ttf",$waterText);// Obtained using  TrueType  The range of text in a font  
$w = $temp[2] - $temp[6]; 
$h = $temp[3] - $temp[7]; 
unset($temp); 
$label = " Text area "; 
} 
if( ($ground_w<$w) || ($ground_h<$h) ) 
{ 
echo " The length or width of the image to be watermarked ".$label." Too small to generate a watermark! "; 
return; 
} 
switch($waterPos) 
{ 
case 0:// random  
$posX = rand(0,($ground_w - $w)); 
$posY = rand(0,($ground_h - $h)); 
break; 
case 1://1 The top is on the left  
$posX = 0; 
$posY = 0; 
break; 
case 2://2 Is the top centered  
$posX = ($ground_w - $w) / 2; 
$posY = 0; 
break; 
case 3://3 The top is on the right  
$posX = $ground_w - $w; 
$posY = 0; 
break; 
case 4://4 The center is on the left  
$posX = 0; 
$posY = ($ground_h - $h) / 2; 
break; 
case 5://5 It is centered in the middle  
$posX = ($ground_w - $w) / 2; 
$posY = ($ground_h - $h) / 2; 
break; 
case 6://6 Center to the right  
$posX = $ground_w - $w; 
$posY = ($ground_h - $h) / 2; 
break; 
case 7://7 Is the bottom in the left  
$posX = 0; 
$posY = $ground_h - $h; 
break; 
case 8://8 Is the bottom center  
$posX = ($ground_w - $w) / 2; 
$posY = $ground_h - $h; 
break; 
case 9://9 Is the bottom in the right  
$posX = $ground_w - $w; 
$posY = $ground_h - $h; 
break; 
default:// random  
$posX = rand(0,($ground_w - $w)); 
$posY = rand(0,($ground_h - $h)); 
break; 
} 
// Set the color mixing mode of the image  
imagealphablending($ground_im, true); 
if($isWaterImage)// Image watermarking  
{ 
imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);// Copy the watermark to the target file  
} 
else// Text watermarking  
{ 
if( !empty($textColor) && (strlen($textColor)==7) ) 
{ 
$R = hexdec(substr($textColor,1,2)); 
$G = hexdec(substr($textColor,3,2)); 
$B = hexdec(substr($textColor,5)); 
} 
else 
{ 
die(" Watermark text color format is incorrect! "); 
} 
imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B)); 
} 
// Generate the watermarked image  
@unlink($groundImage); 
switch($ground_info[2])// Gets the format of the background image  
{ 
case 1:imagegif($ground_im,$groundImage);break; // create gif Format picture  
case 2:imagejpeg($ground_im,$groundImage);break; // create jpeg Format picture  
case 3:imagepng($ground_im,$groundImage);break; // create png Format picture  
default:die($errorMsg); 
} 
// Free memory  
if(isset($water_info)) unset($water_info); 
if(isset($water_im)) imagedestroy($water_im); 
unset($ground_info); 
imagedestroy($ground_im); 
} 
?> 
<?php 
for ($i=0;$i<count($_FILES['userfile']['tmp_name']);$i++) 
{ 
$upfile="./img/".($i+1).".png";// Change the path here to yours  
if(move_uploaded_file($_FILES['userfile']['tmp_name'][$i],$upfile)){ 
imageWaterMark($upfile,9,"./shuiyin.png","Made By Chenduan",5,"#FF0000"); 
/* 
*  Function: image watermarking  ( Watermarking supports images or text ) 
* imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$textColor="#FF0000") 
*  Parameters:  
* $groundImage  Background image, that is, the need to add watermark image, only support GIF,JPG,PNG Format;  
* $waterPos  Watermark location, yes 10 Kind of state, 0 Is a random position;  
* 1 Is the top on the left, 2 Is the top centered, 3 Is the top in the right;  
* 4 To the left of the middle, 5 Is centered in the middle, 6 To the right of the middle;  
* 7 Is the bottom on the left, 8 Is the bottom center, 9 Is the bottom to the right;  
* $waterImage  Image watermarking, that is, as a watermark of the image, only support GIF,JPG,PNG Format;  
* $waterText  Text watermark, that is, text as a watermark, support ASCII Code, do not support Chinese;  
* $textFont  Text size, the value is 1 , 2 , 3 , 4 or 5 By default, 5 ;  
* $textColor  Text color, the value is 106 Base color value, default is #FF0000( red ) ;  
*/ 
echo "<img src=\"".$upfile."\" border=\"0\">"; 
echo " The first ".($i+1)." The operation of the picture was successful <br>"; 
} 
else{ 
echo " The first ".($i+1)." I can't upload a picture <br>"; 
} 
} 
?> 

Related articles: