php水印類
① 誰有php批量處理圖片、圖片生成縮略圖、圖片添加水印的函數
//批量處理圖片、圖片生成縮略圖、圖片添加水印
$dir=opendir(dirname(__FILE__));
while(!!$_file=readdir($dir)){
list($filesname,$kzm)=explode(".",$_file);//獲取擴展名
if($kzm=="gif"or$kzm=="jpg"or$kzm=="JPG"or$kzm=="png"){
if(!makethumb("$_file","120","120","100")){
echo'執行成功!';
}else{
echo'執行失敗!';
}
}
}
closedir($dir);
/**
*處理縮略圖並添加水印函數
*@accesspubliuc
*@param$srcFile-----------圖片文件名
*@param$dstFile-----------另存的文件名
*@param$dstW-------------圖片保存的寬度
*@param$dstH--------------圖片保存的高度
*@param$rate---------------圖片保存的品質
*@param$markwords-----水印文字
*@param$markimage-----水印圖片
*@param使用方法makethumb("a.jpg","b.jpg","120","120","100");
*/
functionmakethumb($srcFile/*,$dstFile*/,$dstW,$dstH,$rate=100/*,$markwords=null,$markimage=null*/){
$data=GetImageSize($srcFile);
switch($data[2]){
case1:
$im=@ImageCreateFromGIF($srcFile);
break;
case2:
$im=@ImageCreateFromJPEG($srcFile);
break;
case3:
$im=@ImageCreateFromPNG($srcFile);
break;
}
if(!$im)returnFalse;
$srcW=ImageSX($im);
$srcH=ImageSY($im);
$dstX=0;
$dstY=0;
if($srcW*$dstH>$srcH*$dstW){
$fdstH=round($srcH*$dstW/$srcW);
$dstY=floor(($dstH-$fdstH)/2);
$fdstW=$dstW;
}
else
{
$fdstW=round($srcW*$dstH/$srcH);
$dstX=floor(($dstW-$fdstW)/2);
$fdstH=$dstH;
}
$ni=ImageCreateTrueColor($dstW,$dstH);
$dstX=($dstX<0)?0:$dstX;
$dstY=($dstX<0)?0:$dstY;
$dstX=($dstX>($dstW/2))?floor($dstW/2):$dstX;
$dstY=($dstY>($dstH/2))?floor($dstH/s):$dstY;
$white=ImageColorAllocate($ni,255,255,255);
$black=ImageColorAllocate($ni,0,0,0);
imagefilledrectangle($ni,0,0,$dstW,$dstH,$white);//填充背景色
ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$fdstW,$fdstH,$srcW,$srcH);
//if($markwords!=null){
//$markwords=iconv("gb2312","UTF-8",$markwords);
////轉換文字編碼
//ImageTTFText($ni,20,30,450,560,$black,"simhei.ttf",$markwords);//寫入文字水印,參數依次為,文字大小|偏轉度|橫坐標|縱坐標|文字顏色|文字類型|文字內容
//}elseif($markimage!=null){
//$wimage_data=GetImageSize($markimage);
//switch($wimage_data[2]){
//case1:
//$wimage=@ImageCreateFromGIF($markimage);
//break;
//case2:
//$wimage=@ImageCreateFromJPEG($markimage);
//break;
//case3:
//$wimage=@ImageCreateFromPNG($markimage);
//break;
//}
//image($ni,$wimage,500,560,0,0,88,31);//寫入圖片水印,水印圖片大小默認為88*31
//imagedestroy($wimage);
//}
$dstFile=$srcFile.'.gif';
ImageJpeg($ni,$dstFile,$rate);
//ImageJpeg($ni,$srcFile,$rate);
imagedestroy($im);
imagedestroy($ni);
}
② PHP給圖片加水印的思想是什麼
貼個php的圖片處理類 你可以自己研究下 共同進步 呵呵 
/*
此類包含以下功能
A.生成縮略圖
B.給圖片添加文字水印(包括中文)
C.將指定的圖片旋轉90度/180度/270度,並保存至文件
D.將圖片水平/垂直翻轉,並保存至文件
E.在線裁剪圖片任意部分矩形(正在編寫之中)
    具體目標效果暫時可以參考 http://yananzb.com/cut/cut.htm
    期待您的響應
成員函數說明
---------------------------------------------------------------------------------------------
public void CImage::__construct(string $src_image_file)
    功能:類CImage的構造函數
    參數 
    $src_image_file    字元串,源圖片文件名 注意,目前只支持gif,png,jpeg,jpg格式,這是GD庫的限制,並非本程序的局限
---------------------------------------------------------------------------------------------
public bool CImage: thumb($image_dist,$x)   
    功能:根據源圖片生成縮略圖,並保存至文件
    $image_dist        字元串         目標縮略圖片路徑及文件名 如 /File/th.jpg
    $x                        整型                目標縮略圖片的尺寸限制即當原始圖片的寬大於高時,那麼新的縮略圖的寬為$x,反之高為$x
---------------------------------------------------------------------------------------------
public bool image_press($image_dist,$str,$font="simkai.ttf")
函數功能:
    圖片生成水印並保存新圖片至目標文件
    參數說明:
    $image_dist 字元串        目標圖片名
    $str                字元串        要寫入到圖片水印的字元串
    $font                字元串        合法的系統字體名或WEB目錄中正確的字體文件名
---------------------------------------------------------------------------------------------
public bool rotate($image_dist,$angle)
    函數功能:
        將源圖片旋轉一定角度並將新圖片保存至文件
    參數說明:
        $image_dist            字元串        目標圖片文件名
        $angle                    整數            要旋轉的角度 只能是90或180或270度
---------------------------------------------------------------------------------------------
public bool rotate_h($image_dist)
    函數功能:
        將源圖片水平翻轉,並將新圖片保存至文件
    參數說明:
        $image_dist        字元串        目標圖片文件名
---------------------------------------------------------------------------------------------
public bool rotate_v($image_dist)
函數功能:
        將源圖片垂直翻轉,並將新圖片保存至文件
    參數說明:
        $image_dist        字元串        目標圖片文件名
---------------------------------------------------------------------------------------------
使用範例:
$p=new CImage("s.jpg");        //創建一個圖片處理對象
$p->thumb("thumb.jpg",300);    //生成縮略圖 限制尺寸為300,保存為thumb.jpg
$p->rotate("rt.jpg",90);        //旋轉90度,並保存為rt.jpg,類似地,你可以將90換成180,270進行旋轉
$p->rotate_h("h.jpg");        //水平翻轉
$p->rotate_v("v.jpg");        //垂直翻轉
*/
class CImage{
    var $src_image;
    var $width;
    var $height;
    var $image_type;
    var $img;
    var $src_x;
    var $src_y;
   
    function __construct($image_file)
    {
        $info=GetImageSize($image_file);
        $this->src_image=$image_file;
        $this->width=$info[0];
        $this->height=$info[1];
        
        switch($info[2])
        {
            case 1:
                $this->image_type="gif";
                break;
            case 2:
                $this->image_type="jpeg";
                break;
            case 3:
                $this->image_type="png";
                break;
            default:
                return false;
                //echo("Unsurport Image type.");
                break;
        }    //swith end
        //echo "ok";
        $new_function='ImageCreateFrom'.ucfirst($this->image_type);
        $this->img=$new_function($this->src_image);
        $this->src_x=ImageSX($this->img);
        $this->src_y=ImageSY($this->img);
    }
    function thumb($image_dist,$x)        //$x為新圖的限制邊的尺寸
    {        
        $src_x=ImageSX($this->img);
        $src_y=ImageSY($this->img);
        $scale=min($x/$src_x,$x/$src_y);
   
        if($scale<1)
        {
            $new_x=floor($scale*$src_x);
            $new_y=floor($scale*$src_y);
            $img_tmp=ImageCreateTrueColor($new_x,$new_y);    //set the size of Canvas for the new Image
            ImageCopyResampled($img_tmp,$this->img,0,0,0,0,$new_x,$new_y,$src_x,$src_y);    //Resampled
            ImageDestroy($this->img);
            $new_function="Image".ucfirst($this->image_type);
            return $new_function($img_tmp,$image_dist);
        }
    }    //  thumb end
   
 //給圖片生成文字水印
    function image_press($image_dist,$str,$font="simkai.ttf") { 
        $str=iconv("GB2312","utf-8",$str);            
        $blue=ImageColorAllocate($this->img,90,255,255);
        $white=ImageColorAllocate($this->img,255,0,0);
        ImageTTFText($this->img,20,0,$this->src_x/2/2,$this->src_y-80,$white,$font,$str);
        $new_function="Image".ucfirst($this->image_type);
        return $new_function($this->img,$image_dist);        
    }
    function rotate($image_dist,$angle)
    {
        $img_tmp=null;
        $new_function="Image".ucfirst($this->image_type);
        if(($angle!=90)&&($angle!=180)&&($angle!=270))
        {
            echo("Un-valid angle on calling CImage::rotate(\$image_dist,\$angle) .<p>The valid angle must be 90 or 180 or 270.");
            return false;
        }
        if(($angle==90)||($angle==270))
        {
            $img_tmp=ImageCreateTrueColor($this->src_y,$this->src_x);
        }
        else
        {
            $img_tmp=ImageCreateTrueColor($this->src_x,$this->src_y);
        }
        
        switch($angle)
        {
            case 90:
                for($i=0;$i<$this->src_x;$i++)
                {
                    for($j=0;$j<$this->src_y;$j++)
                    {
                        ImageSetPixel($img_tmp,$this->src_y-$j-1,$i,ImageColorAt($this->img,$i,$j));   
                    }
                }   
                return $new_function($img_tmp,$image_dist);
                break;
            case 180:
                for($i=0;$i<$this->src_x;$i++)
                {
                    for($j=0;$j<$this->src_y;$j++)
                    {
                        ImageSetPixel($img_tmp,$this->src_x-$i-1,$this->src_y-$j-1,ImageColorAt($this->img,$i,$j));   
                    }
                }
                return $new_function($img_tmp,$image_dist);
                break;
            case 270:
                for($i=0;$i<$this->src_x;$i++)
                {
                    for($j=0;$j<$this->src_y;$j++)
                    {
                        ImageSetPixel($img_tmp,$j,$this->src_x-$i-1,ImageColorAt($this->img,$i,$j));   
                    }
                }
                return $new_function($img_tmp,$image_dist);
                break;
        }    //end switch
        
    }        //end rotate
   
    function rotate_h($image_dist)
    {
        $new_function="Image".ucfirst($this->image_type);
        $img_tmp=ImageCreateTrueColor($this->src_x,$this->src_y);
        ImageCopyResampled($img_tmp,$this->img,0,0,$this->src_x-1,0,$this->src_x,$this->src_y,-$this->src_x,$this->src_y); //水平翻轉
        return    $new_function($img_tmp,$image_dist);   
    }
   
    function rotate_v($image_dist)
    {
    $new_function="Image".ucfirst($this->image_type);
    $img_tmp=ImageCreateTrueColor($this->src_x,$this->src_y);
    ImageCopyResampled($img_tmp,$this->img,0,0,0,$this->src_y-1,$this->src_x,$this->src_y,$this->src_x,-$this->src_y);
    return $new_function($img_tmp,$image_dist);
    }   
}    //end CImage
③ 誰有打水印代碼PHP
非常好用的加水印程序
//watermark(源圖,生成文件,生成位置,水印文件,水印文本,背景色)
 function watermark($source, $target = '', $w_pos = 0, $w_img = '', $w_text = '', $w_font = 12, $w_color = '#cccccc')
 {
  if(!$this->watermark_enable || !$this->check($source)) return false;
  if(!$target) $target = $source;
  if ($w_img == '' && $w_text == '')
  $w_img = $this->w_img;
  $source_info = getimagesize($source);
  $source_w    = $source_info[0]; //獲取寬
  $source_h    = $source_info[1]; //獲取高
  if($source_w < $this->w_minwidth || $source_h < $this->w_minheight) return false; //寬和高達不到要求直接返回
  switch($source_info[2]) //新建圖片
  {
   case 1 :
    $source_img = imagecreatefromgif($source);
    break;
   case 2 :
    $source_img = imagecreatefromjpeg($source);
    break;
   case 3 :
    $source_img = imagecreatefrompng($source);
    break;
   default :
    return false;
  }
  if(!empty($w_img) && file_exists($w_img)) //水印文件
  {
   $ifwaterimage = 1; //是否水印圖
   $water_info   = getimagesize($w_img); //水印信息
   $width        = $water_info[0];
   $height       = $water_info[1];
   switch($water_info[2])
   {
    case 1 :
     $water_img = imagecreatefromgif($w_img);
     break;
    case 2 :
     $water_img = imagecreatefromjpeg($w_img);
     break;
    case 3 :
     $water_img = imagecreatefrompng($w_img);
     break;
    default :
     return;
   }
  }
  else
  {
   $ifwaterimage = 0;
   //imagettfbbox 本函數計算並返回一個包圍著 TrueType 文本范圍的虛擬方框的像素大小。
   //imagettfbbox ( 字體大小, 字體角度, 字體文件,文件 )
   $temp = imagettfbbox(ceil($w_font*1.2), 0, $this->fontfile, $w_text);//取得使用 truetype 字體的文本的范圍
   $width = $temp[4] - $temp[6]; //右上角 X 位置 - 左上角 X 位置
   $height = $temp[3] - $temp[5]; //右下角 Y 位置- 右上角 Y 位置
   unset($temp);
  }
  switch($w_pos)
  {
   case 0: //隨機位置
    $wx = rand(0,($source_w - $width));
    $wy = rand(0,($source_h - $height));
    break;
   case 1: //左上角
    $wx = 5;
    $wy = 5;
    break;
   case 2: //上面中間位置
    $wx = ($source_w - $width) / 2;
    $wy = 0;
    break;
   case 3: //右上角
    $wx = $source_w - $width;
    $wy = 0;
    break;
   case 4: //左面中間位置
    $wx = 0;
    $wy = ($source_h - $height) / 2;
    break;
   case 5: //中間位置
    $wx = ($source_w - $width) / 2;
    $wy = ($source_h - $height) / 2;
    break;
   case 6: //底部中間位置
    $wx = ($source_w - $width) / 2;
    $wy = $source_h - $height;
    break;
   case 7: //左下角
    $wx = 0;
    $wy = $source_h - $height;
    break;
   case 8: //右面中間位置
    $wx = $source_w - $width;
    $wy = ($source_h - $height) /2;
    break;
   case 9: //右下角
    $wx = $source_w - $width;
    $wy = $source_h - $height ;
    break;
   default: //隨機
    $wx = rand(0,($source_w - $width));
    $wy = rand(0,($source_h - $height));
    break;
  }
  if($ifwaterimage) //如果有水印圖
  {
   //imagemerge 拷貝並合並圖像的一部分
   //參數(源圖,水印圖,拷貝到源圖x位置,拷貝到源圖y位置,從水印圖x位置,從水印圖y位置,高,寬,透明度)
   imagemerge($source_img, $water_img, $wx, $wy, 0, 0, $width, $height, $this->w_pct);
  }
  else
  {
   if(!empty($w_color) && (strlen($w_color)==7))
   {
    $r = hexdec(substr($w_color,1,2)); //獲取紅色
    $g = hexdec(substr($w_color,3,2)); //獲取綠色
    $b = hexdec(substr($w_color,5)); //獲取藍色
   }
   else
   {
    return;
   }
   //imagecolorallocate 基於調色板的圖像填充背景色
   //imagestring 水平地畫一行字元串
   //imagestring(源圖,字體大小,位置X,位置Y,文字,顏色)
   //參數($image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text) 
   imagettftext($source_img,$w_font,0,$wx,$wy,imagecolorallocate($source_img,$r,$g,$b),$this->fontfile,$w_text);
   //imagestring($source_img,$w_font,$wx,$wy,$w_text,imagecolorallocate($source_img,$r,$g,$b));
  }
  //輸出到文件或者瀏覽器
  switch($source_info[2])
  {
   case 1 :
    imagegif($source_img, $target); //以 GIF 格式將圖像輸出到瀏覽器或文件
    break;
   case 2 :
    imagejpeg($source_img, $target, $this->w_quality); //以 JPEG 格式將圖像輸出到瀏覽器或文件
    break;
   case 3 :
    imagepng($source_img, $target); //以 PNG 格式將圖像輸出到瀏覽器或文件
    break;
   default :
    return;
  }
  if(isset($water_info))
  {
   unset($water_info); //銷毀
  }
  if(isset($water_img))
  {
   imagedestroy($water_img); //銷毀
  }
  unset($source_info);
  imagedestroy($source_img);
  return true;
 }
 //gd庫必須存在,後綴為jpg|jpeg|gif|png,文件存在,imagecreatefromjpeg或者imagecreatefromgif存在
 function check($image)
 {
  return extension_loaded('gd') && 
  preg_match("/\.(jpg|jpeg|gif|png)/i", $image, $m) && 
  file_exists($image) && 
  function_exists('imagecreatefrom'.($m[1] == 'jpg' ? 'jpeg' : $m[1]));
 }
}
④ php 怎麼上傳完圖片之後,給這個圖片打水印,並且保存在另一個文件夾
這個php中的圖片處理類完全足夠了,使用圖片水印 
$groundImg = "DSC05940.jpeg"; 
$groundInfo = getimagesize($groundImg); 
$ground_w = $groundInfo[0]; 
//print_r($groundInfo); 
$ground_h = $groundInfo[1]; 
switch($groundInfo[2]){ 
case 1: 
$ground_im = imagecreatefromgif($groundImg); 
break; 
case 2: 
$ground_im = imagecreatefromjpeg($groundImg); 
break; 
case 3: 
$ground_im = imagecreatefrompng($groundImg); 
break; 
} 
$waterImg = "DSC05949.jpeg"; 
$imgInfo =getimagesize($waterImg); 
$water_w = $imgInfo[0]; 
$water_w = $imgInfo[1]; 
switch($imgInfo[2]){ 
case 1: 
$water_im = imagecreatefromgif($waterImg); 
break; 
case 2: 
$water_im = imagecreatefromjpeg($waterImg); 
break; 
case 3: 
$water_im = imagecreatefrompng($waterImg); 
break; 
} 
image($ground_im,$water_im,100,100,0,0,500,500); 
header("Content-type: image/jpeg"); 
imagejpeg($ground_im);
這些都很麻煩,建議使用框架,很多框架都提供了圖片處理類供使用
⑤ PHP圖片生成
給你一個php 圖像處理類,完全能實現你的功能,你自己研究一下吧
<?php 
class image
{
 var $w_pct = 50; //透明度
 var $w_quality = 80; //質量
 var $w_minwidth = 300; //最小寬
 var $w_minheight = 300; //最小高
 var $thumb_enable; //是否生成縮略圖
 var $watermark_enable; //是否生水印
 var $interlace = 0;  //圖像是否為隔行掃描的
 var $fontfile;  //字體文件
 var $w_img ; //默認水印圖
    function __construct()
    {
  global $SITE_CONFING;
  $this->thumb_enable = $SITE_CONFING['thumb_enable'];
  $this->watermark_enable = $SITE_CONFING['watermark_enable'];
  $this->set($SITE_CONFING['watermark_minwidth'], $SITE_CONFING['watermark_minheight'], $SITE_CONFING['watermark_quality'], $SITE_CONFING['watermark_pct'], $SITE_CONFING['watermark_fontfile'],$SITE_CONFING['watermark_img']);
    }
 function image()
 {
  $this->__construct();
 }
 function set($w_minwidth = 300, $w_minheight = 300, $w_quality = 80, $w_pct = 100,$fontfile,$w_img)
 {
  $this->w_minwidth = $w_minwidth;
  $this->w_minheight = $w_minheight;
  $this->w_quality = $w_quality;
  $this->w_pct = $w_pct;
  $this->fontfile = $fontfile;
  $this->w_img = $w_img;
 }
    function info($img) 
 {
        $imageinfo = getimagesize($img); //返回圖像信息數組 0=>寬的像素 1=>高的像素 2=>是圖像類型的標記 3 =>是文本字元串,內容為「height="yyy" width="xxx"」,
        if($imageinfo === false) return false;
  $imagetype = strtolower(substr(image_type_to_extension($imageinfo[2]),1)); //獲取圖像文件類型 $imageinfo[2]是圖像類型的標記
  $imagesize = filesize($img); //圖像大小
  $info = array(
    'width'=>$imageinfo[0],
    'height'=>$imageinfo[1],
    'type'=>$imagetype,
    'size'=>$imagesize,
    'mime'=>$imageinfo['mime']
    );
  return $info;
    }
    function thumb($image, $filename = '', $maxwidth = 200, $maxheight = 50, $suffix='_thumb', $autocut = 0) 
    {
     if(!$this->thumb_enable || !$this->check($image)) return false;
        $info  = $this->info($image); //獲取圖片信息
        if($info === false) return false;
  $srcwidth  = $info['width']; //源圖寬
  $srcheight = $info['height']; //源圖高
  $pathinfo = pathinfo($image);
  $type =  $pathinfo['extension']; //取得擴展名
  if(!$type) $type = $info['type']; //如果沒有取到,用$info['type']
  $type = strtolower($type);
  unset($info);
  $scale = min($maxwidth/$srcwidth, $maxheight/$srcheight); //獲取縮略比例
  //獲取按照源圖的比列
  $createwidth = $width  = (int)($srcwidth*$scale); //取得縮略寬
  $createheight = $height = (int)($srcheight*$scale); //取得縮略高
  $psrc_x = $psrc_y = 0;
  if($autocut) //按照縮略圖的比例來獲取
  {
   if($maxwidth/$maxheight<$srcwidth/$srcheight && $maxheight>=$height) //如果縮略圖按比列比源圖窄的話
   {
    $width = $maxheight/$height*$width; //寬按照相應比例做處理
    $height = $maxheight; //高不變
   }
   elseif($maxwidth/$maxheight>$srcwidth/$srcheight && $maxwidth>=$width)//如果縮略圖按比列比源圖寬的話
   {
    $height = $maxwidth/$width*$height;
    $width = $maxwidth;
   }
   $createwidth = $maxwidth;
   $createheight = $maxheight;
  }
  $createfun = 'imagecreatefrom'.($type=='jpg' ? 'jpeg' : $type); //找到不同的圖像處理函數
  $srcimg = $createfun($image); //新建圖像
  if($type != 'gif' && function_exists('imagecreatetruecolor'))
   $thumbimg = imagecreatetruecolor($createwidth, $createheight); //新建一個真彩色圖像
  else
   $thumbimg = imagecreate($width, $height); //新建一個基於調色板的圖像
  if(function_exists('imageresampled')) //重采樣拷貝部分圖像並調整大小,真彩
   //imageresampled(新圖,源圖,新圖左上角x距離,新圖左上角y距離,源圖左上角x距離,源圖左上角y距離,新圖寬,新圖高,源圖寬,源圖高)
   imageresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight); 
  else //拷貝部分圖像並調整大小,調色板
   imageresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height,  $srcwidth, $srcheight); 
  if($type=='gif' || $type=='png')
  {
   //imagecolorallocate 為一幅圖像分配顏色
   $background_color  =  imagecolorallocate($thumbimg,  0, 255, 0);  // 給基於調色板的圖像填充背景色, 指派一個綠色
   // imagecolortransparent 將某個顏色定義為透明色
   imagecolortransparent($thumbimg, $background_color);  //  設置為透明色,若注釋掉該行則輸出綠色的圖 
  }
  // imageinterlace 激活或禁止隔行掃描
  if($type=='jpg' || $type=='jpeg') imageinterlace($thumbimg, $this->interlace);
  $imagefun = 'image'.($type=='jpg' ? 'jpeg' : $type);
  //imagejpeg imagegif imagepng
  if(empty($filename)) $filename  = substr($image, 0, strrpos($image, '.')).$suffix.'.'.$type; //獲取文件名
  //aaa.gif aaa_thumb.gif
  $imagefun($thumbimg, $filename); //新建圖像
  imagedestroy($thumbimg); //銷毀縮略圖
  imagedestroy($srcimg); //銷毀源圖
  return $filename;
    }
 //watermark(源圖,生成文件,生成位置,水印文件,水印文本,背景色)
 function watermark($source, $target = '', $w_pos = 0, $w_img = '', $w_text = '', $w_font = 12, $w_color = '#cccccc')
 {
  if(!$this->watermark_enable || !$this->check($source)) return false;
  if(!$target) $target = $source;
  if ($w_img == '' && $w_text == '')
  $w_img = $this->w_img;
  $source_info = getimagesize($source);
  $source_w    = $source_info[0]; //獲取寬
  $source_h    = $source_info[1]; //獲取高
  if($source_w < $this->w_minwidth || $source_h < $this->w_minheight) return false; //寬和高達不到要求直接返回
  switch($source_info[2]) //新建圖片
  {
   case 1 :
    $source_img = imagecreatefromgif($source);
    break;
   case 2 :
    $source_img = imagecreatefromjpeg($source);
    break;
   case 3 :
    $source_img = imagecreatefrompng($source);
    break;
   default :
    return false;
  }
  if(!empty($w_img) && file_exists($w_img)) //水印文件
  {
   $ifwaterimage = 1; //是否水印圖
   $water_info   = getimagesize($w_img); //水印信息
   $width        = $water_info[0];
   $height       = $water_info[1];
   switch($water_info[2])
   {
    case 1 :
     $water_img = imagecreatefromgif($w_img);
     break;
    case 2 :
     $water_img = imagecreatefromjpeg($w_img);
     break;
    case 3 :
     $water_img = imagecreatefrompng($w_img);
     break;
    default :
     return;
   }
  }
  else
  {
   $ifwaterimage = 0;
   //imagettfbbox 本函數計算並返回一個包圍著 TrueType 文本范圍的虛擬方框的像素大小。
   //imagettfbbox ( 字體大小, 字體角度, 字體文件,文件 )
   $temp = imagettfbbox(ceil($w_font*1.2), 0, $this->fontfile, $w_text);//取得使用 truetype 字體的文本的范圍
   $width = $temp[4] - $temp[6]; //右上角 X 位置 - 左上角 X 位置
   $height = $temp[3] - $temp[5]; //右下角 Y 位置- 右上角 Y 位置
   unset($temp);
  }
  switch($w_pos)
  {
   case 0: //隨機位置
    $wx = rand(0,($source_w - $width));
    $wy = rand(0,($source_h - $height));
    break;
   case 1: //左上角
    $wx = 5;
    $wy = 5;
    break;
   case 2: //上面中間位置
    $wx = ($source_w - $width) / 2;
    $wy = 0;
    break;
   case 3: //右上角
    $wx = $source_w - $width;
    $wy = 0;
    break;
   case 4: //左面中間位置
    $wx = 0;
    $wy = ($source_h - $height) / 2;
    break;
   case 5: //中間位置
    $wx = ($source_w - $width) / 2;
    $wy = ($source_h - $height) / 2;
    break;
   case 6: //底部中間位置
    $wx = ($source_w - $width) / 2;
    $wy = $source_h - $height;
    break;
   case 7: //左下角
    $wx = 0;
    $wy = $source_h - $height;
    break;
   case 8: //右面中間位置
    $wx = $source_w - $width;
    $wy = ($source_h - $height) /2;
    break;
   case 9: //右下角
    $wx = $source_w - $width;
    $wy = $source_h - $height ;
    break;
   default: //隨機
    $wx = rand(0,($source_w - $width));
    $wy = rand(0,($source_h - $height));
    break;
  }
  if($ifwaterimage) //如果有水印圖
  {
   //imagemerge 拷貝並合並圖像的一部分
   //參數(源圖,水印圖,拷貝到源圖x位置,拷貝到源圖y位置,從水印圖x位置,從水印圖y位置,高,寬,透明度)
   imagemerge($source_img, $water_img, $wx, $wy, 0, 0, $width, $height, $this->w_pct);
  }
  else
  {
   if(!empty($w_color) && (strlen($w_color)==7))
   {
    $r = hexdec(substr($w_color,1,2)); //獲取紅色
    $g = hexdec(substr($w_color,3,2)); //獲取綠色
    $b = hexdec(substr($w_color,5)); //獲取藍色
   }
   else
   {
    return;
   }
   //imagecolorallocate 基於調色板的圖像填充背景色
   //imagestring 水平地畫一行字元串
   //imagestring(源圖,字體大小,位置X,位置Y,文字,顏色)
   //參數($image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text) 
   imagettftext($source_img,$w_font,0,$wx,$wy,imagecolorallocate($source_img,$r,$g,$b),$this->fontfile,$w_text);
   //imagestring($source_img,$w_font,$wx,$wy,$w_text,imagecolorallocate($source_img,$r,$g,$b));
  }
  //輸出到文件或者瀏覽器
  switch($source_info[2])
  {
   case 1 :
    imagegif($source_img, $target); //以 GIF 格式將圖像輸出到瀏覽器或文件
    break;
   case 2 :
    imagejpeg($source_img, $target, $this->w_quality); //以 JPEG 格式將圖像輸出到瀏覽器或文件
    break;
   case 3 :
    imagepng($source_img, $target); //以 PNG 格式將圖像輸出到瀏覽器或文件
    break;
   default :
    return;
  }
  if(isset($water_info))
  {
   unset($water_info); //銷毀
  }
  if(isset($water_img))
  {
   imagedestroy($water_img); //銷毀
  }
  unset($source_info);
  imagedestroy($source_img);
  return true;
 }
 //gd庫必須存在,後綴為jpg|jpeg|gif|png,文件存在,imagecreatefromjpeg或者imagecreatefromgif存在
 function check($image)
 {
  return extension_loaded('gd') && 
  preg_match("/\.(jpg|jpeg|gif|png)/i", $image, $m) && 
  file_exists($image) && 
  function_exists('imagecreatefrom'.($m[1] == 'jpg' ? 'jpeg' : $m[1]));
  //imagecreatefromjpeg
  //imagecreatefromgif
  //imagecreatefrompng
 }
}
/**
 縮略圖
1.新建一個圖像資源 通過 imagecreatefromgif imagecreatefromjpeg imagecreatefrompng
2.imageresampled 拷貝圖像,並調整大小
水印:圖片水印,文字水印
1. 創建圖像
2.加水印
圖片水印:imagemerge 把2張圖合並在一起
文字水印:imagettftext  向圖像寫入文字
*/
?>
⑥ thinkphp 文字水印 怎麼調透明度
可以支持水印圖片的透明度(0~100,默認值是80),例如:
$image=newThinkImage();
//在圖片左上角添加水印(水印文件位於./logo.png)水印圖片的透明度為50並保存為water.jpg
$image->open('./1.jpg')->water('./logo.png',ThinkImage::IMAGE_WATER_NORTHWEST,50)->save("water.jpg");
⑦ php如何實現水印平鋪
代碼如下:
imagick代碼:
<?php
$image = new imagick('logo.jpg'); 
$im = new imagick();
$im->newimage( 140, 80, new imagickpixel( "none" ) ); 
$draw = new imagickdraw(); 
$draw->setfillcolor(new imagickpixel( "grey" ));
$draw->setgravity(imagick::gravity_northwest);
$draw->annotation(10,10 ,'right');
$draw->setgravity(imagick::gravity_southeast);
$draw->annotation(5,15 ,'right');
$im->drawimage( $draw); 
$image = $image->textureimage($im);
$image->compositeimage($image,imagick::composite_,0,0);
header( "content-type: image/{$image->getimageformat()}" );
$image->writeimage('wmark_text_tiled.jpg');
$image->clear();
$image->destroy();
?>
