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;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值