首页 > 语言 > PHP > 正文

php给图片加文字水印与图片水印代码

2024-09-04 11:44:23
字体:大 中 小
来源:转载
供稿:网友

这款程序给图片加文字水印时是调用 了C://WINDOWS//Fonts////SIMHEI.TTF字体,给图片加水印时就可以自定图片,实例代码如下:

  1. $image->wprint_img();//执行图片水印 
  2. $image->wprint_string();//执行文字水印 
  3. class editimage{ 
  4.  private $imagefile;//图片文件 
  5.  private $smallimg;//水印图片 
  6.  private $string;//水印文字 
  7.  private $position;//存放位置 
  8.  private $dst_x=600;//原始图片打水印x坐标 
  9.  private $dst_y=0;//原始图片打水印y坐标 
  10.  private $str_x=450; 
  11.  private $str_y=200; 
  12.  private $font="c:windows ontssimhei.ttf";//原始图片打水印字体路径 
  13.  private $imgej;// imagecolorallocate后的变量 
  14.   
  15.  function __get($value){ 
  16.   return $this->$value; 
  17.  } 
  18.  function __set($property,$value){ 
  19.   $this->$property=$value; 
  20.  } 
  21.  /** 
  22.   * 构造函数初始化 
  23.   * 
  24.   * @param string $imagefile 被上水印的文件 
  25.   * @param string $smallimg 水印文件 
  26.   * @param string $string 水印文字 
  27.   * @param string $position 存放位置 
  28.   * @param int $dst_x  被上水印的图片x 
  29.   * @param int $dst_y  被上水印的图片y 
  30.   */ 
  31.  function __construct($imagefile,$smallimg='',$string=''){//,$position='',$dst_x=0,$dst_y=0 
  32.   $this->imagefile=$imagefile; 
  33.   $this->smallimg=$smallimg; 
  34.   $this->string=$string; 
  35.   $this->imgej=$this->imagecreatef($this->imagefile); 
  36.  } 
  37.  
  38.  function get_extname($file){//获取文件的后缀名 
  39.   if (file_exists($this->imagefile)) { 
  40.    $img=getimagesize($file); 
  41.    switch ($img[2]){ 
  42.     case "1": 
  43.      return "gif"; 
  44.     case "2": 
  45.      return "jpg"; 
  46.     case "3": 
  47.      return "png"; 
  48.    } 
  49.   }else{ 
  50.    return false; 
  51.   }  
  52.  }   
  53.  
  54.   
  55. function getsize($file,$wh){//获取图大小. $wh:w获得宽,h获得高 
  56.   $image=getimagesize($file); 
  57.   if ($wh) { 
  58.    switch ($wh){ 
  59.     case "w": 
  60.      return $image[0]; 
  61.     case "h": 
  62.      return $image[1]; 
  63.    } 
  64.   }else{ 
  65.    return false; 
  66.   } 
  67.  } 
  68.  function imagecreatef($file){//创建类型 
  69.   if ($this->get_extname($file)) { 
  70.    switch($this->get_extname($file)){ 
  71.     case "gif": 
  72.      return imagecreatefromgif($file); 
  73.     case "jpg": 
  74.      return imagecreatefromjpeg($file); 
  75.     case "png": 
  76.      return imagecreatefrompng($file); 
  77.    } 
  78.   }else{ 
  79.    echo "文件不存在"; 
  80.   } 
  81.  }  
  82.   
  83.  //水印图片处理 
  84.  function wprint_img(){ 
  85.   if($this->smallimg){ 
  86.    imagecopy($this->imgej,$this->imagecreatef($this->smallimg),$this->dst_x,$this->dst_y,0,0,$this->getsize($this->smallimg,"w"),$this->getsize($this->smallimg,"h")); 
  87.    }else{ 
  88.    return "水印图片不存在!"; 
  89.   } 
  90.  } 
  91.  //水印文字处理 
  92.  function wprint_string(){ 
  93.   return imagettftext($this->imgej,20,0,$this->str_x,$this->str_y,imagecolorallocate($this->imgej,200,200,200),$this->font,iconv("gb2312","utf-8",$this->string)); 
  94.  } 
  95.   
  96.  function choose_imgouttype(){//输出  
  97.    if($this->position){ 
  98.     $this->get_extname($this->imagefile); 
  99.     switch ($this->get_extname($this->imagefile)){ 
  100.     case "gif": 
  101.      return imagegif($this->imgej,$position); 
  102.     case "jpg": 
  103.      return imagejpeg($this->imgej,$this->position); 
  104.     case "jpeg": 
  105.      return imagejpeg($this->imgej,$this->position); 
  106.     case "png": 
  107.      return imagepng($this->imgej,$position); 
  108.     } 
  109.    }else{ 
  110.      
  111.     switch ($this->get_extname($this->imagefile)){ 
  112.     case "gif": 
  113.      return imagegif($this->imgej); 
  114.     case "jpg": 
  115.      return imagejpeg($this->imgej); 
  116.     case "jpeg": 
  117.      return imagejpeg($this->imgej); 
  118.     case "png": 
  119.      return imagepng($this->imgej); 
  120.     } 
  121.    } 
  122.    
  123.  } 
  124. } 
  125.  
  126. //使用方法如下: 
  127.  
  128. $image=new editimage("d90.gif","hknmtt.png","我的d90"); 
  129. $image->wprint_img();//执行图片水印 
  130. $image->wprint_string();//执行文字水印,开源代码Vevb.com 
  131. $image->choose_imgouttype(); 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表