多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > 程序人生 > 隨筆 > kindeditor編輯器加圖片上傳水印功能 for php

kindeditor編輯器加圖片上傳水印功能 for php

來源:程序員人生   發布時間:2015-12-01 15:15:56 閱讀次數:5926次
第一步:打開kindeditor/upload_json.php,在下面增加一個函數,內容如下
  1. /* 
  2.     * 功能:PHP圖片水印,水印支持圖片或文字 
  3.     * 參數: 
  4.     * $groundImage 背景圖片,即需要加水印的圖片,暫只支持GIF,JPG,PNG格式; 
  5.     * $waterPos 水印位置,有10種狀態,0為隨機位置; 
  6.     *   1為頂端居左,2為頂端居中,3為頂端居右; 
  7.     *   4為中部居左,5為中部居中,6為中部居右; 
  8.     *   7為底端居左,8為底端居中,9為底端居右; 
  9.     * $waterImage 圖片水印,即作為水印的圖片,暫只支持GIF,JPG,PNG格式; 
  10.     * $alpha 水印透明度,取值1-100; 
  11.     * $waterText 文字水印,即把文字作為為水印,支持ASCII碼,不支持中文; 
  12.     * $textFont 文字大小,值為1、2、3、4或5,默認為5; 
  13.     * $textColor 文字顏色,值為十六進制顏色值,默認為#FF0000(紅色); 
  14.     * min_image_w 水印圖片最小寬度 
  15.     * min_image_h 水印圖片最小高度 
  16.     * 
  17.     * $waterImage 和 $waterText 最好不要同時使用,選其中之一即可,優先使用 $waterImage。 
  18.     * 當$waterImage有效時,參數$waterString、$stringFont、$stringColor均不生效。 
  19.     * 加水印后的圖片的文件名和 $groundImage 一 
  20.     樣。 
  21. */ 
  22. function imageWaterMark($groundImage$waterPos=0, $waterImage=''$alpha=80, $waterText=''$water_fontfile$textFont=9, $textColor='#FF0000',$min_image_w=300,$min_image_h=200){ 
  23.  
  24.     $isWaterImage = FALSE; 
  25.     $formatMsg = '不支持該圖片格式!請使用GIF、JPG、PNG格式的圖片。'
  26.     $fontFile = $water_fontfile
  27.     //讀取水印文件 
  28.     if(!emptyempty($waterImage) && file_exists($waterImage)){ 
  29.         $isWaterImage = TRUE; 
  30.         $water_info = getimagesize($waterImage); 
  31.         $water_w = $water_info[0];//取得水印圖片的寬 
  32.         $water_h = $water_info[1];//取得水印圖片的高 
  33.         switch($water_info[2]){//取得水印圖片的格式 
  34.             case 1:$water_im = imagecreatefromgif($waterImage);break
  35.             case 2:$water_im = imagecreatefromjpeg($waterImage);break
  36.             case 3:$water_im = imagecreatefrompng($waterImage);break
  37.             default:die($formatMsg); 
  38.         } 
  39.     } 
  40.     //讀取背景圖片 
  41.     if(!emptyempty($groundImage) && file_exists($groundImage)){ 
  42.         $ground_info = getimagesize($groundImage); 
  43.         $ground_w = $ground_info[0];//取得背景圖片的寬 
  44.         $ground_h = $ground_info[1];//取得背景圖片的高 
  45.         switch($ground_info[2]){//取得背景圖片的格式 
  46.             case 1:$ground_im = imagecreatefromgif($groundImage);break
  47.             case 2:$ground_im = imagecreatefromjpeg($groundImage);break
  48.             case 3:$ground_im = imagecreatefrompng($groundImage);break
  49.             default:die($formatMsg); 
  50.         } 
  51.     }else
  52.         alert("水印圖片不存在!"); 
  53.     } 
  54.     //水印位置 
  55.     if($isWaterImage){//圖片水印 
  56.         $w = $water_w
  57.         $h = $water_h
  58.         $label = "圖片的"
  59.     }else{//文字水印 
  60.         $temp = imagettfbbox($textFont, 0, $fontFile$waterText);//取得使用 TrueType 字體的文本的范圍 
  61.         $w = $temp[2] - $temp[6]; 
  62.         $h = $temp[3] - $temp[7]; 
  63.         unset($temp); 
  64.         $label = "文字區域"
  65.     } 
  66.     if(($ground_w<$w) || ($ground_h<$h)){ 
  67.        // echo "需要加水印的圖片的長度或寬度比水印".$label."還小,無法生成水印!"; 
  68.         return
  69.     } 
  70.      
  71.      if(($ground_w<$min_image_w) || ($ground_h<$min_image_h)){ 
  72.        // echo "小于最低背景圖片的高和寬"; 
  73.         return
  74.     } 
  75.      
  76.     switch($waterPos){ 
  77.         case 0://隨機 
  78.         $posX = rand(0,($ground_w - $w)); 
  79.         $posY = rand(0,($ground_h - $h)); 
  80.         break
  81.         case 1://1為頂端居左 
  82.         $posX = 0; 
  83.         $posY = 0; 
  84.         break
  85.         case 2://2為頂端居中 
  86.         $posX = ($ground_w - $w) / 2; 
  87.         $posY = 0; 
  88.         break
  89.         case 3://3為頂端居右 
  90.         $posX = $ground_w - $w
  91.         $posY = 0; 
  92.         break
  93.         case 4://4為中部居左 
  94.         $posX = 0; 
  95.         $posY = ($ground_h - $h) / 2; 
  96.         break
  97.         case 5://5為中部居中 
  98.         $posX = ($ground_w - $w) / 2; 
  99.         $posY = ($ground_h - $h) / 2; 
  100.         break
  101.         case 6://6為中部居右 
  102.         $posX = $ground_w - $w
  103.         $posY = ($ground_h - $h) / 2; 
  104.         break
  105.         case 7://7為底端居左 
  106.         $posX = 0; 
  107.         $posY = $ground_h - $h
  108.         break
  109.         case 8://8為底端居中 
  110.         $posX = ($ground_w - $w) / 2; 
  111.         $posY = $ground_h - $h
  112.         break
  113.         case 9://9為底端居右 
  114.         $posX = $ground_w - $w
  115.         $posY = $ground_h - $h
  116.         if(!$isWaterImage){ 
  117.         $posY = $ground_h - $h-20; 
  118.         } 
  119.         break
  120.         default://隨機 
  121.         $posX = rand(0,($ground_w - $w)); 
  122.         $posY = rand(0,($ground_h - $h)); 
  123.         break
  124.     } 
  125.     //設定圖像的混色模式 
  126.     imagealphablending($ground_im, true); 
  127.     if($isWaterImage){//圖片水印 
  128.         //imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//拷貝水印到目標文件 
  129.         //生成混合圖像 
  130.         imagecopymerge($ground_im$water_im$posX$posY, 0, 0, $water_w$water_h$alpha); 
  131.     } else {//文字水印 
  132.         if( !emptyempty($textColor) && (strlen($textColor)==7)){ 
  133.             $R = hexdec(substr($textColor,1,2)); 
  134.             $G = hexdec(substr($textColor,3,2)); 
  135.             $B = hexdec(substr($textColor,5)); 
  136.         } else { 
  137.             die("水印文字顏色格式不正確!"); 
  138.         } 
  139.         imagestring($ground_im$textFont$posX$posY$waterText, imagecolorallocate($ground_im$R$G$B)); 
  140.     } 
  141.     //生成水印后的圖片 
  142.     @unlink($groundImage); 
  143.     switch($ground_info[2]){//取得背景圖片的格式 
  144.         case 1:imagegif($ground_im,$groundImage);break
  145.         case 2:imagejpeg($ground_im,$groundImage);break
  146.         case 3:imagepng($ground_im,$groundImage);break
  147.         default:die($errorMsg); 
  148.     } 
  149.     //釋放內存 
  150.     if(isset($water_info)) unset($water_info); 
  151.     if(isset($water_im)) imagedestroy($water_im); 
  152.     unset($ground_info); 
  153.     imagedestroy($ground_im); 

第二步:找到$json = new Services_JSON();注意有兩個地方,不是在alert函數里的那個,添加如下代碼:
 

  1. /* 水印配置開始 */ 
  2. require_once $_SERVER['DOCUMENT_ROOT'].'/config.php';//獲取水印配置參數 
  3. if($GLOBALS["wl_water"]["action"]==true){ 
  4.   $water_mark = 1;//1為加水印, 其它為不加 
  5.   $water_pos = 9;//水印位置,10種狀態【0為隨機,1為頂端居左,2為頂端居中,3為頂端居右;4為中部居左,5為中部居中,6為中部居右;7為底端居左,8為底端居中,9為底端居】 
  6.   $water_img = $_SERVER['DOCUMENT_ROOT'].'/upload/water/water.jpg';//水印圖片,默認填寫空,請將圖片上傳至網站根目錄的upfiles下,例: water.gif  
  7.   $water_alpha = 50;//水印透明度 
  8.   $water_text = '';//水印字符串,默認填寫空; 
  9.   //$water_fontfile = $_SERVER['DOCUMENT_ROOT'] .'/upfiles/fonts/arial.ttf';//文字水印使用的字體; 
  10.   $min_image_w=$GLOBALS["wl_water"]["min_image_w"];//水印啟用最小寬度 
  11.   $min_image_h=$GLOBALS["wl_water"]["min_image_h"];//水印啟用最小高度 
  12.   if($water_mark == 1){ 
  13.       imageWaterMark($file_path$water_pos$water_img$water_alpha$water_text$water_fontfile,$min_image_w,$min_image_h); 
  14.   } 
  15.    /* 水印配置結束 */ 

親自測試,可以用的,歡迎大家留言,我主要測試了圖片水印,文字水印我不需要,所以就沒測試呢

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 羞羞视频入口网站 | 欧美日韩乱码毛片免费观看 | 伊人久久大香线蕉观看 | 国产九九精品 | 欧美人与动性xxxxx杂性 | 新天堂网| 欧美性小说 | 国内精品一区二区三区αv 国内精品一区二区三区东京 | 手机看片一区 | 亚洲在线观看免费 | 亚洲精品视频在线免费 | 亚洲女人毛片 | 伊人网在线免费视频 | 精彩视频一区二区三区 | 国内精品久久久久久中文字幕 | 欧美激情精品久久久久 | 久久不色| 欧美日韩一区二区三区麻豆 | 亚洲精品一区二区三区中文字幕 | 成人免费精品视频 | 一区小说二区另类小说三区图 | 亚洲小说春色综合另类网蜜桃 | 欧美精品国产一区二区三区 | 亚洲久久天堂 | 操你综合 | 欧美一区二区三区国产精品 | 久草久爱 | 亚洲欧洲国产综合 | 亚洲成人在线播放 | 黄色免费在线网址 | 日韩欧美视频在线一区二区 | 高清一级毛片免免费看 | 亚洲黄色中文字幕 | 男女午夜视频 | 亚洲性另类 | 在线播放免费人成毛片乱码 | 最近最新中文字幕大全高清6 | 毛片大片 | 一级毛片不卡片免费观看 | 国产成人综合久久 | 亚洲a色|