• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

用来给图片加水印的PHP类_php技巧

php 搞代码 3年前 (2022-01-26) 16次浏览 已收录 0个评论
<?php     <BR>/*    <BR>* 功能:PHP图片水印 (水印支持图片或文字)    <BR>* 参数:    <BR>*     $groundImage   背景图片,即需要加水印的图片,暂只支持GIF,JPG,PNG格式;    <BR>*     $waterPos     水印位置,有10种状态,0为随机位置;    <BR>*                 1为顶端居左,2为顶端居中,3为顶端居右;    <BR>*                 4为中部居左,5为中部居中,6为中部居右;    <BR>*                 7为底端居左,8为底端居中,9为底端居右;    <BR>*     $waterImage     图片水印,即作为水印的图片,暂只支持GIF,JPG,PNG格式;    <BR>*     $waterText     文字水印,即把文字作为为水印,支持ASCII码,不支持中文;    <BR>*     $textFont     文字大小,值为1、2、3、4或5,默认为5;    <BR>*     $textColor     文字颜色,值为十六进制颜色值,默认为#FF0000(红色);    <BR>*    <BR>* 注意:Support GD 2.0,Support FreeType、GIF Read、GIF Create、JPG 、PNG    <BR>*     $waterImage 和 $waterText 最好不要同时使用,选其中之一即可,优先使用 $waterImage。    <BR>*     当$waterImage有效时,参数$waterString、$stringFont、$stringColor均不生效。    <BR>*     加水印后的图片的文件名和 $groundImage 一样。    <BR>* 作者:longware @ 2004-11-3 14:15:13    <BR>*/    <BR>function imageWaterMark($groundImage,$waterPos=0,$waterImage=”",$waterText=”",$textFont=5,$textColor=”#FF0000″)     <BR>{     <BR>  $isWaterImage = FALSE;     <BR>  $formatMsg = “暂不支持该文件格式,请用图片处理软件将图片转换为GIF、JPG、PNG格式。”;     <br><br>  //读取水印文件     <BR>  if(!emptyempty($waterImage) && file_exists($waterImage))     <BR>  {     <BR>    $isWaterImage = TRUE;     <BR>    $water_info = getimagesize($waterImage);     <BR>    $water_w   = $water_info[0];//取得水印图片的宽     <BR>    $water_h   = $water_info[1];//取得水印图片的高     <br><br>    switch($water_info[2])//取得水印图片的格式     <BR>    {     <BR>        case 1:$water_im = imagecreatefromgif($waterImage);break;     <BR>        case 2:$water_im = imagecreatefromjpeg($waterImage);break;     <BR>        case 3:$water_im = imagecreatefrompng($waterImage);break;     <BR>        default:die($formatMsg);     <BR>    }     <BR>  }     <br><br>  //读取背景图片     <BR>  if(!emptyempty($groundImage) && file_exists($groundImage))     <BR>  {     <BR>    $ground_info = getimagesize($groundImage);     <BR>    $ground_w   = $ground_info[0];//取得背景图片的宽     <BR>    $ground_h   = $ground_info[1];//取得背景图片的高     <br><br>    switch($ground_info[2])//取得背景图片的格式     <BR>    {     <BR>        case 1:$ground_im = imagecreatefromgif($groundImage);break;     <BR>        case 2:$ground_im = imagecreatefromjpeg($groundImage);break;     <BR>        case 3:$ground_im = imagecreatefrompng($groundImage);break;     <BR>        default:die($formatMsg);     <BR>    }     <BR>  }     <BR>  else    <BR>  {     <BR>    die(”需要加水印的图片不存在!”);     <BR>  }     <br><br>  //水印位置     <BR>  if($isWaterImage)//图片水印     <BR>  {     <BR>    $w = $water_w;     <BR>    $h = $water_h;     <BR>    $label = “图片的”;     <BR>  }     <BR>  else//文字水印     <BR>  {     <BR>    $temp = imagettfbbox(ceil($textFont*5),0,”./cour.ttf”,$waterText);//取得使用 TrueType 字体的文本的范围     <BR>    $w = $temp[2] - $temp[6];     <BR>    $h = $temp[3] - $temp[7];     <BR>    unset($temp);     <BR>    $label = “文字区域”;     <BR>  }     <BR>  if( ($ground_w<$w) || ($ground_h<$h) )     <BR>  {     <BR>    echo “需要加水印的图片的长度或宽度比水印”.$label.”还小,无法生成水印!”;     <BR>    return;     <BR>  }     <BR>  switch($waterPos)     <BR>  {     <BR>    case 0://随机     <BR>        $posX = rand(0,($ground_w - $w));     <BR>        $posY = rand(0,(<i style="color:transparent">本文来源gaodai$ma#com搞$$代**码)网8</i><strong>搞代gaodaima码</strong>$ground_h - $h));     <BR>        break;     <BR>    case 1://1为顶端居左     <BR>        $posX = 0;     <BR>        $posY = 0;     <BR>        break;     <BR>    case 2://2为顶端居中     <BR>        $posX = ($ground_w - $w) / 2;     <BR>        $posY = 0;     <BR>        break;     <BR>    case 3://3为顶端居右     <BR>        $posX = $ground_w - $w;     <BR>        $posY = 0;     <BR>        break;     <BR>    case 4://4为中部居左     <BR>        $posX = 0;     <BR>        $posY = ($ground_h - $h) / 2;     <BR>        break;     <BR>    case 5://5为中部居中     <BR>        $posX = ($ground_w - $w) / 2;     <BR>        $posY = ($ground_h - $h) / 2;     <BR>        break;     <BR>    case 6://6为中部居右     <BR>        $posX = $ground_w - $w;     <BR>        $posY = ($ground_h - $h) / 2;     <BR>        break;     <BR>    case 7://7为底端居左     <BR>        $posX = 0;     <BR>        $posY = $ground_h - $h;     <BR>        break;     <BR>    case 8://8为底端居中     <BR>        $posX = ($ground_w - $w) / 2;     <BR>        $posY = $ground_h - $h;     <BR>        break;     <BR>    case 9://9为底端居右     <BR>        $posX = $ground_w - $w;     <BR>        $posY = $ground_h - $h;     <BR>        break;     <BR>    default://随机     <BR>        $posX = rand(0,($ground_w - $w));     <BR>        $posY = rand(0,($ground_h - $h));     <BR>        break;       <BR>  }     <br><br>  //设定图像的混色模式     <BR>  imagealphablending($ground_im, true);     <br><br>  if($isWaterImage)//图片水印     <BR>  {     <BR>    imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//拷贝水印到目标文件           <BR>  }     <BR>  else//文字水印     <BR>  {     <BR>    if( !emptyempty($textColor) && (strlen($textColor)==7) )     <BR>    {     <BR>        $R = hexdec(substr($textColor,1,2));     <BR>        $G = hexdec(substr($textColor,3,2));     <BR>        $B = hexdec(substr($textColor,5));     <BR>    }     <BR>    else    <BR>    {     <BR>        die(”水印文字颜色格式不正确!”);     <BR>    }     <BR>    imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));           <BR>  }     <br><br>  //生成水印后的图片     <BR>  @unlink($groundImage);     <BR>  switch($ground_info[2])//取得背景图片的格式     <BR>  {     <BR>    case 1:imagegif($ground_im,$groundImage);break;     <BR>    case 2:imagejpeg($ground_im,$groundImage);break;     <BR>    case 3:imagepng($ground_im,$groundImage);break;     <BR>    default:die($errorMsg);     <BR>  }     <br><br>  //释放内存     <BR>  if(isset($water_info)) unset($water_info);     <BR>  if(isset($water_im)) imagedestroy($water_im);     <BR>  unset($ground_info);     <BR>  imagedestroy($ground_im);     <BR>}     <BR>//—————————————————————————————     <BR>$id=$_REQUEST['id'];     <BR>$num = count($_FILES['userfile']['name']);     <BR>print_r($_FILES['userfile']);     <BR>print_r($_FILES['userfile']['name']);     <br><br>echo $num;     <BR>echo “<bR>”;     <BR>if(isset($id)){     <BR> for($i=0;$i<$id;$i++){     <br><br>  if(isset($_FILES) && !emptyempty($_FILES['userfile']) && $_FILES['userfile']['size']>0)     <BR>{     <BR>  $uploadfile = “./”.time().”_”.$_FILES['userfile'][name][$i];     <BR>  echo “<br>”;     <BR>  echo $uploadfile;     <BR>  if (copy($_FILES['userfile']['tmp_name'][$i], $uploadfile))     <BR>  {     <BR>    echo “OK<br>”;     <br><br>    //文字水印     <BR>    //imageWaterMark($uploadfile,5,”",”HTTP://www.lvye.info”,5,”#cccccc“);     <br><br>    //图片水印     <BR>    $waterImage=”logo_ok1.gif”;//水印图片路径     <BR>    imageWaterMark($uploadfile,9,$waterImage);     <br><br>    echo “”;     <BR>  }     <BR>  else    <BR>  {     <BR>    echo “Fail<br>”;     <BR>  }     <BR>}     <BR> }     <BR>}     <br><br>?>     <BR>     <BR><?php     <BR>for($a=0;$a<$id;$a++){     <BR> echo “文件: <br>”;     <br><br>}     <BR>?>     <BR>     <BR>    <BR>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:用来给图片加水印的PHP类_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址