PHP添加中文水印

示例代码1:

<?php
Header("Content-type: image/png");      /*通知浏览器,要输出图像*/
$im       = imagecreate(400 , 300);        /*定义图像的大小*/
$gray     = ImageColorAllocate($im , 235 , 235 , 235);
$pink     = ImageColorAllocate($im, 255 , 128 , 255);

$fontfile = "simkai.ttf";

/* $fontfile 字体的路径,视操作系统而定,可以是 simhei.ttf(黑体) , SIMKAI.TTF(楷体) , SIMFANG.TTF(仿宋) ,SIMSUN.TTC(宋体&新宋体) 等 GD 支持的中文字体*/
$str   = iconv('GB2312','UTF-8','中文水印');     /*将 gb2312 的字符集转换成 UTF-8 的字符*/
ImageTTFText($im, 30, 0, 100, 200, $pink , $fontfile , $str);
/* 加入中文水印 */
Imagepng($im);
ImageDestroy($im);
?>

 

 

示例代码2:

<?
header("Content-type: image/jpeg");
$image = "1.jpg"; //原图
$fnt = "c:/windows/fonts/stcaiyun.ttf"; // 彩云
$str = "谨防盗版 谨防盗版";
//$str = iconv("GB2312","UTF-8",$str);
$size = 40;
$angle = 20;
$ar = imagettfbbox($size, $angle, $fnt, $str);
$width = $ar[2];
$height = max($size,$ar[1]-$ar[5]);
$im = imagecreatefromjpeg($image);
//$white = imagecolorallocate($im, 255,255,255);
$color = imagecolorallocatealpha($im, 255,255,255,1);
for($i=0;$i<5;$i++) {
//$color = imagecolorclosestalpha($im,255,255,255,90-$i*10);
imagettftext($im, $size, $angle, 40-$ar[6], $height-$ar[1]+100*$i, $color, $fnt, $str);
}
imagejpeg($im);
imagedestroy($im);
?>

 

 

示例代码3:

<?php
class imagelogo{
var $input_image_file = "";        //输入图片的文件名
var $output_image_file = "";       //生成文件的名称
var $logo_image_name = "";         //包含存放路径的水印图片的文件名
var $logo_text = "";               //水印文字
var $logo_text_size = 18;          //水印文字大小
var $logo_text_angle = 4;          //水印文字角度
var $logo_text_pos = 6;            //水印文字放置位置
var $logo_text_font = "";          //水印文字的字体
var $logo_text_color = "#ffffff"; //水印字体的颜色值
var $logo_image_pos = 6;           //水印图片放置的位置
   //1 = 顶部居左,2 = 顶部居中,3 = 顶部居右,4 = 底部居左
   //5 = 底部居中,6 = 底部居右,7 = 中间居左,8 = 居中,9 = 中间居右
var $logo_image_transition = 25;   //水印图片与原图片的融合度(1=100)
var $jpeg_quality = 75;            //jpeg图片的质量
//生成水印图片
function create($filename=""){
   if ($filename) $this->input_image_file = strtolower(trim($filename));
   $src_image_type = $this->get_type($this->input_image_file);
   $src_image = $this->createImage($src_image_type,$this->input_image_file);
   if (!$src_image) return;
   $src_image_w=imagesx($src_image);
   $src_image_h=imagesy($src_image);
   //开始处理水印logo图片信息,把两个图片合成为一个图片
   if ($this->logo_image_name){
    $this->logo_image_name = strtolower(trim($this->logo_image_name));
    $logo_image_type = $this->get_type($this->logo_image_name);
    $logo_image = $this->createImage($logo_image_type,$this->logo_image_name);
    $logo_image_w=imagesx($logo_image);
    $logo_image_h=imagesy($logo_image);
    $temp_logo_image = $this->getPos($src_image_w,$src_image_h,$this->logo_image_pos,$logo_image);
    $logo_image_x = $temp_logo_image["dest_x"];
    $logo_image_y = $temp_logo_image["dest_y"];
    imagecopymerge($src_image, $logo_image,$logo_image_x,$logo_image_y,0,0,$logo_image_w,$logo_image_h,$this->logo_image_transition);
   }
   //水印为纯文本
   if ($this->logo_text){
    $this->logo_text = $this->gb2utf8($this->logo_text);
    $temp_logo_text = $this->getPos($src_image_w,$src_image_h,$this->logo_text_pos);
    $logo_text_x = $temp_logo_text["dest_x"];
    $logo_text_y = $temp_logo_text["dest_y"];
    if(preg_match("/([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i", $this->logo_text_color, $color))
    {
     $red = hexdec($color[1]);
     $green = hexdec($color[2]);
     $blue = hexdec($color[3]);
     $logo_text_color = imagecolorallocate($src_image, $red,$green,$blue);
    }else{
     $logo_text_color = imagecolorallocate($src_image, 255,255,255);
    }
    //用TrueType字体向图像写入文本
    imagettftext($src_image,$this->logo_text_size,$this->logo_angle,$logo_text_x,$logo_text_y,$logo_text_color,$this->logo_text_font,$this->logo_text);
   }
   //保存生成的图片为新的文件
   if ($this->output_image_file){
    switch ($this->get_type($this->output_image_file)){
     case 'gif':$src_img=imagegif($src_image, $this->output_image_file); break;
     case 'jpeg':$src_img=imagejpeg($src_image, $this->output_image_file, $this->jpeg_quality); break;
     case 'png':$src_img=imagepng($src_image, $this->output_image_file); break;
     default:$src_img=imagejpeg($src_image, $this->output_image_file, $this->jpeg_quality); break;
    }
   }
   else
   { //在原来图片的基础上生成新的合成图片
    if ($src_image_type = "jpg") $src_image_type="jpeg";
    header("Content-type: image/{$src_image_type}");
    switch ($src_image_type){
     case 'gif':$src_img=imagepng($src_image); break;
     case 'jpg':$src_img=imagejpeg($src_image, "", $this->jpeg_quality);break;
     case 'png':$src_img=imagepng($src_image);break;
     default:$src_img=imagejpeg($src_image, "", $this->jpeg_quality);break;
    }
   }
   imagedestroy($src_image);
}
//根据文件名和类型创建图片
function createImage($type,$img_name){
   if (!$type){
    $type = $this->get_type($img_name);
   }
   switch ($type){
    case 'gif':
     if (function_exists('imagecreatefromgif'))
    $tmp_img=@imagecreatefromgif($img_name);
     break;
    case 'jpg':
     $tmp_img=imagecreatefromjpeg($img_name);
     break;
    case 'png':
     $tmp_img=imagecreatefrompng($img_name);
     break;
    default:
     $tmp_img=imagecreatefromstring($img_name);
     break;
   }
   return $tmp_img;
}
//根据源图像的长、宽,位置代码,水印图片id来生成把水印放置到源图像中的位置
function getPos($sourcefile_width,$sourcefile_height,$pos,$logo_image=""){
   if ($logo_image){
    $insertfile_width = imagesx($logo_image);
    $insertfile_height = imagesy($logo_image);
   }else {
    $lineCount = explode("\r\n",$this->logo_text);
    $fontSize = imagettfbbox($this->logo_text_size,$this->logo_text_angle,$this->logo_text_font,$this->logo_text);
    $insertfile_width = $fontSize[2] - $fontSize[0];
    $insertfile_height = count($lineCount)*($fontSize[1] - $fontSize[3]);
   }
   switch ($pos){
    case 1://顶部居左
     $dest_x = 0;
     if ($this->logo_text){
      $dest_y = $insertfile_height;
     }else{
      $dest_y = 0;
     }
     break;
    case 2://顶部居中
     $dest_x = ( ( $sourcefile_width - $insertfile_width ) / 2 );
     if ($this->logo_text){
      $dest_y = $insertfile_height;
     }else{
      $dest_y = 0;
     }
     break;
    case 3://顶部居右
     $dest_x = $sourcefile_width - $insertfile_width;
     if ($this->logo_text){
      $dest_y = $insertfile_height;
     }else{
      $dest_y = 0;
     }
     break;
    case 4://底部居左
     $dest_x = 0;
     $dest_y = $sourcefile_height - $insertfile_height;
     break;
    case 5://底部居中
     $dest_x = ( ( $sourcefile_width - $insertfile_width ) / 2 );
     $dest_y = $sourcefile_height - $insertfile_height;
     break;
    case 6://底部居右
     $dest_x = $sourcefile_width - $insertfile_width;
     $dest_y = $sourcefile_height - $insertfile_height;
     break;
    case 7://中间居左
     $dest_x = 0;
     $dest_y = ( $sourcefile_height / 2 ) - ( $insertfile_height / 2 );
     break;
    case 8://居中
     $dest_x = ( $sourcefile_width / 2 ) - ( $insertfile_width / 2 );
     $dest_y = ( $sourcefile_height / 2 ) - ( $insertfile_height / 2 );
     break;
    case 9://中间居右
     $dest_x = $sourcefile_width - $insertfile_width;
     $dest_y = ( $sourcefile_height / 2 ) - ( $insertfile_height / 2 );
     break;
    default://底部居右
     $dest_x = $sourcefile_width - $insertfile_width;
     $dest_y = $sourcefile_height - $insertfile_height;
     break;
   }
   return array("dest_x"=>$dest_x,"dest_y"=>$dest_y);
}
//获取图片的格式,主要包括jpg,png和gif
function get_type($img_name){
   $name_array = explode(".",$img_name);
   if (preg_match("/\.(gif|jpg|jpeg|png)$/", $img_name, $matches)){
    $type = strtolower($matches[1]);
   }else{
    $type = "string";
   }
   return $type;
}
//指定的文字转换为UTF-8格式,使用gb2312保证中文的正常显示
function gb2utf8($gb){
   if(!trim($gb))
    return $gb;
   $filename="./gb2312.txt";
   $tmp=file($filename);
   $codetable=array();
   while(list($key,$value)=each($tmp))
    $codetable[hexdec(substr($value,0,6))]=substr($value,7,6);
   $utf8="";
   while($gb){
    if (ord(substr($gb,0,1))>127){
     $tthis=substr($gb,0,2);
     $gb=substr($gb,2,strlen($gb)-2);
     $utf8.=$this->u2utf8(hexdec($codetable[hexdec(bin2hex($tthis))-0x8080]));
    }else{
     $tthis=substr($gb,0,1);
     $gb=substr($gb,1,strlen($gb)-1);
     $utf8.=$this->u2utf8($tthis);
    }
   }
   return $utf8;
}
//转换为UTF8编码
function u2utf8($c){
   $str="";
   if ($c < 0x80){
    $str.=$c;
   }else if ($c < 0x800){
    $str.=chr(0xC0 | $c>>6);
    $str.=chr(0x80 | $c & 0x3F);
   }else if ($c < 0x10000){
    $str.=chr(0xE0 | $c>>12);
    $str.=chr(0x80 | $c>>6 & 0x3F);
    $str.=chr(0x80 | $c & 0x3F);
   }else if ($c < 0x200000){
    $str.=chr(0xF0 | $c>>18);
    $str.=chr(0x80 | $c>>12 & 0x3F);
    $str.=chr(0x80 | $c>>6 & 0x3F);
    $str.=chr(0x80 | $c & 0x3F);
   }
   return $str;
}
}
?>

<?php
include_once("imagelogo.class.php");
$imglogo = new imagelogo();
$imglogo->logo_text = "大家好,phpbook";
$imglogo->logo_text_font = "./SIMLI.TTF";、//字体
$imglogo->create("./scene.jpg");
?>

<?php
include_once("imagelogo.class.php");
$imglogo = new imagelogo();
$imglogo->logo_text = "大家好,phpbook";
$imglogo->logo_text_font = "./SIMLI.TTF";
$imglogo->output_image_file ="./sd.jpg";
$imglogo->create("./scene.jpg");
?>

 

 示例代码4:

<?php 
/*-------------------------------------------------------------
**描述:这是用于给指定图片加底部水印(不占用图片显示区域)的自定义类,需创建对象调用
**创建:2007-10-09
**更新:2007-10-09
**说明:1、需要gd库支持,需要iconv支持(php5已经包含不用加载)
        2、只适合三种类型的图片,jpg/jpeg/gif/png,其它类型不处理
        3、注意图片所在目录的属性必须可写
        4、调用范例:
            $objImg = new MyWaterDownChinese();
            $objImg->Path = "images/";
            $objImg->FileName = "1.jpg";
            $objImg->Text = "HAHAKONGJIAN[url]HTTP://HI.BAIDU.COM/LYSONCN[/url]";
            $objImg->Font = "./font/simhei.ttf";
            $objImg->Run();
**成员函数:
----------------------------------------------------------------*/
class MyWaterDownChinese{
          var $Path = "./"; //图片所在目录相对于调用此类的页面的相对路径
          var $FileName = ""; //图片的名字,如“1.jpg”
          var $Text = "";   //图片要加上的水印文字,支持中文
          var $TextColor = "#ffffff"; //文字的颜色,gif图片时,字体颜色只能为黑色
          var $TextBgColor = "#000000"; //文字的背景条的颜色
          var $Font = "c://windows//fonts//simhei.ttf"; //字体的存放目录,相对路径
          var $OverFlag = true; //是否要覆盖原图,默认为覆盖,不覆盖时,自动在原图文件名后+"_water_down",如“1.jpg”=> "1_water_down.jpg"
          var $BaseWidth = 200; //图片的宽度至少要>=200,才会加上水印文字。
         
//------------------------------------------------------------------
//功能:类的构造函数(php5.0以上的形式)
//参数:无
//返回:无
function __construct(){;}

//------------------------------------------------------------------
//功能:类的析构函数(php5.0以上的形式)
//参数:无
//返回:无
function __destruct(){;}
//------------------------------------------------------------------

//------------------------------------
//功能:对象运行函数,给图片加上水印
//参数:无
//返回:无
function Run()
{
    if($this->FileName == "" || $this->Text == "")
        return;
    //检测是否安装GD库
    if(false == function_exists("gd_info"))
    {
        echo "系统没有安装GD库,不能给图片加水印.";
        return;
    }
    //设置输入、输出图片路径名
    $arr_in_name = explode(".",$this->FileName);
    //
    $inImg = $this->Path.$this->FileName;
    $outImg = $inImg;
    $tmpImg = $this->Path.$arr_in_name[0]."_tmp.".$arr_in_name[1]; //临时处理的图片,很重要
    if(!$this->OverFlag)
        $outImg = $this->Path.$arr_in_name[0]."_water_down.".$arr_in_name[1];
    //检测图片是否存在
    if(!file_exists($inImg))
        return ;
    //获得图片的属性
    $groundImageType = @getimagesize($inImg); 
    $imgWidth = $groundImageType[0]; 
    $imgHeight = $groundImageType[1];
    $imgType = $groundImageType[2];
    if($imgWidth < $this->BaseWidth) //小于基本宽度,不处理
        return;
    
    //图片不是jpg/jpeg/gif/png时,不处理
    switch($imgType)
    {
         case 1: 
              $image = imagecreatefromgif($inImg); 
              $this->TextBgColor = "#ffffff"; //gif图片字体只能为黑,所以背景颜色就设置为白色
              break; 
         case 2: 
              $image = imagecreatefromjpeg($inImg);
              break; 
         case 3: 
              $image = imagecreatefrompng($inImg);
              break; 
         default: 
              return;
              break;
    }
    //创建颜色
    $color = @imagecolorallocate($image,hexdec(substr($this->TextColor,1,2)),hexdec(substr($this->TextColor,3,2)),hexdec(substr($this->TextColor,5,2))); //文字颜色
    //生成一个空的图片,它的高度在底部增加水印高度
    $newHeight = $imgHeight+20;
    $objTmpImg = @imagecreatetruecolor($imgWidth,$newHeight);
    $colorBg = @imagecolorallocate($objTmpImg,hexdec(substr($this->TextBgColor,1,2)),hexdec(substr($this->TextBgColor,3,2)),hexdec(substr($this->TextBgColor,5,2))); //背景颜色    
    //填充图片的背景颜色
    @imagefill ($objTmpImg,0,0,$colorBg);
    //把原图copy到临时图片中
    @imagecopy($objTmpImg,$image,0,0,0,0,$imgWidth,$imgHeight);
    //创建要写入的水印文字对象
    $objText = $this->createText($this->Text);
    //计算要写入的水印文字的位置
    $x = 5;
    $y = $newHeight-5;
    //写入文字水印
    @imagettftext($objTmpImg,10,0,$x,$y,$color,$this->Font,$objText);    
    //生成新的图片,临时图片
    switch($imgType)
    {
         case 1: 
              imagegif($objTmpImg,$tmpImg);
              break; 
         case 2: 
              imagejpeg($objTmpImg,$tmpImg);
              break; 
         case 3: 
              imagepng($objTmpImg,$tmpImg);
              break; 
         default: 
              return;
              break;
    }    
    //释放资源
    @imagedestroy($objTmpImg);
    @imagedestroy($image);
    //重新命名文件
    if($this->OverFlag)
    {
        //覆盖原图
        @unlink($inImg);
        @rename($tmpImg,$outImg);
    }
    else
    {
        //不覆盖原图
        @rename($tmpImg,$outImg);
    }
}

//--------------------------------------
//功能:创建水印文字对象
//参数:无
//返回:创建的水印文字对象
function createText($instring)
{
   $outstring="";
   $max=strlen($instring);
   for($i=0;$i<$max;$i++)
   {
       $h=ord($instring[$i]);
       if($h>=160 && $i<$max-1)
       {
           $outstring .= "&#".base_convert(bin2hex(iconv("gb2312","ucs-2",substr($instring,$i,2))),16,10).";";
           $i++;
       }
       else
       {
           $outstring .= $instring[$i];
       }
   }
   return $outstring;
}

}//class
?>

 

 

示例代码5:

<?php
/*
图片处理函数功能:缩放、剪切、相框、水印、锐化、旋转、翻转、透明度、反色
处理并保存历史记录的思路:当有图片有改动时自动生成一张新图片,命名方式可以考虑在原图片的基础上加上步骤,例如:图片名称+__第几步
*/
class picture
{
    var $PICTURE_URL;//要处理的图片
    var $DEST_URL="temp__01.jpg";//生成目标图片位置
    var $PICTURE_CREATE;//要创建的图片
    var $TURE_COLOR;//新建一个真彩图象
 
    var $PICTURE_WIDTH;//原图片宽度
    var $PICTURE_HEIGHT;//原图片高度
 
  /*
  水印的类型,默认的为水印文字
  */
    var $MARK_TYPE=1;
    var $WORD;//经过UTF-8后的文字
    var $WORD_X;//文字横坐标
    var $WORD_Y;//文字纵坐标
    var $FONT_TYPE;//字体类型
    var $FONT_SIZE="12";//字体大小
    var $FONT_WORD;//文字
    var $ANGLE=0;//文字的角度,默认为0
    var $FONT_COLOR="#000000";//文字颜色
    var $FONT_PATH="font/simkai.ttf";//字体库,默认为宋体
    var $FORCE_URL;//水印图片
    var $FORCE_X=0;//水印横坐标
    var $FORCE_Y=0;//水印纵坐标
    var $FORCE_START_X=0;//切起水印的图片横坐标
    var $FORCE_START_Y=0;//切起水印的图片纵坐标
 
    var $PICTURE_TYPE;//图片类型
    var $PICTURE_MIME;//输出的头部
 
  /*
  缩放比例为1的话就按缩放高度和宽度缩放
  */
    var $ZOOM=1;//缩放类型
    var $ZOOM_MULTIPLE;//缩放比例
    var $ZOOM_WIDTH;//缩放宽度
    var $ZOOM_HEIGHT;//缩放高度
 
  /*
  裁切,按比例和固定长度、宽度
  */
    var $CUT_TYPE=1;//裁切类型
    var $CUT_X=0;//裁切的横坐标
    var $CUT_Y=0;//裁切的纵坐标
    var $CUT_;//裁切的宽度
    var $CUT_HEIGHT=100;//裁切的高度
 
  /*
  锐化
  */
    var $SHARP="7.0";//锐化程度
 
  /*
  透明度处理
  */
    var $ALPHA='100';//透明度在0-127之间
    var $ALPHA_X="90";
    var $ALPHA_Y="50";
 
  /*
  任意角度旋转
  */
    var $CIRCUMROTATE="90.0";//注意,必须为浮点数
 
  /*
  出错信息
  */
    var $ERROR=array(
        'unalviable'=>'没有找到相关图片!'
    );
 
  /*
  构造函数:函数初始化
  */
    function __construct($PICTURE_URL)
    {
        $this->get_info($PICTURE_URL);
    }
     
    function get_info($PICTURE_URL)
    {
        /*
        处理原图片的信息,先检测图片是否存在,不存在则给出相应的信息
        */
        @$SIZE=getimagesize($PICTURE_URL);
        if(!$SIZE)
        {
            exit($this->ERROR['unalviable']);
        }
 
        //得到原图片的信息类型、宽度、高度
        $this->PICTURE_MIME=$SIZE['mime'];
        $this->PICTURE_;
        $this->PICTURE_HEIGHT=$SIZE[1];
 
        //创建图片
        switch($SIZE[2])
        {
            case 1:
                $this->PICTURE_CREATE=imagecreatefromgif($PICTURE_URL);
                $this->PICTURE_TYPE="imagejpeg";
                $this->PICTURE_EXT="jpg";
                break;
            case 2:
                $this->PICTURE_CREATE=imagecreatefromjpeg($PICTURE_URL);
                $this->PICTURE_TYPE="imagegif";
                $this->PICTURE_EXT="gif";
                break;
            case 3:
                $this->PICTURE_CREATE=imagecreatefrompng($PICTURE_URL);
                $this->PICTURE_TYPE="imagepng";
                $this->PICTURE_EXT="png";
                break;
        }  
 
        /*
        文字颜色转换16进制转换成10进制
        */
        preg_match_all("/([0-f]){2,2}/i",$this->FONT_COLOR,$MATCHES);
        if(count($MATCHES)==3)
        {
            $this->RED=hexdec($MATCHES[0][0]);
            $this->GREEN=hexdec($MATCHES[0][1]);
            $this->BLUE=hexdec($MATCHES[0][2]);
        }
    }
 
    #end of __construct
 
    /*
    将16进制的颜色转换成10进制的(R,G,B)
    */
    function hex2dec()
    {
        preg_match_all("/([0-f]){2,2}/i",$this->FONT_COLOR,$MATCHES);
        if(count($MATCHES)==3)
        {
            $this->RED=hexdec($MATCHES[0][0]);
            $this->GREEN=hexdec($MATCHES[0][1]);
            $this->BLUE=hexdec($MATCHES[0][2]);
        }
    }
 
    //缩放类型
    function zoom_type($ZOOM_TYPE)
    {
        $this->ZOOM=$ZOOM_TYPE;
    }
 
    //对图片进行缩放,如果不指定高度和宽度就进行缩放
    function zoom()
    {
        //缩放的大小
        if($this->ZOOM==0)
        {
            $this->ZOOM_;gt;PICTURE_WIDTH * $this->ZOOM_MULTIPLE;
            $this->ZOOM_HEIGHT=$this->PICTURE_HEIGHT * $this->ZOOM_MULTIPLE;
        }
        //新建一个真彩图象
        $this->TRUE_COLOR=imagecreatetruecolor($this->ZOOM_WIDTH,$this->ZOOM_HEIGHT);
        $WHITE=imagecolorallocate($this->TRUE_COLOR,255,255,255);
        imagefilledrectangle($this->TRUE_COLOR,0,0,$this->ZOOM_WIDTH,$this->ZOOM_HEIGHT,$WHITE);
        imagecopyresized($this->TRUE_COLOR,$this->PICTURE_CREATE,0,0,0,0,$this->ZOOM_WIDTH,$this->ZOOM_HEIGHT,$this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);
    }
 
    #end of zoom
    //裁切图片,按坐标或自动
    function cut()
    {
        $this->TRUE_COLOR=imagecreatetruecolor($this->CUT_WIDTH,$this->CUT_WIDTH);
        imagecopy($this->TRUE_COLOR,$this->PICTURE_CREATE, 0, 0, $this->CUT_X, $this->CUT_Y,$this->CUT_WIDTH,$this->CUT_HEIGHT);
    }
     #end of cut

    /*
    在图片上放文字或图片
    水印文字
    */
    function _mark_text()
    {
        $this->TRUE_COLOR=imagecreatetruecolor($this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);
        $this->WORD=mb_convert_encoding($this->FONT_WORD,'utf-8','gb2312');
        /*
        取得使用 TrueType 字体的文本的范围
        */
        $TEMP = imagettfbbox($this->FONT_SIZE,0,$this->FONT_PATH,$this->WORD);
        $WORD_LENGTH=strlen($this->WORD);
        $WORD_WIDTH =$TEMP[2] - $TEMP[6];
        $WORD_HEIGHT =$TEMP[3] - $TEMP[7];
        /*
        文字水印的默认位置为右下角
        */
        if($this->WORD_X=="")
        {
            $this->WORD_X=$this->PICTURE_WIDTH-$WORD_WIDTH;
        }
        if($this->WORD_Y=="")
        {
            $this->WORD_Y=$this->PICTURE_HEIGHT-$WORD_HEIGHT;
        }
        imagesettile($this->TRUE_COLOR,$this->PICTURE_CREATE);
        imagefilledrectangle($this->TRUE_COLOR,0,0,$this->PICTURE_WIDTH,$this->PICTURE_HEIGHT,IMG_COLOR_TILED);
        $TEXT2=imagecolorallocate($this->TRUE_COLOR,$this->RED,$this->GREEN,$this->Blue);
        imagettftext($this->TRUE_COLOR,$this->FONT_SIZE,$this->ANGLE,$this->WORD_X,$this->WORD_Y,$TEXT2,$this->FONT_PATH,$this->WORD);
    }
 
    /*
    水印图片
    */
  function _mark_picture()
  {
    /*
    获取水印图片的信息
    */
    @$SIZE=getimagesize($this->FORCE_URL);
    if(!$SIZE)
    {
      exit($this->ERROR['unalviable']);
    }
    $FORCE_PICTURE_;
    $FORCE_PICTURE_HEIGHT=$SIZE[1];
    //创建水印图片
    switch($SIZE[2])
    {
      case 1:
        $FORCE_PICTURE_CREATE=imagecreatefromgif($this->FORCE_URL);
        $FORCE_PICTURE_TYPE="gif";
        break;
      case 2:
        $FORCE_PICTURE_CREATE=imagecreatefromjpeg($this->FORCE_URL);
        $FORCE_PICTURE_TYPE="jpg";
        break;
      case 3:
        $FORCE_PICTURE_CREATE=imagecreatefrompng($this->FORCE_URL);
        $FORCE_PICTURE_TYPE="png";
        break;
    }
    /*
      判断水印图片的大小,并生成目标图片的大小,如果水印比图片大,则生成图片大小为水印图片的大小。否则生成的图片大小为原图片大小。
    */
    $this->NEW_PICTURE=$this->PICTURE_CREATE;
    if($FORCE_PICTURE_WIDTH>$this->PICTURE_WIDTH)
    {
      $CREATE_;gt;FORCE_START_X;
    }
    else
    {
      $CREATE_;gt;PICTURE_WIDTH;
    }
     
    if($FORCE_PICTURE_HEIGHT>$this->PICTURE_HEIGHT)
    {
      $CREATE_HEIGHT=$FORCE_PICTURE_HEIGHT-$this->FORCE_START_Y;
    }
    else
    {
      $CREATE_HEIGHT=$this->PICTURE_HEIGHT;
    }
    /*
    创建一个画布
    */
    $NEW_PICTURE_CREATE=imagecreatetruecolor($CREATE_WIDTH,$CREATE_HEIGHT);
    $WHITE=imagecolorallocate($NEW_PICTURE_CREATE,255,255,255);
    /*
    将背景图拷贝到画布中
    */
    imagecopy($NEW_PICTURE_CREATE, $this->PICTURE_CREATE, 0, 0, 0, 0,$this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);
   
    /*
    将目标图片拷贝到背景图片上
    */
    imagecopy($NEW_PICTURE_CREATE, $FORCE_PICTURE_CREATE, $this->FORCE_X, $this->FORCE_Y, $this->FORCE_START_X, $this->FORCE_START_Y,$FORCE_PICTURE_WIDTH,$FORCE_PICTURE_HEIGHT);
    $this->TRUE_COLOR=$NEW_PICTURE_CREATE;
  }
  #end of mark
 
  function alpha_()
  {
    $this->TRUE_COLOR=imagecreatetruecolor($this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);
    $rgb="#CDCDCD";
    $tran_color="#000000";
    for($j=0;$j<=$this->PICTURE_HEIGHT-1;$j++)
    {
      for ($i=0;$i<=$this->PICTURE_WIDTH-1;$i++)
      {
        $rgb = imagecolorat($this->PICTURE_CREATE,$i,$j);
        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;
        $now_color=imagecolorallocate($this->PICTURE_CREATE,$r,$g,$b);
        if ($now_color==$tran_color)
        {
          continue;
        }
        else
        {
          $color=imagecolorallocatealpha($this->PICTURE_CREATE,$r,$g,$b,$ALPHA);
          imagesetpixel($this->PICTURE_CREATE,$ALPHA_X+$i,$ALPHA_Y+$j,$color);
        }
        $this->TRUE_COLOR=$this->PICTURE_CREATE;
      }
    }
  }

  /*
  图片旋转:
  沿y轴旋转
  */
  function turn_y()
  {
    $this->TRUE_COLOR=imagecreatetruecolor($this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);
    for ($x = 0; $x < $this->PICTURE_WIDTH; $x++)
    {
      imagecopy($this->TRUE_COLOR, $this->PICTURE_CREATE, $this->PICTURE_WIDTH - $x - 1, 0, $x, 0, 1, $this->PICTURE_HEIGHT);
    }
  }
  /*
  沿X轴旋转
  */
  function turn_x()
  {
    $this->TRUE_COLOR=imagecreatetruecolor($this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);
    for ($y = 0; $y < $this->PICTURE_HEIGHT; $y++)
    {
      imagecopy($this->TRUE_COLOR, $this->PICTURE_CREATE, 0, $this->PICTURE_HEIGHT - $y - 1, 0, $y, $this->PICTURE_WIDTH, 1);
    }
  }
   
   
  /*
  任意角度旋转
  */
  function turn()
  {
    $this->TRUE_COLOR=imagecreatetruecolor($this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);
    imageCopyResized($this->TRUE_COLOR,$this->PICTURE_CREATE,0,0,0,0,$this->PICTURE_WIDTH,$this->PICTURE_HEIGHT,$this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);
    $WHITE=imagecolorallocate($this->TRUE_COLOR,255,255,255);
    $this->TRUE_COLOR=imagerotate ($this->TRUE_COLOR, $this->CIRCUMROTATE, $WHITE);
  }

  /*
  图片锐化
  */
  function sharp()
  {
    $this->TRUE_COLOR=imagecreatetruecolor($this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);
    $cnt=0;
    for ($x=0; $x<$this->PICTURE_WIDTH; $x++)
    {
      for ($y=0; $y<$this->PICTURE_HEIGHT; $y++)
      {
        $src_clr1 = imagecolorsforindex($this->TRUE_COLOR, imagecolorat($this->PICTURE_CREATE, $x-1, $y-1));
        $src_clr2 = imagecolorsforindex($this->TRUE_COLOR, imagecolorat($this->PICTURE_CREATE, $x, $y));
        $r = intval($src_clr2["red"]+$this->SHARP*($src_clr2["red"]-$src_clr1["red"]));
        $g = intval($src_clr2["green"]+$this->SHARP*($src_clr2["green"]-$src_clr1["green"]));
        $b = intval($src_clr2["blue"]+$this->SHARP*($src_clr2["blue"]-$src_clr1["blue"]));
        $r = min(255, max($r, 0));
        $g = min(255, max($g, 0));
        $b = min(255, max($b, 0));
        if (($DST_CLR=imagecolorexact($this->PICTURE_CREATE, $r, $g, $b))==-1)
          $DST_CLR = imagecolorallocate($this->PICTURE_CREATE, $r, $g, $b);
          $cnt++;
        if ($DST_CLR==-1) die("color allocate faile at $x, $y ($cnt).");
          imagesetpixel($this->TRUE_COLOR, $x, $y, $DST_CLR);
      }
    }
  }
   
  /*
     将图片反色处理??
  */
  function return_color()
  {
    /*
    创建一个画布
    */
    $NEW_PICTURE_CREATE=imagecreate($this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);
    $WHITE=imagecolorallocate($NEW_PICTURE_CREATE,255,255,255);
    /*
    将背景图拷贝到画布中
    */
    imagecopy($NEW_PICTURE_CREATE, $this->PICTURE_CREATE, 0, 0, 0, 0,$this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);
    $this->TRUE_COLOR=$NEW_PICTURE_CREATE;
  }
   
  /*
  生成目标图片并显示
  */
  function show()
  {
    // 判断浏览器,若是IE就不发送头
    if(isset($_SERVER['HTTP_USER_AGENT']))
    {
      $ua = strtoupper($_SERVER['HTTP_USER_AGENT']);
      if(!preg_match('/^.*MSIE.*\)$/i',$ua))
      {
        header("Content-type:$this->PICTURE_MIME");
      }
    }
    $OUT=$this->PICTURE_TYPE;
    $OUT($this->TRUE_COLOR);
  }
 
  /*
  生成目标图片并保存
  */
  function save_picture()
  {
    // 以 JPEG 格式将图像输出到浏览器或文件
    $OUT=$this->PICTURE_TYPE;
    if(function_exists($OUT))
    {
      // 判断浏览器,若是IE就不发送头
      if(isset($_SERVER['HTTP_USER_AGENT']))
      {
        $ua = strtoupper($_SERVER['HTTP_USER_AGENT']);
        if(!preg_match('/^.*MSIE.*\)$/i',$ua))
        {
          header("Content-type:$this->PICTURE_MIME");
        }
      }
      if(!$this->TRUE_COLOR)
      {
        exit($this->ERROR['unavilable']);
      }
      else
      {
        $OUT($this->TRUE_COLOR,$this->DEST_URL);
        $OUT($this->TRUE_COLOR);
      }
    }
  }
  /*
  析构函数:释放图片
  */
  function __destruct()
  {
  /*释放图片*/
    imagedestroy($this->TRUE_COLOR);
    imagedestroy($this->PICTURE_CREATE);
  }
  #end of class
}
?>

 

你可能感兴趣的:(C++,c,PHP,C#,ext)