关于微信小程序生成海报一个简单的办法

这篇具有很好参考价值的文章主要介绍了关于微信小程序生成海报一个简单的办法。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

废话不多说,直接入题,先上GITHUB地址,这个组件很好用,有图形生成工具,你不用再自己写代码一个个元素对齐了,是不是很爽。

GITHUB:https://github.com/Kujiale-Mobile/Painter

生成painter代码:

​​​​​​​React App 这个很好用吧,自己把设计变成海报,然后复制出代码

微信小程序生成海报,微信小程序,小程序

充分利用painter的优势。小程序生成图片库,轻松通过 json 方式绘制一张可以发到朋友圈的图片>>Painter

下载源代码

这个也简单,先下载源代码,然后小程序根目录里建一个components文件夹,把程序代码贴进去,当然你可以用GIT命令,省去手动下载的麻烦。

git clone https://github.com/Kujiale-Mobile/Painter.git

新建一个生成海报的小程序页面,引入这个组件微信小程序生成海报,微信小程序,小程序

"usingComponents":{
  "painter":"/components/painter/painter"
}

生成海报代码:

微信小程序生成海报,微信小程序,小程序

<painter customStyle='position: absolute; left: -9999rpx;' palette="{{template}}" bind:imgOK="onImgOK" widthPixels="1000"/>

数据传入后,则会自动进行绘图。绘图完成后,你可以通过绑定 imgOK 或 imgErr 事件来获得成功后的图片 或失败的原因。

bind:imgOK="onImgOK"
bind:imgErr="onImgErr"

onImgOK(e) {
  其中 e.detail.path 为生成的图片路径
},

第一步生成的海报代码可以放进页面,然后修改图片地址为动态的即可。

微信小程序生成海报,微信小程序,小程序


palette() {
        return (
{
  "width": "654px",
  "height": "1000px",
  "background": "#FEF8F3",
  "views": [
    {
      "type": "qrcode",
      "content": "哈哈哈",
      "css": {
        "color": "#000000",
        "background": "#ffffff",
        "width": "200px",
        "height": "200px",
        "top": "779px",
        "left": "29px",
        "rotate": "0",
        "borderRadius": "10px"
      }
    },
    {
      "type": "text",
      "text": "扫码关注",
      "css": {
        "color": "#ff0000",
        "background": "rgba(0,0,0,0)",
        "width": "200px",
        "height": "43.89999999999999px",
        "top": "2px",
        "left": "1113px",
        "rotate": "0",
        "borderRadius": "",
        "borderWidth": "1px",
        "borderColor": "#000000",
        "shadow": "10 10 5 #888888",
        "padding": "0px",
        "fontSize": "30px",
        "fontWeight": "normal",
        "maxLines": "2",
        "lineHeight": "43.290000000000006px",
        "textStyle": "fill",
        "textDecoration": "none",
        "fontFamily": "",
        "textAlign": "left"
      }
    },
    {
      "type": "text",
      "text": "扫码关注",
      "css": {
        "color": "#ff0000",
        "background": "rgba(0,0,0,0)",
        "width": "200px",
        "height": "43.89999999999999px",
        "top": "2px",
        "left": "1113px",
        "rotate": "0",
        "borderRadius": "",
        "borderWidth": "1px",
        "borderColor": "#000000",
        "shadow": "10 10 5 #888888",
        "padding": "0px",
        "fontSize": "30px",
        "fontWeight": "normal",
        "maxLines": "2",
        "lineHeight": "43.290000000000006px",
        "textStyle": "fill",
        "textDecoration": "none",
        "fontFamily": "",
        "textAlign": "left"
      }
    },
    {
      "type": "text",
      "text": "扫码关注",
      "css": {
        "color": "#C87756",
        "background": "rgba(0,0,0,0)",
        "width": "30px",
        "height": "175.10999999999996px",
        "top": "787.94px",
        "left": "254px",
        "rotate": "0",
        "borderRadius": "",
        "borderWidth": "",
        "borderColor": "#000000",
        "shadow": "",
        "padding": "0px",
        "fontSize": "30px",
        "fontWeight": "normal",
        "maxLines": "5",
        "lineHeight": "43.290000000000006px",
        "textStyle": "fill",
        "textDecoration": "none",
        "fontFamily": "",
        "textAlign": "left"
      }
    },
    {
      "type": "text",
      "text": "1. 忌食辛辣刺激食物\n2. 忌运动量过大\n3. 戒烟戒酒,忌暴饮暴食",
      "css": {
        "color": "#C87756",
        "background": "rgba(0,0,0,0)",
        "width": "250px",
        "height": "93.88px",
        "top": "611px",
        "left": "40px",
        "rotate": "0",
        "borderRadius": "",
        "borderWidth": "",
        "borderColor": "#000000",
        "shadow": "",
        "padding": "0px",
        "fontSize": "20px",
        "fontWeight": "normal",
        "maxLines": "6",
        "lineHeight": "31.080000000000002px",
        "textStyle": "fill",
        "textDecoration": "none",
        "fontFamily": "",
        "textAlign": "left"
      }
    },
    {
      "type": "text",
      "text": "养生小贴士",
      "css": {
        "color": "#C87756",
        "background": "rgba(0,0,0,0)",
        "width": "200px",
        "height": "42.89999999999999px",
        "top": "537px",
        "left": "42px",
        "rotate": "0",
        "borderRadius": "",
        "borderWidth": "",
        "borderColor": "#C87756",
        "shadow": "",
        "padding": "0px",
        "fontSize": "30px",
        "fontWeight": "bold",
        "maxLines": "2",
        "lineHeight": "43.290000000000006px",
        "textStyle": "fill",
        "textDecoration": "none",
        "fontFamily": "webfontzk",
        "textAlign": "left"
      }
    },
    {
      "type": "text",
      "text": "分",
      "css": {
        "color": "#C87756",
        "background": "rgba(0,0,0,0)",
        "width": "130px",
        "height": "185.89999999999998px",
        "top": "708px",
        "left": "484px",
        "rotate": "0",
        "borderRadius": "",
        "borderWidth": "",
        "borderColor": "#000000",
        "shadow": "",
        "padding": "0px",
        "fontSize": "130px",
        "fontWeight": "normal",
        "maxLines": "2",
        "lineHeight": "187.59000000000003px",
        "textStyle": "fill",
        "textDecoration": "none",
        "fontFamily": "webfontzkxw",
        "textAlign": "left"
      }
    },
    {
      "type": "text",
      "text": "秋",
      "css": {
        "color": "#C87756",
        "background": "rgba(0,0,0,0)",
        "width": "130px",
        "height": "185.89999999999998px",
        "top": "580px",
        "left": "463.5px",
        "rotate": "0",
        "borderRadius": "",
        "borderWidth": "",
        "borderColor": "#000000",
        "shadow": "",
        "padding": "0px",
        "fontSize": "130px",
        "fontWeight": "normal",
        "maxLines": "2",
        "lineHeight": "187.59000000000003px",
        "textStyle": "fill",
        "textDecoration": "none",
        "fontFamily": "webfontzkxw",
        "textAlign": "left"
      }
    },
    {
      "type": "image",
      "url": "https://static.fotor.com.cn/assets/res/pic/454d3884-b1d1-4059-a08b-18427f93c8eb.jpg?x-oss-process=image/resize,lfit,h_1200,w_1200/format,src",
      "css": {
        "width": "568.01px",
        "height": "475.27px",
        "top": "0px",
        "left": "39.19px",
        "rotate": "0",
        "borderRadius": "",
        "borderWidth": "",
        "borderColor": "#000000",
        "shadow": "",
        "mode": "scaleToFill"
      }
    },
    {
      "type": "text",
      "text": "请朋友们注意防寒保暖,小心着凉",
      "css": {
        "color": "#fff",
        "background": "rgba(0,0,0,0)",
        "width": "22px",
        "height": "372.9000000000001px",
        "top": "61px",
        "left": "71px",
        "rotate": "0",
        "borderRadius": "",
        "borderWidth": "",
        "borderColor": "#000000",
        "shadow": "",
        "padding": "0px",
        "fontSize": "22px",
        "fontWeight": "normal",
        "maxLines": "22",
        "lineHeight": "24.42px",
        "textStyle": "fill",
        "textDecoration": "none",
        "fontFamily": "",
        "textAlign": "left"
      }
    }
  ]
}
        );
      }

关于如何生成小程序码

请看小程序文档,写的很清楚了,先获取TOKEN然后调用接口即可生成,把图片保存服务器生成小程序码的URL

文档地址:获取不限制的小程序码 | 微信开放文档

给一个PHP的代码,仅供参考

<?php
	header("Content-Type: text/html; charset=utf-8");
	function test($code){  //code为参数
	    	$appid = 'xxx';
	    	$secret = 'xxxx';
	    	$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
	        //开启session
	        session_start(); 
	        // 保存2小时 
	        $lifeTime = 2 * 3600; 
	        setcookie(session_name(), session_id(), time() + $lifeTime, "/"); 
	        // echo $url;
	        $access_token = $_SESSION['access_token'];
	        if(empty($access_token)){
	            $access_token_data = getJson($url);
	            $access_token = $access_token_data['access_token'];
	            $_SESSION['access_token'] = $access_token;
	        }
	        if(!empty($access_token)){
	            $url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='.$access_token;
	            $data['path'] = 'pages/data/data?id='.$code;
	            $data['scene'] = 'type=qrcode';//(string类型,必须是数字)
	            $data['width'] = 430;
	            $result = curlPost($url,$data,'POST');
	            // p($result);
				$filename = md5($code);  //文件名
	            $ret = file_put_contents('./uploads/'.$filename.'.png', $result, true);
				$path='./uploads/'.$filename.'.png';
				var_dump($path);
	            echo '成功';
	        }else{
	            echo 'string';
	        }
	 }
	function getJson($url,$data=array(),$method='GET'){
	        $ch = curl_init();//1.初始化  
	        curl_setopt($ch, CURLOPT_URL, $url);//2.请求地址  
	        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式  
	        //4.参数如下  
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
	        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
	        if($method=="POST"){//5.post方式的时候添加数据  
	            $data = json_encode($data);
	            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
	        }  
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	        $output = curl_exec($ch);
	        curl_close($ch);
	        return json_decode($output, true);
	}
	
	function curlPost($url,$data,$method){
	        $ch = curl_init();   //1.初始化  
	        curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址  
	        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式  
	        //4.参数如下  
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https  
	        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
	        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器  
	        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
	        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);  
	            curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容  
	            curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');  
	          
	        if($method=="POST"){//5.post方式的时候添加数据  
	            $data = json_encode($data);
	            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
	        }  
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
	        $tmpInfo = curl_exec($ch);//6.执行  
	      
	        if (curl_errno($ch)) {//7.如果出错  
	            return curl_error($ch);  
	        }  
	        curl_close($ch);//8.关闭  
	        return $tmpInfo;  
	}  
	test(12);
?>

保存海报分享

另外,生成的海报如果保存一键转发,小程序文档也有相应的接口,直接可以使用,不过请注意你开发工具的版本要高于2.16.0

地址也发下吧:wx.showShareImageMenu(Object object) | 微信开放文档

微信小程序生成海报,微信小程序,小程序

 微信小程序生成海报,微信小程序,小程序

至此,完成了小程序生成海报分享功能。 文章来源地址https://www.toymoban.com/news/detail-813421.html

到了这里,关于关于微信小程序生成海报一个简单的办法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 微信小程序实现画布生成海报功能

    微信小程序可以通过使用 标签来实现生成海报的功能。以下是基本实现步骤: 1.在 WXML 文件中创建一个 标签,并设置其宽度和高度属性。 ’ 2.在 JS 文件中,获取到 标签的上下文对象。 3.在 ctx 上下文对象中,使用各种绘图 API 绘制出海报的内容 4.最后调用 ctx.draw() 方法将海

    2024年02月16日
    浏览(46)
  • 微信小程序生成海报工具Painter

    由于我们无法将小程序直接分享到朋友圈,但分享到朋友圈的需求又很多,业界目前的做法是利用小程序的 Canvas 功能生成一张带有二维码的图片,然后引导用户下载图片到本地后再分享到朋友圈。而小程序 Canvas 功能是很难用的,往往为了绘制一张简单图片,就得写上一堆

    2024年02月09日
    浏览(78)
  • 微信小程序 api+前端实现生成分享海报

    1.先看效果图,点击分享海报按钮,然后弹出分享海报  2.前端代码 这里用的组件有vant组件库还有canvas_drawer(一个画布组件) canvas_drawer下载地址 https://github.com/kuckboy1994/mp_canvas_drawer 把 components 中的 canvasdrawer 拷贝到自己项目下,然后再app.json中引用就行了,如下 \\\"usingCompon

    2024年02月09日
    浏览(47)
  • 微信小程序生成二维码海报并分享

    背景:点击图标,生成海报后,点击保存相册,可以保存 生成海报:插件wxa-plugin-canvas,此处使用页面异步生成组件方式,官网地址:wxa-plugin-canvas - npm 二维码:调用后端接口生成二维码 需要调用获取图片信息接口wx.getImageInfo(),获取到图片的宽高以做整体宽高配置 closePos

    2024年03月21日
    浏览(64)
  • uniapp微信小程序生成分享海报(模板自取)

    uniapp微信小程序生成分享海报模板 1、模板自取 2、可自行按需求更改调整 3、效果图如下: 生成前  ----- 生成后的图 需知: 博主的实现效果是先把需要生成的图片排版成静态页面,再点击生成海报-----通过canvas生成海报!!!            不需要这样效果话可以省略第一步

    2024年02月14日
    浏览(46)
  • 微信小程序使用canvas生成分享海报功能复盘

    近期需要开发一个微信小程序生成海报分享的功能。在h5一般都会直接采用 html2canvas 或者 dom2image 之类的库直接处理。但是由于小程序不具备传统意义的dom元素,所以也没有办法采用此类工具。 所以就只能一笔一笔的用 canvas 画出来了,下面对实现这个功能中遇到的问题做一

    2024年02月16日
    浏览(58)
  • 微信小程序插件 painter 生成海报、二维码

    GitHub 地址:https://github.com/Kujiale-Mobile/Painter 一、引入 将 components/painter 整个文件放到自己项目的 components 中;以组件的形式在页面的 json 文件中引入; 二、使用 3、注意 1、使用网络图片的时候需要在小程序后台配置 request、uploadFile、downloadFile 合法域名,否则线上版本不显示

    2024年02月15日
    浏览(44)
  • Java生成微信小程序二维码,5种实现方式,一个比一个简单

    先介绍一下 项目场景 ,主要是通过微信小程序二维码 裂变分享 ,每个账号有专属邀请二维码,分享出去,有新人扫码入驻,就可以得到现金奖励或红包奖励。当然,产品设计会更丰富,不止有裂变模式,还有渠道推广模式,还有各种奖励规则,但核心实现都是生成二维码。

    2024年02月03日
    浏览(41)
  • uniapp 微信小程序 动态生成海报分享朋友圈,需先保存图片

    直接拷贝代码,不使用插件,自己纯代码实现。 从相册 或拍照 选择一个图片做海报背景。 大吉大利 今晚吃鸡。

    2024年02月11日
    浏览(79)
  • 微信小程序使用canvas画布生成二维码海报分享图片(完整示例代码)

    canvas.js //获取应用实例 const app = getApp() Page({ /** 页面的初始数据 */ data: { // canvas _width: 0, //手机屏宽 _heigth: 0,//手机屏高 swiperHeight: 300,//主图图片高度 canvasType: false,//canvas是否显示 loadImagePath: ‘’,//下载的图片 imageUrl: ‘https://cos.myfaka.com/car/service/1.jpg’, //主图网络路径 codeU

    2024年04月12日
    浏览(79)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包