php圖片加水印

<?php
header("Content-Type: application/octet-stream");

uploadimg($userid, $img,$type);

function uploadimg($userid,$img,$type){
if($userid==0 || $type==0){
$array = array('code'=>'3','message'=>'參數不合法');
echo json_encode($array);
exit;
}else{
$byte = $img;
//轉成二進制數據流
  $byte = str_replace(' ','',$byte);   //處理數據 
    $byte = str_ireplace("<",'',$byte);
    $byte = str_ireplace(">",'',$byte);
    $byte=pack("H*",$byte);  
    if($type==1){
$filename="header.jpg";//頭像
    }
if($type==2){
$filename="self_1.jpg";//身份證正面
    }
if($type==3){
$filename="self_2.jpg";//身份證正面
    }
$jpg = $byte;//得到post過來的二進制原始數據
$path=ROOT_PATH."images/".$userid;   
if(!file_exists($path)) 

mkdir($path,0777); 
}
$file = fopen($path.'/'.$filename,"w");//打開文件準備寫入
fwrite($file,$jpg);//寫入
fclose($file);//關閉
$path2 = DOOCARE."images/".$userid.'/'.$filename;
if($type==2||$type==3){
$dst_path = $path.'/'.$filename;
$dst = imagecreatefromstring(file_get_contents($dst_path));
/*imagecreatefromstring()--從字符串中的圖像流新建一個圖像,返回一個圖像標示符,其表達了從給定字符串得來的圖像
圖像格式將自動監測,只要php支持jpeg,png,gif,wbmp,gd2.*/
$font = ROOT_PATH."images/".'verdana.ttf';
//$black = imagecolorallocate($dst, 255, 255, 255);
$black = imagecolorallocatealpha ($dst, 255 , 255, 255 , 90);
$arr = getimagesize($dst_path);
$width = $arr[0];//寬度
$height = $arr[1];//高度
$width_p = intval($width)*intval($width);
$height_p = intval($height)*intval($height);
$long = ceil(sqrt(intval($width_p)+intval($height_p)));
$long = $long/6;
//imagefttext($dst, 20, 45,60, 60, $black, $font, 'Doocare');
imagefttext($dst, $long, 45,80, $height, $black, $font, 'Doocare');
//imagefttext($dst, 15, 45, 10, 65, $black, $font, 'Doocare');
/*imagefttext($img,$size,$angle,$x,$y,$color,$fontfile,$text)
$img由圖像創建函數返回的圖像資源
size要使用的水印的字體大小
angle(角度)文字的傾斜角度,如果是0度代表文字從左往右,如果是90度代表從上往下
x,y水印文字的第一個文字的起始位置
color是水印文字的顏色
fontfile,你希望使用truetype字體的路徑*/
list($dst_w,$dst_h,$dst_type) = getimagesize($dst_path);
/*list(mixed $varname[,mixed $......])--把數組中的值賦給一些變量
像array()一樣,這不是真正的函數,而是語言結構,List()用一步操作給一組變量進行賦值*/
/*getimagesize()能獲取到什麼信息?
getimagesize函數會返回圖像的所有信息,包括大小,類型等等*/
switch($dst_type){
   case 1://GIF
       header("content-type:image/gif");
       imagegif($dst,$dst_path);
       break;
   case 2://JPG
       header("content-type:image/jpeg");
       imagejpeg($dst,$dst_path);
       break;
   case 3://PNG
       header("content-type:image/png");
       imagepng($dst,$dst_path);
       break;
   default:
       break;
   /*imagepng--以PNG格式將圖像輸出到瀏覽器或文件
   imagepng()將GD圖像流(image)以png格式輸出到標註輸出(通常爲瀏覽器),或者如果用filename給出了文件名則將其輸出到文件*/
}
imagedestroy($dst);
}
$is_success = file_exists($path.'/'.$filename);
if($is_success){
if($type==1){
$array = array('code'=>1,'img'=>$path2,'type'=>$type,'message'=>'成功');
}else if($type==2||$type==3){
$array = array('code'=>1,'message'=>'成功');
}
}else{
$array = array('code'=>2,'message'=>'失敗');
}
echo json_encode($array);
exit;
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章