|
仔細(xì)看了小程序本身的程序,沒有發(fā)現(xiàn)好的方法。
<?php //初始數(shù)據(jù)準(zhǔn)備
define('PATH', "/opt/*******/wxaapp/".date("Y/m/d/")."/".rand(1,50)."/");
include_once('/opt/*****/function.php');
$path = $dir.date("Y/m/d/")."/".rand(1,50)."/";
create_dirs(PATH,0777);
/**
*獲取二維碼程序($appid,$secret,access_token 一般另外處理,存到緩存!這里為了看的方便就放這里了)
*/
$appid='****';
$secret='****';
$sid = intval($_POST['sid']); //需要放在小程序碼里的參數(shù)
$avatarUrl = $_POST['avatarUrl']; //傳入后臺原頭像(1080*1080,網(wǎng)絡(luò)地址) if(!$sid || !$avatarUrl){
$arr = array(
'ret'=>0,
'msg'=>'參數(shù)錯誤');
echo json_encode($arr);
die();
}
//保存原始頭像
$img_file = file_get_contents($avatarUrl);
$img_content= base64_encode($img_file);
$file_tou_name = time().".png";
$headurl = PATH.$file_tou_name;
file_put_contents($headurl,base64_decode($img_content));
//獲取token(一般放在緩存!!盡量不要每次都要去請求一次。一天2000次)
$url_access_token = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
$json_access_token = sendCmd($url_access_token,array());
$arr_access_token = json_decode($json_access_token,true);
$access_token = $arr_access_token['access_token'];
//獲取二維碼 if(!empty($access_token)) {
$url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='.$access_token;
$data = '{"path": "/pages*****index?id='.$sid.'", "width":430}';
$result = sendCmd($url,$data);
$file_code_name = "21".time().".png";
file_put_contents(PATH.$file_code_name,$result);
} else {
$arr = array('ret'=>0,'msg'=>'ACCESS TOKEN為空!');
}
//header("content-type:image/png"); //傳入保存后的頭像文件名
$imgg = yuan_img($headurl);
$file_name = "22".time().".png";
imagepng($imgg,PATH.$file_name);
imagedestroy($imgg);
$target_im = imagecreatetruecolor(192,192); //創(chuàng)建一個新的畫布(縮放后的),從左上角開始填充透明背景
imagesavealpha($target_im, true);
$trans_colour = imagecolorallocatealpha($target_im, 0, 0, 0, 127);
imagefill($target_im, 0, 0, $trans_colour);
$o_image = imagecreatefrompng(PATH.$file_name); //獲取原圖的內(nèi)容
imagecopyresampled($target_im,$o_image, 0, 0,0, 0, 192, 192, 1080, 1080);
$file_head_name = "23".time().".png";
$comp_path =PATH.$file_head_name;
imagepng($target_im,$comp_path);
imagedestroy($target_im);
//傳入保存后的二維碼地址
$url = create_pic_watermark(PATH.$file_code_name,$comp_path,"center");
$arr = array('ret'=>1,
'msg'=>'success',
'data'=>array('url'=>$url),
);
echo json_encode($arr);
/**
* [create_pic_watermark 添加圖片水印] 頭像貼在二維碼中間
* @param [string] $dest_image [需要添加圖片水印的圖片名]
* @param [string] $watermark [水印圖片名]
* @param [string] $locate [水印位置,center,left_buttom,right_buttom三選一]
* @return [type] [description]
*/ function create_pic_watermark($dest_image,$watermark,$locate){
list($dwidth,$dheight,$dtype)=getimagesize($dest_image);
list($wwidth,$wheight,$wtype)=getimagesize($watermark);
$types=array(1 => "GIF",2 => "JPEG",3 => "PNG",
4 => "SWF",5 => "PSD",6 => "BMP",
7 => "TIFF",8 => "TIFF",9 => "JPC",
10 => "JP2",11 => "JPX",12 => "JB2",
13 => "SWC",14 => "IFF",15 => "WBMP",16 => "XBM");
$dtype=strtolower($types[$dtype]);//原圖類型
$wtype=strtolower($types[$wtype]);//水印圖片類型
$created="imagecreatefrom".$dtype;
$createw="imagecreatefrom".$wtype;
$imgd=$created($dest_image);
$imgw=$createw($watermark);
switch($locate){
case 'center':
$x=($dwidth-$wwidth)/2;
$y=($dheight-$wheight)/2;
break;
case 'left_buttom':
$x=1;
$y=($dheight-$wheight-2);
break;
case 'right_buttom':
$x=($dwidth-$wwidth-1);
$y=($dheight-$wheight-2);
break;
default:
die("未指定水印位置!");
break;
}
imagecopy($imgd,$imgw,$x,$y,0,0, $wwidth,$wheight);
$save="image".$dtype;
//保存到服務(wù)器
$f_file_name = "24".time().".png";
imagepng($imgd,PATH.$f_file_name); //保存
imagedestroy($imgw);
imagedestroy($imgd);
//傳回處理好的圖片
$url = 'https://www.q***ei.com/'.str_replace('/opt/********by/','',PATH.$f_file_name);
return $url;
}
/**
* [yuan_img 編輯圖片為圓形] 剪切頭像為圓形
* @param [string] $imgpath [頭像保存之后的圖片名]
*/ function yuan_img($imgpath) {
$ext = pathinfo($imgpath);
$src_img = null;
switch ($ext['extension']) {
case 'jpg':
$src_img = imagecreatefromjpeg($imgpath);
break;
case 'png':
$src_img = imagecreatefromjpeg($imgpath);
break;
}
$wh = getimagesize($imgpath);
$w = $wh[0];
$h = $wh[1];
$w = min($w, $h);
$h = $w;
$img = imagecreatetruecolor($w, $h);
//這一句一定要有
imagesavealpha($img, true);
//拾取一個完全透明的顏色,最后一個參數(shù)127為全透明
$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefill($img, 0, 0, $bg);
$r = $w / 2; //圓半徑
$y_x = $r; //圓心X坐標(biāo)
$y_y = $r; //圓心Y坐標(biāo) for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
$rgbColor = imagecolorat($src_img, $x, $y);
if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
}
}
return $img;
}
/**
* 發(fā)起請求
* @param string $url 請求地址
* @param string $data 請求數(shù)據(jù)包
* @return string 請求返回數(shù)據(jù)
*/ function sendCmd($url,$data) {
$curl = curl_init(); // 啟動一個CURL會話
curl_setopt($curl, CURLOPT_URL, $url); // 要訪問的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 對認(rèn)證證書來源的檢測
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 從證書中檢查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:')); //解決數(shù)據(jù)包大不能提交
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自動跳轉(zhuǎn)
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自動設(shè)置Referer
curl_setopt($curl, CURLOPT_POST, 1); // 發(fā)送一個常規(guī)的Post請求
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的數(shù)據(jù)包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 設(shè)置超時限制防止死循
curl_setopt($curl, CURLOPT_HEADER, 0); // 顯示返回的Header區(qū)域內(nèi)容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 獲取的信息以文件流的形式返回
$tmpInfo = curl_exec($curl); // 執(zhí)行操作 if (curl_errno($curl)) {
echo 'Errno'.curl_error($curl);
}
curl_close($curl); // 關(guān)鍵CURL會話 return $tmpInfo; // 返回數(shù)據(jù)
}
?>
|