商城系統(tǒng) 注冊

微信小程序改變小程序碼中間logo的方法

2020-09-27|HiShop
導(dǎo)讀:仔細(xì)看了小程序本身的程序,沒有發(fā)現(xiàn)好的方法。 所以改變方法,把頭像傳回后臺,使用 php gd庫在后臺操作,然后傳回小程序端。...

仔細(xì)看了小程序本身的程序,沒有發(fā)現(xiàn)好的方法。

 

所以改變方法,把頭像傳回后臺,使用 php gd庫在后臺操作,然后傳回小程序端。

//初始數(shù)據(jù)準(zhǔn)備
define('PATH', "/opt/************p/".date("Y/m/d/")."/".rand(1,50)."/");
include_once('/op******/function.php');
$path = $dir.date("Y/m/d/")."/".rand(1,50)."/";
create_dirs(PATH,0777);

一.獲取傳入的頭像,并保存到本地。

//保存原始頭像
$img_file = file_get_contents($avatarUrl);  //小程序傳的頭像是網(wǎng)絡(luò)地址需要周轉(zhuǎn)一下
$img_content= base64_encode($img_file);	
$file_tou_name = time().".png";
$headurl = PATH.$file_tou_name;
file_put_contents($headurl,base64_decode($img_content));

二.獲取特定頁面帶參數(shù)的小程序碼并保存。順便再寫一下獲取token的方法(token一般放在緩存中)

//獲取token
$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/answer/index/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);     //yuan_img() 方法在文末會列出
$file_name = "22".time().".png";
imagepng($imgg,PATH.$file_name);
imagedestroy($imgg);

四.縮小頭像(原圖為1080,430的小程序碼logo為192)

$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);

五.所有準(zhǔn)備條件都好了。進(jìn)行拼接。(使用加水印方式把處理過后的頭像蓋住logo)

//傳入保存后的二維碼地址
$url = create_pic_watermark(PATH.$file_code_name,$comp_path,"center");  //方法文末列出
$arr = array('ret'=>1,
		'msg'=>'success',
		'data'=>array('url'=>$url),     //處理完的新小程序碼 保存在服務(wù)器,傳回地址給小程序端即可
		);
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.qubaobei.com/'.str_replace('/opt/ci123/www/html/markets/app2/baby/','',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;
}

網(wǎng)絡(luò)請求:

/**
 * 發(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ù)      
}

微信小程序改變小程序碼中間logo的方法

電話咨詢 預(yù)約演示 0元開店