/* * Function: php image watermark (watermark supports images or text) * Parameters: *$groundimage background image, that is, the image that needs to be watermarked, currently only supports gif, jpg , png format; *$waterpos watermark position, there are 10 states, 0 is a random position; *1 is the top left, 2 is the top center, 3 is the top right; *4 is the middle left, 5 is the middle center , 6 means the middle is on the right; *7 means the bottom is on the left, 8 is the bottom is in the center, 9 is the bottom is on the right; *$waterimage image watermark, that is, the image used as the watermark, currently only supports gif, jpg, and png formats ; *$watertext text watermark, that is, text is used as a watermark, supports ASCII code, does not support Chinese; *$textfont text size, the value is 1, 2, 3, 4 or 5, the default is 5; *$textcolor Text color, the value is a hexadecimal color value, the default is #ff0000 (red); * * Note: support gd 2.0, support freetype, gif read, gif create, jpg, png *$waterimage and $watertext are the most It is best not to use them at the same time, just choose one of them, and use $waterimage first. *When $waterimage is valid, the parameters $waterstring, $stringfont, and $stringcolor are not valid. *The file name of the watermarked image is the same as $groundimage. * Author: longware @ 2004-11-3 14:15:13 */ function imagewatermark($groundimage,$waterpos=0,$waterimage="",$watertext="",$textfont=5,$textcolor ="#ff0000") { $iswaterimage = false; $formatmsg = "This file format is not supported yet. Please use image processing software to convert the image to gif, jpg, or png format."; //Read watermark file if(!empty($waterimage) && file_exists($waterimage)) { $iswaterimage = true; $water_info = getimagesize($waterimage); $water_w = $water_info[ 0];//Get the width of the watermark image $water_h = $water_info[1];//Get the height of the watermark image switch($water_info[2])//Get the format of the watermark image { case 1:$ water_im = imagecreatefromgif($waterimage);break; case 2:$water_im = imagecreatefromjpeg($waterimage);break; case 3:$water_im = imagecreatefrompng($waterimage);break; default:die($formatmsg); } } //Read the background image if(!empty($groundimage) && file_exists($groundimage)) { $ground_info = getimagesize($groundimage); $ground_w = $ground_info[0];// Get the width of the background image $ground_h = $ground_info[1];//Get the height of the background image switch($ground_info[2])//Get 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("The picture that needs to be watermarked does not exist!"); } //Watermark position if($iswaterimage)//Picture watermark { $w = $water_w; $h = $water_h; $label = "Picture"; } else//Text watermark { $temp = imagettfbbox(ceil($textfont*2.5),0,"./cour.ttf",$watertext);//Get Range of text using truetype 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 that needs to be watermarked is longer than the watermark ".$label." Small, unable to generate watermark! "; return; } switch($waterpos) { case 0://random $posx = rand(0,($ground_w - $w)); $posy = rand(0,($ground_h - $h)); break; case 1://1 means top left $posx = 0; $posy = 0; break; case 2://2 means top center $posx = ($ ground_w - $w) / 2; $posy = 0; break; case 3://3 is the top right $posx = $ground_w - $w; $posy = 0; break; case 4 ://4 means center left $posx = 0; $posy = ($ground_h - $h) / 2; break; case 5://5 means center center $posx = ($ground_w - $w ) / 2; $posy = ($ground_h - $h) / 2; break; case 6://6 is the middle right $posx = $ground_w - $w; $posy = ($ground_h - $h) / 2; break; case 7://7 is bottom left $posx = 0; $posy = $ground_h - $h; break; case 8://8 is bottom center $posx = ($ground_w - $w) / 2; $posy = $ground_h - $h; break; case 9://9 is bottom right $posx = $ground_w - $w; $posy = $ground_h - $h; break; default://random $posx = rand(0,($ground_w - $w)); $posy = rand(0,($ground_h - $h) ; posy, 0, 0, $water_w,$water_h);//Copy watermark to target file } else//Text watermark { 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("The watermark text color format is incorrect!"); } imagestring ( $ground_im, $textfont, $posx, $posy, $watertext, imagecolorallocate($ground_im, $r, $g, $b)); } //The image after generating the watermark @unlink($groundimage); switch($ground_info[2])//Get the format of the background image { case 1:imagegif($ground_im,$groundimage);break; case 2:imagejpeg($ground_im,$ groundimage);break; case 3:imagepng($ground_im,$groundimage);break; default:die($errormsg); } //Release memory if(isset($water_info)) unset($water_info) ; if(isset($water_im)) imagedestroy($water_im); unset($ground_info); imagedestroy($ground_im); } //-------- if(isset($_files ) && !empty($_files['userfile']) && $_files['userfile']['size']>0)
{
$uploadfile = "./".time()."_".$ _files['userfile']['name'];
if (copy($_files['userfile']['tmp_name'], $uploadfile))
{
echo "ok ";
//Text watermark
imagewatermark($uploadfile,0,"","http://blog.csdn.net/longware/",5,"#ff0000");
//Image watermark
//$waterimage="./hanweb_shuiyin. gif";//Watermark image path
//imagewatermark($uploadfile,0,$waterimage);
echo " " ;
}
else
{
echo "fail ";
}
}
?>
File:
Copy code