千家信息网

微信小程序如何改变小程序码中间的logo

发表于:2024-10-22 作者:千家信息网编辑
千家信息网最后更新 2024年10月22日,本篇内容介绍了"微信小程序如何改变小程序码中间的logo"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成
千家信息网最后更新 2024年10月22日微信小程序如何改变小程序码中间的logo

本篇内容介绍了"微信小程序如何改变小程序码中间的logo"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

所以改变方法,把头像传回后台,使用 php gd库在后台操作,然后传回小程序端。

//初始数据准备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);  //小程序传的头像是网络地址需要周转一下$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的方法(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);              //创建一个新的画布(缩放后的),从左上角开始填充透明背景   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);   //获取上文已保存的修改之后头像的内容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);

五.所有准备条件都好了。进行拼接。(使用加水印方式把处理过后的头像盖住logo)

//传入保存后的二维码地址$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;    //保存到服务器    $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);        //拾取一个完全透明的颜色,最后一个参数127为全透明        $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);        imagefill($img, 0, 0, $bg);        $r   = $w / 2; //圆半径        $y_x = $r; //圆心X坐标        $y_y = $r; //圆心Y坐标        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;}

网络请求:

/** * 发起请求 * @param  string $url  请求地址 * @param  string $data 请求数据包 * @return   string      请求返回数据 */function sendCmd($url,$data){    $curl = curl_init(); // 启动一个CURL会话          curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址                      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在          curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:')); //解决数据包大不能提交         curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转          curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer          curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求          curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包          curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循         curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容          curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回                $tmpInfo = curl_exec($curl); // 执行操作          if (curl_errno($curl)) {             echo 'Errno'.curl_error($curl);          }          curl_close($curl); // 关键CURL会话          return $tmpInfo; // 返回数据      }

"微信小程序如何改变小程序码中间的logo"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

0