- /*PHP圖片加文字水印類庫
-
-
- 這類庫暫時只支援文字水印,位置為文字水印,位置為右下角,顏色隨機
-
- 呼叫方法:
- 1、在需要加浮水印的檔案頂部引入類別庫:
- include_once 'imageClass.php';
- 2、宣告新類別:
- $tpl=new image_fu;
- 3、給圖片浮水印提供參數:
- $tpl->img(圖片路徑,水印文字,字體路徑,字體大小,字體角度);
- 例如:$ tpl->img('abc.jpg','這是水印文字','ziti.ttf',30,0)
-
- */
-
- class image_fu{
-
- private $image;
- private $img_info;
- private $img_width;
- private $img_height;
- private $img_im;
- private$img_text;
- private $img_new;
- private $img_text_size;
- private $img_jd;
-
-
- function img($img='',$$='',$ttf='' ,$size=12,$jiaodu=0){
- if(isset($img)&&file_exists($img)){//偵測圖片是否存在
- $this->image =$img;
- $this->img_text=$txt;
- $this->img_text_size=$size;
- $this->img_jd=$jiaodu;
- if(file_exists($ttf)){
- $this ->img_ttf=$ttf;
- }else{
- exit('字型檔:'.$ttf.'不存在!');
- }
- $this->imgyesno();
- }else{
- exit('圖片檔案:'.$img.'不存在');
- }
- }
-
- private function imgyesno(){
-
-
- $this->img_info =getimagesize($this->image);
- $this->img_width =$this->img_info[0];//圖片寬
- $this->img_height=$this- >img_info[1];//圖片高
-
- //偵測圖片類型
- switch($this->img_info[2]){
- case 1:$this->img_im = imagecreatefromgif( $this->image);break;
- case 2:$this->img_im = imagecreatefromjpeg($this->image);break;
- case 3:$this->img_im = imagecreatefrompng($this-> image);break;
- default:exit('圖片格式不支援浮水印');
- }
-
- $this->img_text();
- }
-
-
- private function img_text(){
-
- imagealphablending($this->img_im,true);
-
- //設定顏色
- $color=imagecolor. 255,255,255);
- $txt_height=$this->img_text_size;
- $txt_jiaodu=$this->img_jd;
- $ttf_im=imagettfbbox($web_height,$ >img_text);
- $w = $ttf_im[2] - $ttf_im[6];
- $h = $ttf_im[3] - $ttf_im[7];
- //$w = $ttf_im [7];
- //$h = $ttf_im[8];
-
- unset($ttf_im);
-
- $txt_y =$this->img_height*0.5;
- $txt_x =$this->img_width*0.2;
- //$txt_y =0;
- //$txt_x =0;
-
-
-
- $this->img_new=@@@ imagettftext($this->img_im,$txt_height,$txt_jiaodu,$txt_x,$txt_y,$color,$this->img_ttf,$this->img_text);
-
- @unlink($this->img_text);
-
- @unlink($this->image );//刪除圖片
- switch($this->img_info[2]) {//取得背景圖片的格式
- case 1:imagegif($this->img_im,$this->image); ;
- case 2:imagejpeg($this->img_im,$this->image);break;
- case 3:imagepng($this->img_im,$this->image);break;
- break;
- break; default: exit('浮水印圖片失敗');
- }
-
- }
-
-
- //顯示圖片
- function img_show(){echo '';}
-
- //釋放內存
- private function img_nothing( ){
- unset($this->img_info);
- imagedestroy($this->img_im);
- }
-
} ?>
複製程式碼
-
/**
- * 由 JetBrains PhpStorm 建立。
- * 使用者:taoqili
- * 日期:12-7-18
- * 時間:上午10:42
- */
- header("Content-Type: text/html; charset=utf-8");
- error_reporting(E_ERROR | E_WARNING);
- include "Uploader.class.php";
- //上傳圖片框中的描述表單名稱,
- $title = htmlspecialchars($_POST['pictitle'], ENT_QUOTES);
- $path = htmlspecialchars($_POST['dir'], ENT_QUOTES);
-
- //上傳設定
- $config = array(
- "savePath" //上傳設定
- $config = array(
- "savePath" = => path == "1" ? "upload/" : "upload1/"),
- "maxSize" => 1000, //單位KB
- "allowFiles" => array(".gif", ".png ", ".jpg", ".jpeg", ".bmp")
- );
-
- //產生上傳實例物件並完成上傳
- $up = new Uploader("upfile", $config);
-
- /**
- * 得到上傳檔案所對應的各個參數,陣列結構
- * array(
- * "originalName" => "", //原始檔案名稱
- * "name" => "", //新檔案名稱
- * "url" => "", //傳回的位址
- * "size" => "", //檔案大小
- * "type" => "" , //檔案類型
- * "state" => "" //上傳狀態,上傳成功時必須回傳"SUCCESS"
- * )
- */
- $info = $up->getFileInfo();
-
- /**
- * 向瀏覽器傳回資料json資料
- * {
- * 'url' :'a.jpg', //儲存後的檔案路徑
- * 'title' :'hello', / /檔案描述,對圖片來說在前端會加入到title屬性上
- * 'original' :'b.jpg', //原始檔名
- * 'state' :'SUCCESS' //上傳狀態,成功時返回SUCCESS,其他任何值將原樣返回至圖片上傳框
- * }
- */
-
- /* include "imgwater.php";
- $tpl=new image_fu;
- $tpl->img($info['url'],'http://www.zgxbzh.com/', 'simsun.ttc',30,0);*/
-
- echo "{'url':'" . $info["url"] . "','title':'" . $title . "','original':'" . $info["originalName"] . "','state':'" . $info["state"] . "'}";
-
複製程式碼 |