php上传图片加水印(图片水印,文字水印)

 更新时间:2016年11月25日 16:59  点击:1311
这是一款比较完整理的在用户上传图片时就自动给图片增加上水印,这款增加水印功能可以增加文字水印与图片水印哦。
 代码如下 复制代码

/*
 * created on 2010-6-21
 *
 * the class for control image
 *
 * made by s71ence
 *
 * @$img_path 图片路径
 * @$is_auto_reduce 图片是否自动按照大小等级压缩 1是
 * @$is_appoint 是否手动进行压缩或放大 1是
 * @$multiple 手动指定压缩/放大比例
 * @$is_water_str 是否加水印文字 1是
 * @$water_str 水印文字
 * @$is_watermark 是否加水印图片 1是
 * @$logo_path 水印图片路径
 * @$is_display 是否显示图片 1是
 * @$is_create 是否生成压缩后的图片 1是
 *
 * 注:
 * 1.生成新图时不可显示图片,即$isdisplay和$iscreate不可同时置为1
 * 2.当图片宽或高小于1000时,需手动设置压缩比例进行压缩
 * 3.不建议启用水印,若要启用,建议原图片大小最好在1000以内
 * 4.水印文字中不可含有中文
 * 5.新生成的图片在原目录文件中,支持n个层级
 */

 class image_control
 {
  private $img_path;
  private $is_auto_reduce;
  private $is_appoint;
  private $multiple;
  private $is_water_str;
  private $water_str;
  private $is_watermark;
  private $logo_path;
  private $is_display;
  private $is_create;

  function __construct($img_path,$is_auto_reduce,$is_appoint,$multiple,$is_water_str,$water_str,$is_watermark,$logo_path,$is_display,$is_create)
  {
   $this->img_path=$img_path;
   $this->is_auto_reduce=$is_auto_reduce;
   $this->is_appoint=$is_appoint;
   $this->multiple=$multiple;
   $this->is_water_str=$is_water_str;
   $this->water_str=$water_str;
   $this->is_watermark=$is_watermark;
   $this->logo_path=$logo_path;
   $this->is_display=$is_display;
   $this->is_create=$is_create;
  }

  function img_control()
  {
  //获取原图
  $img_info=getimagesize($this->img_path);

  switch($img_info[2])
  {
   case 1:
    $img_get=@imagecreatefromgif($this->img_path);
   break;

   case 2:
    $img_get=@imagecreatefromjpeg($this->img_path);
   break;

   case 3:
    $img_get=@imagecreatefrompng($this->img_path);
   break;
  }

  //文字水印
  if($this->is_water_str==1)
  {
   //imagettftext(原图,文字大小,文字旋转,水印起始坐标x,水印起始坐标y,$te,'simhei.ttf',$str);
   $te=imagecolorallocate($img_get,255,255,255);
   $str=iconv("gbk","utf-8",$this->water_str);//水印文字
   imagettftext($img_get,16,0,$img_info[0]-200,$img_info[1]-20,$te,'msyh.ttf',$str);
  }

  //图片水印
  if($this->is_watermark==1)
  {
   //水印图片处理
   $logo_info=getimagesize($this->logo_path);

   switch($logo_info[2])
   {
    case 1:
     $logo=@imagecreatefromgif($this->logo_path);
    break;

    case 2:
     $logo=@imagecreatefromjpeg($this->logo_path);
    break;

    case 3:
     $logo=@imagecreatefrompng($this->logo_path);
    break;
   }

   //水印logo图片
   //函数说明:imagecopy(原图,水印图片,水印坐标x,水印坐标y,水印图片开始坐标x,水印图片开始坐标y,'水印图片宽','水印图片高');
   imagecopy($img_get,$logo,0,0,0,0,$logo_info[0],$logo_info[1]);
  }

  //自动图片压缩 按图片大小分级自动压缩
  //imagecopyresized(画布,原图,画布起始x坐标,画布起始y坐标,原图起始x坐标,原图起始x坐标,新图片宽,新图片高,原图片宽,原图片高);
  if($this->is_auto_reduce==1)
  {
   if($img_info[0]>=3000 || $img_info[1]>=3000)
   {
    $new_image_get=imagecreatetruecolor($img_info[0]*0.03,$img_info[1]*0.03);//生成画布
    imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*0.03,$img_info[1]*0.03,$img_info[0],$img_info[1]);
   }
   else if($img_info[0]>=2500 || $img_info[1]>=2500)
   {
    $new_image_get=imagecreatetruecolor($img_info[0]*0.04,$img_info[1]*0.04);
    imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*0.04,$img_info[1]*0.04,$img_info[0],$img_info[1]);
   }
   else if($img_info[0]>=2000 || $img_info[1]>=2000)
   {
    $new_image_get=imagecreatetruecolor($img_info[0]*0.05,$img_info[1]*0.05);
    imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*0.05,$img_info[1]*0.05,$img_info[0],$img_info[1]);
   }
   else if($img_info[0]>=1500 || $img_info[1]>=1500)
   {
    $new_image_get=imagecreatetruecolor($img_info[0]*0.08,$img_info[1]*0.08);
    imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*0.08,$img_info[1]*0.08,$img_info[0],$img_info[1]);
   }
   else if($img_info[0]>=1000 || $img_info[1]>=1000)
   {
    $new_image_get=imagecreatetruecolor($img_info[0]*0.1,$img_info[1]*0.1);
    imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*0.1,$img_info[1]*0.1,$img_info[0],$img_info[1]);
   }
   else if($img_info[0]>=500 || $img_info[1]>=500)
   {
    $new_image_get=imagecreatetruecolor($img_info[0]*0.2,$img_info[1]*0.2);
    imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*0.2,$img_info[1]*0.2,$img_info[0],$img_info[1]);
   }
   else if($img_info[0]>=300 || $img_info[1]>=300)
   {
    $new_image_get=imagecreatetruecolor($img_info[0]*0.3,$img_info[1]*0.3);
    imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*0.3,$img_info[1]*0.3,$img_info[0],$img_info[1]);
   }
   else
   {
    $new_image_get=imagecreatetruecolor($img_info[0]*1,$img_info[1]*1);
    imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*1,$img_info[1]*1,$img_info[0],$img_info[1]);
   }
  }

  //手动图片压缩
  //imagecopyresized(画布,原图,画布起始x坐标,画布起始y坐标,原图起始x坐标,原图起始x坐标,新图片宽,新图片高,原图片宽,原图片高);
  if($this->is_appoint)
  {
   $new_image_get=imagecreatetruecolor($img_info[0]*$this->multiple,$img_info[1]*$this->multiple);//生成画布
   imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*$this->multiple,$img_info[1]*$this->multiple,$img_info[0],$img_info[1]);
  }

  //图像输出
  if($this->is_display==1)
  {
   header("content-type: image/jpeg");
   return imagejpeg($new_image_get);
  }

  //新图像生成
  if($this->is_create==1)
  {
   $new_name=explode("/",$this->img_path);
   $new_name_string="";

   for($i=0;$i<count($new_name)-1;$i++)
   {
    $new_name_string.=$new_name[$i]."/";
   }

   $new_img_path=$new_name_string."new".$new_name[$i];

   if(imagejpeg($new_image_get,$new_img_path) && imagejpeg($img_get,$this->img_path))
   {
    setcookie("img_new_path", $new_img_path);
       //return "图片生成成功!<br/>新图:".$new_img_path."<br/>原图:".$this->img_path;
   }
   else
   {
    return "图片生成失败,请检查配置是否正确!";
   }
  }
  }

  function __desctruct()
  {
   //clear
  }
 }

//调用方法

 代码如下 复制代码

/* $img_path="../users/user_photo/t2.jpg"; //被操作的图片路径
 $is_auto_reduce=1;//图片是否自动按照大小等级压缩 1是
 $is_appoint=0;//是否手动进行压缩 1是
 $multiple=0.5;//手动指定压缩比例
 $is_water_str=0;//是否加水印文字
 $water_str="www.111cn.net";//水印文字
 $is_watermark=0;//是否加水印图片 1是
 $logo_path="../image/logo_about.gif";//水印图片路径
 $is_display=0;//是否显示图片 1是
 $is_create=1;//是否生成压缩后的图片 1是
 $img=new image_control($img_path,$is_auto_reduce,$is_appoint,$multiple,$is_water_str,$water_str,$is_watermark,$logo_path,$is_display,$is_create);
 echo $img->img_control();*/

这款程序给图片加文字水印时是调用 了C:\\WINDOWS\\Fonts\\\\SIMHEI.TTF字体,给图片加水印时就可以自定图片哦。

$image->wprint_img();//执行图片水印
$image->wprint_string();//执行文字水印
*/

 代码如下 复制代码

class editimage{
 private $imagefile;//图片文件
 private $smallimg;//水印图片
 private $string;//水印文字
 private $position;//存放位置
 private $dst_x=600;//原始图片打水印x坐标
 private $dst_y=0;//原始图片打水印y坐标
 private $str_x=450;
 private $str_y=200;
 private $font="c:windows ontssimhei.ttf";//原始图片打水印字体路径
 private $imgej;// imagecolorallocate后的变量
 
 function __get($value){
  return $this->$value;
 }
 function __set($property,$value){
  $this->$property=$value;
 }
 /**
  * 构造函数初始化
  *
  * @param string $imagefile 被上水印的文件
  * @param string $smallimg 水印文件
  * @param string $string 水印文字
  * @param string $position 存放位置
  * @param int $dst_x  被上水印的图片x
  * @param int $dst_y  被上水印的图片y
  */
 function __construct($imagefile,$smallimg='',$string=''){//,$position='',$dst_x=0,$dst_y=0
  $this->imagefile=$imagefile;
  $this->smallimg=$smallimg;
  $this->string=$string;
  $this->imgej=$this->imagecreatef($this->imagefile);
 }

 function get_extname($file){//获取文件的后缀名
  if (file_exists($this->imagefile)) {
   $img=getimagesize($file);
   switch ($img[2]){
    case "1":
     return "gif";
    case "2":
     return "jpg";
    case "3":
     return "png";
   }
  }else{
   return false;
  }
 }  
 

 

我们经常会碰到要对大量的图片进行一次性增加水印,这就会碰到批量增加图片水印功能了,我们为你提供一款php批量增加水印的功能。


//config.php
//=================================用户配置区=================================

$per=1;     //每次处理的图片数目
$dir1='img';    //输入目录
$dir2='img2';    //输出目录  输出图片会保存于$dir2/$dir1目录下面
$mark_img='www.111cn.net.gif';    //水印图片
$mark_text=''; //水印文字内容  【不支持中文】
$mark_text_size=25;   //水印文字大小
$mark_pos=5;    //水印位置,0为随机,1为顶端居左,2为顶端居中,3为顶端居右,4为中部居左,5为中部居中,6为中部居右,7为底端居左,8为底端居中,9为底端居右;

//=================================用户配置区【结束】=================================

 代码如下 复制代码

//mark.php加水印文件
function go( $url, $t = 0 )
{
    exit( ( "<script language=网页特效>settimeout("location.replace('".$url."')",".$t * 1000 ).")</script>" );
}

function imagewatermark( $product_img, $waterpos = 0, $waterimage = "", $watertext = "", $textfont = 25, $textcolor = "#ff0000", $newdir = "marked" )
{
    echo "正在处理图片:".$product_img."<br>";
    $iswaterimage = false;
    $formatmsg = "暂不支持该文件格式,请用图片处理软件将图片转换为gif、jpg、png格式。";
    if ( !empty( $waterimage ) && file_exists( $waterimage ) )
    {
        $iswaterimage = true;
        $water_info = getimagesize( $waterimage );
        $water_w = $water_info[0];
        $water_h = $water_info[1];
        switch ( $water_info[2] )
        {
        case 1 :
            $water_im = imagecreatefromgif( $waterimage );
            break;
        case 2 :
            $water_im = imagecreatefromjpeg( $waterimage );
            break;
        case 3 :
            $water_im = imagecreatefrompng( $waterimage );
            break;
        default :
            echo $formatmsg;
            return;
        }
    }
    if ( !empty( $product_img ) && file_exists( $product_img ) )
    {
        $ground_info = getimagesize( $product_img );
        $ground_w = $ground_info[0];
        $ground_h = $ground_info[1];
        switch ( $ground_info[2] )
        {
        case 1 :
            $ground_im = imagecreatefromgif( $product_img );
            break;
        case 2 :
            $ground_im = imagecreatefromjpeg( $product_img );
            break;
        case 3 :
            $ground_im = imagecreatefrompng( $product_img );
            break;
        default :
            echo $formatmsg;
            return;
        }
    }
    else
    {
        exit( "需要加水印的图片不存在!" );
    }
    if ( $iswaterimage )
    {
        $w = $water_w;
        $h = $water_h;
        $label = "图片的";
    }
    else
    {
        $temp = @imagettfbbox( @ceil( $textfont * 2.5 ), 0, "arial.ttf", $watertext );
        $w = $temp[2] - $temp[6];
        $h = $temp[3] - $temp[7];
        unset( $temp );
        $label = "文字区域";
    }
    if ( $ground_w < $w || $ground_h < $h )
    {
        echo "需要加水印的图片的长度或宽度比水印".$label."还小,无法生成水印!";
    }
    else
    {
        switch ( $waterpos )
        {
        case 0 :
            $posx = rand( 0, $ground_w - $w );
            $posy = rand( 0, $ground_h - $h );
            break;
        case 1 :
            $posx = 0;
            $posy = 0;
            break;
        case 2 :
            $posx = ( $ground_w - $w ) / 2;
            $posy = 0;
            break;
        case 3 :
            $posx = $ground_w - $w;
            $posy = 0;
            break;
        case 4 :
            $posx = 0;
            $posy = ( $ground_h - $h ) / 2;
            break;
        case 5 :
            $posx = ( $ground_w - $w ) / 2;
            $posy = ( $ground_h - $h ) / 2;
            break;
        case 6 :
            $posx = $ground_w - $w;
            $posy = ( $ground_h - $h ) / 2;
            break;
        case 7 :
            $posx = 0;
            $posy = $ground_h - $h;
            break;
        case 8 :
            $posx = ( $ground_w - $w ) / 2;
            $posy = $ground_h - $h;
            break;
        case 9 :
            $posx = $ground_w - $w;
            $posy = $ground_h - $h;
            break;
        default :
            $posx = rand( 0, $ground_w - $w );
            $posy = rand( 0, $ground_h - $h );
            break;
        }
        imagealphablending( $ground_im, true );
  imagecopyresampled($ground_im, $water_im,0,0, $posx, $posy, 0, 0, $water_w, $water_h);
        if ( $iswaterimage )
        {
   
            imagecopymerge( $ground_im, $water_im, $posx, $posy, 0, 0, $water_w, $water_h,35 );
        }
        else
        {
            if ( !empty( $textcolor ) && strlen( $textcolor ) == 7 )
            {
                $r = hexdec( substr( $textcolor, 1, 2 ) );
                $g = hexdec( substr( $textcolor, 3, 2 ) );
                $b = hexdec( substr( $textcolor, 5 ) );
            }
            else
            {
                exit( "水印文字颜色格式不正确!" );
            }
            imagestring( $ground_im, $textfont, $posx, $posy, $watertext, imagecolorallocate( $ground_im, $r, $g, $b ) ,35);
        }
        switch ( $ground_info[2] )
        {
        case 1 :
            imagegif( $ground_im, $newdir."/".$product_img );
            break;
        case 2 :
            imagejpeg( $ground_im, $newdir."/".$product_img );
            break;
        case 3 :
            imagepng( $ground_im, $newdir."/".$product_img );
            break;
        default :
            echo $errormsg;
        }
        if ( isset( $water_info ) )
        {
            unset( $water_info );
        }
        if ( isset( $water_im ) )
        {
            imagedestroy( $water_im );
        }
        unset( $ground_info );
        imagedestroy( $ground_im );
        unset( $water_im );
        unset( $ground_im );
    }
}

set_time_limit( 0 );
include( "config.php" );
if ( $_get[id] )
{
    $id = $_get[id];
}
else
{
    $id = 1;
}
if ( !is_dir( $dir2."/".$dir1 ) )
{
    mkdir( $dir2."/".$dir1, 511 );
}
$ti = 0;
$dir = opendir( $dir1 );
while ( $file = readdir( $dir ) )
{
    if ( $file == "." || $file == ".." )
    {
    }
    else
    {
        ++$ti;
        if ( $ti < ( $id - 1 ) * $per )
        {
        }
        else
        {
            if ( ( $id - 1 ) * $per <= $ti && $ti < $id * $per )
            {
                imagewatermark( $dir1."/".$file, $mark_pos, $mark_img, $mark_text, $mark_text_size, "#ff0000", $dir2 );
            }
            if ( $id * $per <= $ti )
            {
                closedir( $dir );
                go( "mark.php?id=".( $id + 1 ) );
            }
        }
    }
}

echo "<center>

这款图片上传源代码是一款可以上传图片并且还具有给上传的图片生成缩略图与增加水印功能哦,可以说是一款完美的图片上传类哦。

 

 代码如下 复制代码

class upfile {
 public $filepath = "www.111cn.net/"; //上传文件存放文件夹

 public $filesize = 1000000; //允许上传的大小

 //如果要修改允许上传文件的类型  请搜索 【 switch ($upfiletype) { //文件类型  】

 public $reimagesize = array (
  true, //是否生成缩略图
  400, //缩略图宽
  300,//缩略图高
  "" //缩略图存放文件夹 如果为空和当前要生成缩略图的文件在同一目录 文件前缀r_
 ); //是否生成缩略图 array(生成或不生成,缩略图宽,缩略图高,存放文件夹); 注意:存放文件夹后跟 '/'

 public $india = true; //是否打水印 true打 false不打

 public $indiaimage = ""; //水印图片地址为空则不打图片水印 如果有文字水印建议不要开启图片水印

 public $indiaimagex = 100; //图片距离图片左边距离

 public $indiaimagey = 10; //图片距离图片上面距离

 public $indiatext = "www.111cn.net"; //水印文字

 public $fontsize = 6; //水印文字大小,1最小6最大

 public $indiatextx = 10; //文字距离图片左边距离

 public $indiatexty = 10; //文字距离图片上面距离

 public $r = 250; //图片颜色三原色 $r红

 public $g = 250; //$g绿

 public $b = 250; //$b蓝

 public $indiapath = ""; //加了水印的图片保存路径,如果为空就直接替代原来的图片

 //开始上传处理
 function uploadfile($upfile) {
  if ($upfile == "") {
   die("uploadfile:参数不足");
  }
  if (!file_exists($this->filepath)) {
   mkdir($this->filepath);
  }
  $upfiletype = $upfile['type'];
  $upfilesize = $upfile['size'];
  $upfiletmpname = $upfile['tmp_name'];
  $upfilename = $upfile['name'];
  $upfileerror = $upfile['error'];
  if ($upfilesize > $this->filesize) {
   return false; //文件过大
  }
  switch ($upfiletype) { //文件类型
   case 'image/jpeg' :
    $type = 'jpg';
    break;
   case 'image/pjpeg' :
    $type = 'jpg';
    break;
   case 'image/png' :
    $type = 'png';
    break;
   case 'image/gif' :
    $type = 'gif';
    break;
  }
  if (!isset ($type)) {
   return false; //不支持此类型
  }
  if (!is_uploaded_file($upfiletmpname) or !is_file($upfiletmpname)) {
   return false;
   ; //文件不是经过正规上传的;
  }
  if ($this->upfileerror != 0) {
   return false; //其他错误
  }
  if ($this->upfileerror == 0) {
   if (!file_exists($upfiletmpname)) {
    return false; //临时文件不存在
   } else {
    $filename = date("ymdhis", time() + 3600 * 8); //图片已当前时间命名
    $filename = $this->filepath . $filename . "." . $type;
    if (!move_uploaded_file($upfiletmpname, $filename)) {
     return false; //文件在移动中丢失
    } else {
     if ($this->india == true) {
      $this->goindia($filename, $type,true);
     } else {
      if ($this->reimagesize[0] == true) {
       $this->goreimagesize($filename, $type);
      } else {
       return true; //上传成功!
       unlink($upfiletmpname);
      }
     }
    }

   }
  }

 }
 //添加水印处理
 function goindia($filename, $filetype,$reimage=false) {
  if (!file_exists($filename)) {
   $this->reerror(7); //要添加水印的文件不存在
  } else {
   if ($filetype == "jpg") {
    $im = imagecreatefromjpeg($filename);
   } else
    if ($filetype == "gif") {
     $im = imagecreatefromgif($filename);
    } else
     if ($filetype == "png") {
      $im = imagecreatefrompng($filename);
     }
   if ($this->indiatext != "") { //如果水印文字不为空
    $textcolor = imagecolorallocate($im, $this->r, $this->g, $this->b); //设置文字颜色
    imagestring($im, $this->fontsize, $this->indiatextx, $this->indiatexty, $this->indiatext, $textcolor); //将文字写入图片
   }
   if ($this->indiaimage != "") {//如果水印图片不为空
    $indiaimagetype = getimagesize($this->indiaimage);
    $logow = $indiaimagetype[0]; //得到水印图片的宽
    $logoh = $indiaimagetype[1]; //得到水印图片的高
    switch ($indiaimagetype[2]) { //判断水印图片的格式
     case 1 :
      $indiaimagetype = "gif";
      $logo = imagecreatefromgif($this->indiaimage);
      break;
     case 2 :
      $indiaimagetype = "jpg";
      $logo = imagecreatefromjpeg($this->indiaimage);
      break;
     case 3 :
      $indiaimagetype = "png";
      $logo = imagecreatefrompng($this->indiaimage);
      break;
    }
    imagealphablending($im, true); //打开混色模式
    imagecopy($im, $logo, $this->indiaimagex, $this->indiaimagey, 0, 0, $logow, $logoh);
    imagedestroy($im);
    imagedestroy($logo);
   }
  }
  if ($this->indiapath == "") { //如果水印存放地址不为空
   if ($filetype == "jpg") {
    imagejpeg($im, $filename);
   } else
    if ($filetype == "gif") {
     imagegif($im, $filename);
    } else
     if ($filetype == "png") {
      imagepng($im, $filename);
     }
   if($reimage == true){
    $this->goreimagesize($filename,$filetype);
   }else{
    return true; //添加水印成功
   }
  } else {
   if (!file_exists($this->indiapath)) {
    mkdir($this->indiapath);
    return false; //请重新上传
   } else {
    $indianame = basename($filename);
    $indianame = $this->indiapath . $indianame;
    if ($filetype == "jpg") {
     imagejpeg($im, $indianame);
    } else
     if ($filetype == "gif") {
      imagegif($im, $indianame);
     } else
      if ($filetype == "png") {
       imagepng($im, $indianame);
      }
    if($reimage == true){
     $this->goreimagesize($indianame,$filetype);
     echo $indianame;
    }else{
     return true; //添加水印成功
    }
   }
  }
 }
 function goreimagesize($filename, $filetype) {
  if (!file_exists($filename)) {
   return false; //要生成缩略图的图片不存在
  } else {
   if ($filetype == 'jpg') {
    $reimage = imagecreatefromjpeg($filename);
   }
   elseif ($filetype == 'png') {
    $reimage = imagecreatefrompng($filename);
   } else
    if ($filetype == 'gif') {
     $reimage = imagecreatefromgif($filename);
    }
   if (isset ($reimage)) {
    $srcimagetype = getimagesize($filename);
    $srcimagetypew = $srcimagetype[0]; //得到原始图片宽度
    $srcimagetypeh = $srcimagetype[1]; //得到原始图片高度
    $reim = imagecreatetruecolor($this->reimagesize[1], $this->reimagesize[2]);
    imagecopyresized($reim, $reimage, 0, 0, 0, 0, $this->reimagesize[1], $this->reimagesize[2], $srcimagetypew, $srcimagetypeh);
    $reimagepath = $this->reimagesize[3];
    if ($reimagepath != "") { //如果存放水印地址不为空
     if (!file_exists($reimagepath)) {
      mkdir($reimagepath);
     } else {
      $reimagename = basename($filename);
      $reimagename = $reimagepath . "r_" . $reimagename;
      if ($filetype == "gif")
       imagegif($reim, $reimagename);
      else
       if ($filetype == "jpg")
        imagejpeg($reim, $reimagename);
       else
        if ($filetype == "png")
         imagepng($reim, $reimagename);
      return true;
     }
    } else {
     $filename = basename($filename);
     if($this->indiapath == ""){
      $filename = $this->filepath."r_" . $filename;
     }else{
      $filename = $this->indiapath."r_" . $filename;
     }
     if ($filetype == "gif")
      imagegif($reim, $filename);
     else
      if ($filetype == "jpg")
       imagejpeg($reim, $filename);
      else
       if ($filetype == "png")
        imagepng($reim, $filename);
     return true;
    }

   }
  }
 }

}
if ($_post["submit"]) {
 $file = $_files['uploadfile'];
 $upfile = new upfile();
 echo $upfile->uploadfile($file);
}
?>

  <form action="" method="post" enctype="multipart/form-data">
  <input type="file" name="uploadfile"/><br/>
  <input type="submit" value="上传" name="submit"/>
  </form>

给图片加水印是很多网站会做的事情,下面这段代码只支持给图片加上文字水印哦,现在暂时只支持英文的,如果要让此程序能支持中文汉字就把$font=\\\'verdana.ttf\\\';调用一个中文字体就OK了。
 代码如下 复制代码

$image=$_get[id];
$im=getimagesize($image);
//print_r($im);
//以下是读取文字的宽高
$font='verdana.ttf';//字体
$font_size = $im[0]/20;//文字大小
$size=imagettfbbox($font_size,0,$font,'111cn.net');//文字宽高
$textwidth = $size [4];//取出宽
$textheight = abs( $size [7] );//取出高

$pic_w=abs(($im[0]-$textwidth)/2);//文字存放宽的位置
$pic_h=abs(($im[1]-$textheight)/2);//文字存放高的位置

 switch($im[2])               
{
case 1:
$im=imagecreatefromgif($image);
break;

case 2:
$im=imagecreatefromjpeg($image);
break;

case 3:
$im=imagecreatefrompng($image);
break;
}
//$logo="2.png";//商标图片
//$ing=getimagesize($logo);
//
// switch($ing[2])               
//
//{
//case 1:
//$in=imagecreatefromgif($logo);
//break;
//
//case 2:
//$in=imagecreatefromjpeg($logo);
//break;
//
//case 3:
//$in=imagecreatefrompng($logo);
//break;
//}
//imagecopy($im,$in,200,3,0,0,'120','110');//图片合并
 $te = imagecolorclosestalpha($im,255,255,255,60);//最后一个数值是透明度越大越透明
$str=iconv("gbk","utf-8","111cn.net"); //设置文字的内容和编码,很简单吧

imagettftext($im,$font_size,0,$pic_w,$pic_h,$te,'verdana.ttf',$str);

header("content-type: image/jpeg");
imagejpeg($im);


?>

[!--infotagslink--]

相关文章

  • php上传图片学习笔记与心得

    我们在php中上传文件就必须使用#_FILE变量了,这个自动全局变量 $_FILES 从 PHP 4.1.0 版本开始被支持。在这之前,从 4.0.0 版本开始,PHP 支持 $HTTP_POST_FILES 数组。这...2016-11-25
  • php图片添加文字水印实现代码

    这篇文章主要为大家详细介绍了php图片添加文字水印实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-03-17
  • php+jquery Ajax异步上传图片(ajaxSubmit)实例

    下面我们一起来看一个php+jquery Ajax异步上传图片(ajaxSubmit)实例,这个我们真正的利用了ajax而不是使用iframe之类的哦。 效果如下 ...2016-11-25
  • PHP:实现给上传图片加水印的程序代码

    用PHP给上传图片加水印的程序是通过判定文件类型建立图形,然后把其复制到原建立的图形上,填充并建立rectangle,以备写入imagestring()或是原已经定好的图像程序当中判定水...2016-11-25
  • vue实现上传图片添加水印

    这篇文章主要为大家详细介绍了vue实现上传图片添加水印,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-13
  • PHP上传图片时判断上传文件是否为可用图片的方法

    这篇文章主要介绍了PHP上传图片时判断上传文件是否为可用图片的方法,涉及php针对图片的后缀检测操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2016-11-01
  • 【帝国CMS插件】帝国CMS7.0图集批量上传插件 批量上传图片

    帝国CMS的图集上传一直是很蛋疼的事情。 猪先飞网以前发布过一款 图集批量上传插件 ,但可惜只支持6.6版。不支持7.0版。 而帝国CMS7.0版自2013年03月份发布以来,一直没有人放...2015-12-30
  • php检测上传图片的长度和宽度

    /* array getimagesize ( string $filename [, array &$imageinfo ] ) getimagesize()函数将确定任何给定的图像大小的文件,并返回随着文件类型和高度/宽度的文本字符串...2016-11-25
  • C#实现上传照片到物理路径,并且将地址保存到数据库的小例子

    这篇文章主要介绍了c#上传图片,并将地址保存到数据库中的简单实例,有需要的朋友可以参考一下...2021-09-22
  • php给图片加文字水印与图片水印代码(1/2)

    这款程序给图片加文字水印时是调用 了C:\\WINDOWS\\Fonts\\\\SIMHEI.TTF字体,给图片加水印时就可以自定图片哦。 $image->wprint_img();//执行图片水印 $image->...2016-11-25
  • 帝国7.0 多值字段修改为 可以上传图片的形式

    我们知道多值字段功能很强大,但不能上传图片确很操蛋,其实改吧改吧就可以了,只是帝国的大大们似乎不太注意这些小细节,只有靠自己来优化了。<script> function domvadd_ffff() {...2015-12-30
  • php ckeditor上传图片文件大小限制修改

    ckeditor编辑器在上传图片或文件时是没有大小限制的,下面我们来给大家介绍两种ckeditor上传图片文件大小限制问题解决办法。 一种可以通过修改PHP.INI配置文件上传...2016-11-25
  • php+ajax实现带进度条的上传图片功能【附demo源码下载】

    这篇文章主要介绍了php+ajax实现带进度条的上传图片功能,涉及php文件传输及ajax无刷新提交的相关操作技巧,并附带demo源码供读者下载参考,需要的朋友可以参考下...2016-10-02
  • PHP中FCK上传图片文件名乱码

    使用fck的朋友可能会碰这样一个情况就是如果上你的文件名为英文字母是没有任何问题,如果上传的是中文汉字就会出现中文名乱码了,下面我来给大家分析与介绍解决方法。...2016-11-25
  • thinkphp3.2实现上传图片的控制器方法

    这篇文章主要介绍了thinkphp3.2实现上传图片的控制器方法,结合实例形式分析了thinkPHP图片文件上传相关的文件类型判断,文件路径及相关属性操作技巧,需要的朋友可以参考下...2016-05-04
  • php ckeditor上传图片文件名乱码解决方法

    文件名乱码一般是中文导致的,因为ckeditor使用的是uft8编码如果我们页面使用的是gbk或gb2312就有可能出现乱码问题,解决办法只要对上传文件重命名即可。 打开editor...2016-11-25
  • CKeditor增加上传图片功能方法

    CKeditor可以配合CKfinder实现文件的上传及管理。但是往往我们上传的图片需要某些自定义的操作,比如将图片路径写入数据库,图片加水印等等操作。 实现原理:配置CKeditor...2016-09-20
  • .net c# gif动画如何添加图片水印实现思路及代码

    本文将详细介绍下c#实现gif动画添加图片水印,思路很清晰,感兴趣的你可以参考下哈,希望可以帮助到你...2021-09-22
  • Vue组件封装上传图片和视频的示例代码

    这篇文章主要介绍了Vue封装上传图片和视频的组件,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-07-31
  • Unity实现图片水印生成

    这篇文章主要为大家详细介绍了Unity实现图片水印生成,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25