图片加水印的php类

TextWatermarkTool.class.php

  1. <?
  2. /**
  3.   * 为图片加水印 . 
  4.   * 本类只能给图片添加文字水印.
  5.   * 本类的开发环境为php5,gd2库.如果低于php5和gd2库的.可能导致本程序无法执行.
  6.   * 另外,为了使用 exif_imagetype 函数来判断图片类型,请打开php.ini中的 extension=php_mbstring.dll 和 extension=php_exif.dll
  7.   * 并保证, extension=php_mbstring.dll 在 extension=php_exif.dll 之前.....不知道为什么..
  8.   * @author  enna@163.com
  9.   * @version  1.0
  10.   */
  11. class TextWatermarkTool {
  12.   //在图片上面加文字时,采用的字体的路径(只能使用truetype字体。后缀名为TTF的字体)
  13.   var $f;
  14.   //字体大小
  15.   var $fontSize = 12;
  16.   //文字的角度
  17.   var $textAngle = -45;
  18.   
  19.   //需要加文字的图片的宽度和高度。根据读取出来的图象自动获取
  20.   var $imgWidth = 1;
  21.   var $imgHeight = 1;
  22.   //当从imgUrl读取图象失败的时候,默认创建图象的长度和宽度
  23.   var $DefImgWidth = 250;
  24.   var $DefImgHeight = 150;
  25.   
  26.   //输出的图象的长度和宽度
  27.   var $outWidth = 0;
  28.   var $outHeight = 0;
  29.   //输出图象的质量,0 - 100
  30.   var $outImgQuality = 75;
  31.   //需要处理的图象的地址
  32.   var $imgUrl = "";
  33.   //需要往上述图象上添加的文字
  34.   var $text = "";
  35.   //上述文字的颜色
  36.   var $textColor = "0,0,0";
  37.   //文字的底色:注如果使用底色的话。
  38.   var $textBarColor = "255,255,255";
  39.   //标识是否为文字绘制背景栏
  40.   var $paintBar = true;
  41.   //文字背景栏的高度
  42.   var $barHeight = 1;
  43.   //文字背景栏的宽度,默认为图片宽度
  44.   var $barWidth = 1;//$imgWidth;
  45.   //设置文字背景栏的起始x坐标
  46.   var $barX = 0;
  47.   //设置文字背景的起始Y坐标
  48.   var $barY = 0;
  49.   //设置文字背景栏是否渐变
  50.   var $barGradual = true;
  51.   //背景栏的透明度
  52.   var $barAlpla = 75;
  53.   //上述文字的透明度,0为不透明,127为完全透明
  54.   var $alpha = 75;
  55.   //上述文字的x轴的位置
  56.   var $textX = 10;
  57.   //上述文字y轴位置
  58.   var $textY = 10;
  59.   //当加载由$fontName指定的字体文件失败时,使用的默认字体
  60.   var $defFont = 5;
  61.   //需要处理的图片文件的格式
  62.   var $imgFormat = "jpeg";
  63.   //输出的图片格式
  64.   var $outFormat = "jpeg";
  65.   //当把图片保存到磁盘时的文件路径
  66.   //var $imgSavePath = ".";
  67.   //当把图片保存到磁盘时,所使用的文件名
  68.   var $imgSaveName = "Nonam";//.$outFormat;
  69.   
  70.   //根据$imgUrl 读取出来的image数据文件,类型为php的resource类
  71.   var $image = NULL;
  72.   //输出类型:文件:file或者浏览器:browser
  73.   var $outType = "browser";
  74.   var $JPEG = "jpeg";
  75.   var $JPG = "jpg";
  76.   var $PNG = "png";
  77.   var $GIF = "gif";
  78.   var $BMP = "bmp";
  79.   var $WBMP = "wbmp";
  80.   var $XBM = "xbm";
  81.   var $XPM = "xpm";
  82.   var $OUTTOFILE = "file";
  83.   var $OUTTOBROWSER = "borwser";
  84.   
  85.   /**
  86.    * 设置水印文字所采用的字体路径
  87.    * @param  $path  一个ttf字体所在的路径(包含字体名字)
  88.    * 
  89.    */
  90.   function setFontName ($path) {
  91.    $this->fontName = $path;
  92.   }
  93.   /**
  94.    * 获取水印文字的字体
  95.    * @return String - 字体路径,包含字体文件名
  96.    *
  97.    */
  98.   function getFontName () {
  99.    return $this->fontName;
  100.   }
  101.   /**
  102.    * 设置输出文件的类型,分 file 和 browser 两种
  103.    * 当设置为file时,文件会保存到本地磁盘,设置为browser时,会直接输出到浏览器
  104.    * @param  $o 输出的类型.只有两种值:file和browser
  105.    *
  106.    */
  107.   function setOutType ($o) {
  108.    if (strtolower($o) !== $this->OUTTOFILE && strtolower($o) != $this->OUTTOBROWSER)
  109.     $o = $this->OUTTOBROWSER;
  110.    $this->outType = $o;
  111.   }
  112.   /**
  113.    * 获取输出文件类型
  114.    * @return  String - 输出图片文件的类型.为file和browser之一
  115.    *
  116.    */
  117.   function getOutType () {
  118.    return $this->outType;
  119.   }
  120.   /**
  121.    * 设置文字底色栏的透明度
  122.    * @param $alpha  int , 文字底色栏的透明度.取值为 0 - 127
  123.    *  0表示完全不透明,127表示完全透明
  124.    *
  125.    */
  126.   function setBarAlpha ($alpha) {
  127.    if ($alpha < 0 || $alpha > 127)
  128.     $alpha = 75;
  129.    $this->barAlpha = $alpha;
  130.   }
  131.   /**
  132.    * 获取文字底色栏的透明度
  133.    * @return  int - 文字底色栏的透明度
  134.    */
  135.   function getBarAlpha () {
  136.    return $this->barAlpha;
  137.   }
  138.   
  139.   /**
  140.    * 设置输出图片文件的质量. 只有当输出图片格式为jpg的时候有效.
  141.    * @param  $i , int 输出的图片文件的质量.取值为 0 - 100
  142.    *  数字越大,质量越高
  143.    *
  144.    */
  145.   function setOutImgQuality ($i) {
  146.    if ($i < 0 || $i > 100)
  147.     $i = 75;
  148.    $this->outImgQuality = $i;
  149.   }
  150.   /**
  151.    * 获取输出图片的质量
  152.    * @return  int 图片的质量
  153.    *
  154.    */
  155.   function getOutImgQuality () {
  156.    return $this->outImgQuality;
  157.   }
  158.   /**
  159.    * 设置文字的倾斜角度
  160.    * @param  $a  , int 文字的旋转角度
  161.    *
  162.    */
  163.   function setTextAngle ($a) {
  164.    $this->textAngle = $a;
  165.   }
  166.   /**
  167.    * 获取文本的旋转角度
  168.    * @return  int , 文本的旋转角度
  169.    *
  170.    */
  171.   function getTextAngle () {
  172.    return $this->textAngle;
  173.   }
  174.   /**
  175.    * 设置文本所采用的字体大小
  176.    * @param  $size - int , 文本所采用的字体大小
  177.    *
  178.    */  
  179.   function setFontSize ($size) {
  180.    if ($size <= 0 )
  181.     $size = 12;
  182.    $this->fontSize = $size;
  183.   }
  184.   /*
  185.    * 获取文本所采用的字体大小
  186.    * @return  int  , 文本所采用的字体大小
  187.    */
  188.   function getFontSize () {
  189.    return $this->fontSize;
  190.   }
  191.   /**
  192.    * 设置输出图片的宽度
  193.    * @param  $w , int 输出图片的宽度
  194.    *
  195.    */
  196.   function setOutWidth ($w){
  197.    if ($w < 0)
  198.     $w = $this->getImgWidth();
  199.    $this->outWidth = $w;
  200.   }
  201.   /**
  202.    * 获取输出图片的宽度
  203.    * @return  输出图片的宽度
  204.    *
  205.    */
  206.   function getOutWidth () {
  207.    return $this->outWidth;
  208.   }
  209.   
  210.   /**
  211.    * 设置输出图片的高度
  212.    * @param  $h , int 输出图片的高度
  213.    *
  214.    */
  215.   function setOutHeight ($h) {
  216.    if ($h <= 0 )
  217.     $h = $this->getImgHeight();
  218.    $this->outHeight = $h;
  219.   }
  220.   /**
  221.    * 获取输出图片的高度
  222.    * @return  输出图片的宽度
  223.    *
  224.    */
  225.   function getOutHeight () {
  226.    return $this->outHeight;
  227.   }
  228.   
  229.   /**
  230.    * 设置要加水印的图片的地址.可以为硬盘图片文件的地址,也可以为网络图片的网址
  231.    * @param  $url  String , 图片的地址.
  232.    *
  233.    */
  234.   function setImgUrl ($url) {
  235.    $this->imgUrl = $url;
  236.   }
  237.   /**
  238.    * 获取图片的地址
  239.    * @return  String 图片的地址
  240.    *
  241.    */
  242.   function getImgUrl () {
  243.    return $this->imgUrl;
  244.   }
  245.   /**
  246.    * 设置要加到图片上的水印文字内容
  247.    * @param  $t String  要加到图片上的水印文字内容
  248.    *
  249.    */
  250.   function setText ($t) {
  251.    $this->text = $t;
  252.   }
  253.   /**
  254.    * 获取要加到图片上的水印文字内容
  255.    * @return  String  要加到图片上的水印文字内容
  256.    *
  257.    */
  258.   function getText () {
  259.    return $this->text;
  260.   }
  261.   function getImgWidth () {
  262.    return $this->imgWidth;
  263.   }
  264.   function getImgHeight () {
  265.    return $this->imgHeigh;
  266.   }
  267.   
  268.   /**
  269.    * 设置文字底色栏的起始的x坐标
  270.    * @param  $t int 
  271.    *
  272.    */
  273.   function setBarX ($t) {
  274.    //echo "<BR>barX: *** ".$this->getImgWidth()."-".$this->getBarWidth()."=".($this->getImgWidth() - $this->getBarWidth());
  275.    if ($t <= 0 || $t >= $this->getImgWidth() - $this->getBarWidth())
  276.     $t = 0;
  277.    $this->barX = $t;
  278.    //echo $this->barX;
  279.   }
  280.   /**
  281.    * 获取文字底色栏的起始X坐标
  282.    * @return  int - 文字底色栏的起始X坐标
  283.    */
  284.   function getBarX () {
  285.    return $this->barX;
  286.   }
  287.   /**
  288.    * 设置文字底色栏的起始Y坐标
  289.    * @param  $t int 
  290.    *
  291.    */
  292.   function setBarY ($t) {
  293.    //echo "<BR>barY: *** ".($this->getImgHeight() - $this->getBarHeight());
  294.    if ($t <= 0 || $t >= $this->getImgHeight() - $this->getBarHeight())
  295.     $t = 0;
  296.    $this->barY = $t;
  297.    //echo $this->barY;
  298.   }
  299.   /**
  300.    * 获取文字底色栏的起始Y坐标
  301.    * @return  int - 文字底色栏的起始Y坐标
  302.    */
  303.   function getBarY () {
  304.    return $this->barY;
  305.   }
  306.   
  307.   /**
  308.    * 设置文字底色栏的颜色
  309.    * @param  $c String 
  310.    *
  311.    */
  312.   function setTextBarColor ($c) {
  313.    $this->textBarColor = $c;
  314.   }
  315.   /**
  316.    * 获取文字底色栏的颜色
  317.    * @return  String - 文字底色栏的颜色
  318.    */
  319.   function getTextBarColor () {
  320.    return $this->textBarColor;
  321.   }
  322.   /**
  323.    * 设置是否需要绘制文字底色栏
  324.    * @param  $boolean   boolean true:需要绘制底色栏,反之 false
  325.    *
  326.    */
  327.   function setPaintBar ($boolean) {
  328.    $this->paintBar = $boolean;
  329.   }
  330.   /**
  331.    * 获取是否需要绘制文字底色栏
  332.    * @return  boolean  true:需要绘制底色栏,反之 false
  333.    */
  334.   function getPaintBar () {
  335.    return $this->paintBar;
  336.   }
  337.   /**
  338.    * 设置文字底色栏的高度
  339.    * @param  $h int 
  340.    *
  341.    */
  342.   function setBarHeight ($h) {
  343.    if ($h < 0 )
  344.     $h = 1;
  345.    $this->barHeight = $h;
  346.   }
  347.   /**
  348.    * 获取文字底色栏的高度
  349.    * @return  int - 文字底色栏的高度
  350.    */
  351.   function getBarHeight () {
  352.    return $this->barHeight ;
  353.   }
  354.   /**
  355.    * 设置文字底色栏的宽度
  356.    * @param  $width    int 
  357.    *
  358.    */
  359.   function setBarWidth ($width){
  360.    if ($width < 0)
  361.     $width = $this->getImgWidth();
  362.    $this->barWidth = $width;
  363.   }
  364.   /**
  365.    * 获取文字底色栏的宽度
  366.    * @return  int - 文字底色栏的宽度
  367.    */
  368.   function getBarWidth () {
  369.    return $this->barWidth;
  370.   }
  371.   
  372.   /**
  373.    * 设置文字底色栏是否采用渐变.true:采用渐变,反之,false
  374.    * @param  $boolean  boolean true:采用渐变,反之,false
  375.    *
  376.    */
  377.   function setBarGradual  ($boolean){
  378.    $this->barGradual = $boolean;
  379.   }
  380.   /**
  381.    * 获取文字底色栏是否采用渐变
  382.    * @return  boolean - true:采用渐变,反之,false
  383.    */
  384.   function getBarGradual () {
  385.    return $this->barGradual;
  386.   }
  387.   /**
  388.    * 设置文字颜色字符串.采用rgb格式的10进制.rgb用,隔开,比如 255,255,0
  389.    * @param  $c String 
  390.    *
  391.    */
  392.   function setTextColor ($c) {
  393.    $this->textColor = $c;
  394.   }
  395.   /**
  396.    * 获取文字颜色
  397.    * @return  string - 文字颜色字符串.采用rgb格式的10进制.rgb用,隔开,比如 255,255,0
  398.    */
  399.   function getTextColor () {
  400.    return $this->textColor;
  401.   }
  402.   /**
  403.    * 设置文字文字的透明度
  404.    * @param  $a  int 
  405.    *
  406.    */
  407.   function setAlpha ($a) {
  408.    if ($a > 127 || $a < 0){
  409.     $a = 75;
  410.    }
  411.    $this->alpha = $a;
  412.   }
  413.   /**
  414.    * 获取文字的透明度
  415.    * @return  int - 文字的透明度
  416.    */
  417.   function getAlpha () {
  418.    return $this->alpha;
  419.   }
  420.   /**
  421.    * 设置文字底色栏的起始的x坐标
  422.    * @param  $x int 
  423.    *
  424.    */
  425.   function setTextX ($x) {
  426.    $this->textX = $x;
  427.   }
  428.   /**
  429.    * 获取水印文字的起始X坐标
  430.    * @return  int - 水印文字的起始X坐标
  431.    */
  432.   function getTextX () {
  433.    return $this->textX;
  434.   }
  435.   /**
  436.    * 设置水印文字的起始Y坐标
  437.    * @param  $y int 
  438.    *
  439.    */
  440.   function setTextY ($y) {
  441.    $this->textY = $y;
  442.   }
  443.   /**
  444.    * 获取水印文字的起始Y坐标
  445.    * @return  int - 水印文字的起始Y坐标
  446.    */
  447.   function getTextY () {
  448.    return $this->textY;
  449.   }
  450.   
  451.   /**
  452.    * 获取要被加水印的图片的格式
  453.    * 注:这里可以修改一下,采用php库的图象函数exif_imagetype,判断图片的格式,而不是根据输入文件的后缀名来判断
  454.    * @return  int - 要被加水印的图片的格式
  455.    */
  456.   function getImgFormat () {
  457.    //return $this->imgFormat;
  458.    if (function_exists('exif_imagetype')) {
  459.     $tpn = exif_imagetype($this->getImgUrl());
  460.     switch ($tpn) {
  461.      case 1 :
  462.       $tp = "gif";
  463.       break;
  464.      case 2 :
  465.       $tp = "jpeg";
  466.       break;
  467.      case 3 :
  468.       $tp = "png";
  469.       break;
  470.      case 6 :
  471.       $tp = "bmp";
  472.       break;
  473.      case 15 :
  474.       $tp = "wbmp";
  475.       break;
  476.      case 16 :
  477.       $tp = "xbm";
  478.       break;
  479.     }//end switch
  480.     return $tp;
  481.    }else{
  482.     return $this->getImageType($this->getImgUrl());
  483.    }
  484.   }
  485.   /**
  486.    * 获取图片类型
  487.    * 这个函数是当php.ini中没有打开php_exif.dll扩展时调用的
  488.    * 如果已经打开了这个扩展,可以使用php自带的exif_imagetype函数达到相同效果
  489.    * @param  $imgUrl  String   图片地址
  490.    * @return  String  图片类型,如jpg,gif等
  491.    */
  492.    function getImageType ($imgUrl) {
  493.    //echo ":::::::::::::<BR>";
  494.    $imghandle = fopen($imgUrl,"r");
  495.    $byte1 = fread($imghandle ,1);
  496.    $hex = base_convert(ord($byte1),10,16);
  497.    fclose($imghandle);
  498.    $res = "";
  499.    switch ($hex) {
  500.     case 47:
  501.      $res = "gif";
  502.      break;
  503.     case ff:
  504.      $res = "jpg";
  505.      break;
  506.     case 42:
  507.      $res = "bmp";
  508.      break;
  509.     case 89:
  510.      $res = "png";
  511.      break;
  512.     case 00:
  513.      $res = "wbmp";
  514.      break;
  515.    }//end switch
  516.    //echo "RES = ".$res." , HEX=".$hex."<BR>";
  517.    return $res;
  518.    }
  519.   /**
  520.    * 设置输出图片的类型.如jpg,gif等
  521.    * @param  $o string
  522.    *
  523.    */
  524.   function setOutFormat ($o) {
  525.    $this->outFormat = $o;
  526.   }
  527.   /**
  528.    * 获取输出图片的类型.如jpg,gif等
  529.    * @return  string - 输出图片的类型.如jpg,gif等
  530.    */
  531.   function getOutFormat () {
  532.    return $this->outFormat;
  533.   }
  534.                 
  535.   /**
  536.    * 设置输出图片的名称
  537.    * @param  $n String 
  538.    *
  539.    */
  540.   function setImgSaveName ($n) {
  541.    $this->imgSaveName = $n;//.$this->getOutFormat();
  542.   }
  543.   /**
  544.    * 获取输出图片的名称.
  545.    * @return  int - 输出图片的名称
  546.    */
  547.   function getImgSaveName () {
  548.    return $this->imgSaveName;
  549.   }
  550.   
  551.   /**
  552.    * 构造函数
  553.    * @param  $backImgUrl 要被加水印的图片地址
  554.    * @param  $text  水印文字内容
  555.    *
  556.    */
  557.   function TextWatermarkTool ($backImgUrl ,  $text ) {
  558.    $this->imgUrl = $backImgUrl ;
  559.    $this->text = $text;
  560.    $this->init();
  561.   }
  562.   /**
  563.    * 初始化 . 主要做以下工作:
  564.    * 1.根据设置的图片格式,采用不同的方法读取图片数据,保存到该类实例的image对象中,以便被后面的方法调用.
  565.    * 2.初始化一些宽度和高度值
  566.    *
  567.    */
  568.   function init () {
  569.    //echo getType($this->image)."<BR>";
  570.    //echo $this->JPEG."<BR>";
  571.    if (strtolower($this->getImgFormat()) == strtolower($this->JPEG) || 
  572.     strtolower($this->getImgFormat()) == strtolower($this->JPG)){
  573.     $this->image = imagecreatefromjpeg ($this->getImgUrl());
  574.    }else if (strtolower($this->getImgFormat()) == strtolower($this->GIF)){
  575.     $this->image = imagecreatefromgif ($this->getImgUrl());
  576.    }else if (strtolower($this->getImgFormat()) == strtolower($this->PNG)){
  577.     $this->image = imagecreatefrompng ($this->getImgUrl());
  578.    }else if (strtolower($this->getImgFormat()) == strtolower($this->WBMP)){
  579.     $this->image = imagecreatefromwbmp ($this->getImgUrl());
  580.    }else if (strtolower($this->getImgFormat()) == strtolower($this->XBM)){
  581.     $this->image = imagecreatefromxbm ($this->getImgUrl());
  582.    }else {
  583.     $this->image = imagecreatetruecolor (250,150);
  584.    }
  585.    //echo getType($this->image)."<BR>";
  586.    if ($this->image !== NULL){
  587.     ImageAlphaBlending($this->image, true);
  588.     $this->imgWidth = imagesx($this->image);
  589.     $this->imgHeight = imagesy($this->image);
  590.     //$this->setBarWidth($this->imgWidth);
  591.     $this->setOutWidth($this->imgWidth);
  592.     $this->setOutHeight($this->imgHeight);
  593.     //echo $w." , ".$h."<BR>";
  594.    }
  595.   }
  596.   /**
  597.    * 绘制文字底栏
  598.    *
  599.    */
  600.   function paintBar () {
  601.    $barImg = NULL;
  602.    if ($this->getPaintBar()){
  603.     $colors = explode(",",$this->getTextBarColor());
  604.     $r = $colors[0]?$colors[0]:0;
  605.     $g = $colors[1]?$colors[1]:0;
  606.     $b = $colors[2]?$colors[2]:0;
  607.     
  608.     $_alpha = 0;
  609.     $x1 = $this->getBarX();
  610.     $y1 = $this->getBarY();
  611.     if ($this->getBarGradual()){
  612.      if ($this->getBarWidth() <= 0)
  613.       $this->setBarWidth(1);
  614.      $brw = $this->getBarWidth();
  615.      //echo "brw=".$brw."<BR>";
  616.      $_deltan = 127/$brw;
  617.      $y2 = $this->getBarHeight();
  618.      for ($i = 0 ; $i < $brw ; $i++) {
  619.       $_alpha += $_deltan;
  620.       $color = imagecolorallocatealpha($this->image,$r,$g,$b,$_alpha);
  621.       $x2 = $x1+1;
  622.       imagefilledrectangle ($this->image, $x1$y1$x2$y2$color );
  623.       $x1++;
  624.      }//end for
  625.     }else{
  626.      //echo "BarAlpha = ".$this->getBarAlpha();
  627.      $color = imagecolorallocatealpha($this->image,$r,$g,$b,$this->getBarAlpha());
  628.      $x2 = $x1+$this->getBarWidth();
  629.      $y2 = $y1+$this->getBarHeight();
  630.      imagefilledrectangle ($this->image, $x1$y1$x2$y2$color );
  631.     }//end if else
  632.    }//end if 
  633.    
  634.    return $this->image;
  635.   }//end function paintBar
  636.   /**
  637.    * 绘制文字
  638.    *
  639.    */
  640.   function paintText () {
  641.    
  642.    $colors = explode(",",$this->getTextColor());
  643.    $r = $colors[0]?$colors[0]:0;
  644.    $g = $colors[1]?$colors[1]:0;
  645.    $b = $colors[2]?$colors[2]:0;
  646.    if ($this->getFontName() == "" || !file_exists($this->getFontName()))
  647.     $font = $this->defFont;
  648.    else
  649.     $font = $this->getFontName();
  650.    //如果是中文,得先转换成utf-8.当然,如果是中文,字体就必须设置成能正常显示中文的字体了.
  651.    $txt = $this->getText();
  652.    $txt = iconv('GB2312','UTF-8',$txt);
  653.    //echo "TextAlpha:".$this->getAlpha();
  654.    $textcolor = imagecolorallocatealpha($this->image,$r,$g,$b,$this->getAlpha());
  655.    //注:imagettftext 的  x  所表示的坐标定义了第一个字符的基本点(大概是字符的左下角)。这和 imagestring() 不同,其 x,y 定义了第一个字符的左上角。例如 "top left" 为 0, 0。 所以这里的Y需要加上字体的大小,另外再加一个偏移量
  656.    imagettftext ($this->image, $this->getFontSize(),$this->getTextAngle(), $this->getTextX(), $this->getTextY()+$this->getFontSize()+5,$textcolor,$font$txt);
  657.    
  658.    //imagestring($this->image, 5, $this->getTextX(), $this->getTextY(), "Hello world!", $textcolor);
  659.    /*
  660.    $txt = $this->getText();
  661.    $textcolor = imagecolorallocatealpha($this->image,255,255,255,90);
  662.    //注:imagettftext 的  x  所表示的坐标定义了第一个字符的基本点(大概是字符的左下角)。这和 imagestring() 不同,其 x,y 定义了第一个字符的左上角。例如 "top left" 为 0, 0。 所以这里的Y需要加上字体的大小,另外再加一个偏移量
  663.    imagettftext ($this->image, 20,35, 80, 325,$textcolor,"d://windows//fonts//verdana.ttf", $txt);
  664.    */
  665.   }//end func
  666.   
  667.   /**
  668.    * 输出图象
  669.    *
  670.    */
  671.   function output () {
  672.    //echo "(***) ".$this->getOutType();
  673.    if ($this->getOutType() == $this->OUTTOFILE){
  674.     //echo "tofile";
  675.     switch (strtolower($this->getOutFormat())) {
  676.      case $this->JPEG:
  677.       ImageJPEG($this->image,$this->getImgSaveName(),$this->getOutImgQuality());
  678.       break;
  679.      case $this->GIF:
  680.       ImageGIF($this->image,$this->getImgSaveName());
  681.       break;
  682.      case $this->PNG:
  683.       ImagePNG($this->image,$this->getImgSaveName());
  684.       break;
  685.      case $this->WBMP:
  686.       Imagewbmp($this->image,$this->getImgSaveName());
  687.       break;
  688.      default:
  689.       ImageJPEG($this->image,$this->getImgSaveName(),$this->getOutImgQuality());
  690.       break;
  691.     }//end switch
  692.    }else{
  693.     //echo "to browser";
  694.     $header = "Content-type: image/";
  695.     switch (strtolower($this->getOutFormat())) {
  696.      case $this->JPEG:
  697.       $header .= $this->JPEG;
  698.       break;
  699.      case $this->GIF:
  700.       $header .= $this->GIF;
  701.       break;
  702.      case $this->PNG:
  703.       $header .= $this->PNG;
  704.       break;
  705.      case $this->WBMP:
  706.       $header .= $this->WBMP;
  707.       break;
  708.      default:
  709.       $header .= $this->JPEG;
  710.       break;
  711.     }//end switch
  712.     //echo "*********".$header;
  713.     header ($header);
  714.     ImageJPEG($this->image);
  715.    }
  716.    ImageDestroy($this->image);
  717.    
  718.   }//end func
  719.   /**
  720.    * 主要方法.供别的类调用的.
  721.    *
  722.    */
  723.   function makeWatermark () {
  724.    $this->paintBar();
  725.    $this->paintText();
  726.    $this->output();
  727.   }
  728. }//end class
  729. //for test
  730. $cls = new TextWatermarkTool("c://img//mozilla.jpg","www.xgame8.com");
  731. //$cls->setFontName("d://windows//fonts//simhei.ttf");
  732. $cls->setOutType("browser");
  733. //$cls->setText($cls->getOutType()."   ".$cls->getImgFormat()." /n ".$cls->getImgUrl());
  734. //$cls->setImgFormat("gif");
  735. $cls->setAlpha(60);
  736. $cls->setTextAngle(-45);
  737. $cls->setTextX(8);
  738. $cls->setTextY(8);
  739. $cls->setBarGradual(true);
  740. $cls->setPaintBar(false);
  741. $cls->setBarX(8);
  742. $cls->setBarY(8);
  743. $cls->setBarAlpha(75);
  744. $cls->setBarHeight(25);
  745. $cls->setTextBarColor("255,255,255");
  746. $cls->setBarWidth(120);
  747. $cls->setFontSize(20);
  748. $cls->setTextColor("255,255,255");
  749. $cls->setFontName("d://windows//fonts//verdana.ttf");
  750. $cls->setOutFormat("png");
  751. $cls->setOutImgQuality("100");
  752. $cls->setImgSaveName("c://img//mozillaw.png");
  753. $cls->makeWatermark();
  754. //end for test
  755. ?>

调用的例子:

  1. <?
  2.         require_once("TextWatermarkTool.class.php");
  3.         //echo "HELLO";
  4.         $cls = new TextWatermarkTool("http://127.0.0.1/phpq/templates/xgame8/images/top.jpg","极限吧 -- www.xgame8.com");
  5.         $cls->setFontName("d://windows//fonts//simhei.ttf");
  6.         $cls->setOutType("file");
  7.         $cls->setAlpha(80);
  8.         $cls->setTextX(0);
  9.         $cls->setTextY(0);
  10.         $cls->setBarGradual(false);
  11.         $cls->setPaintBar(true);
  12.         $cls->setTextBarColor("161,191,247");
  13.         $cls->setBarWidth(120);
  14.         $cls->setFontSize(20);
  15.         $cls->setTextColor("255,255,255");
  16.         $cls->setOutFormat("png");
  17.         $cls->setBarAlpha(100);
  18.         $cls->setImgSaveName("c://byclass.jpg");
  19.         $cls->makeWatermark();
  20. ?>

来源地址:http://bbs.phpchina.com/thread-16964-1-1.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值