1、传入参数,动态生成小程序,保存到服务器;
2、读取服务器保存的小程序码,读取一个背景图片;
3、合并成一个新图片
public function getWxcode($ticket_id,$lss_id,$tls_id,$qiantai,$width=430){
if(empty($ticket_id)||empty($lss_id)){
echo 0;die;
}
$ACCESS_TOKEN=$this->getWxAccessToken();
$url="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$ACCESS_TOKEN['access_token'];
$post_data=array();
$post_data['scene'] = "t=" . $ticket_id."&lss=".$lss_id."&tls=".$tls_id."&q=".$qiantai;
$post_data['page'] = "";
$post_data['width'] = $width;
$post_data['is_hyaline'] = true;
$post_data=json_encode($post_data);
$data=$this->send_post($url,$post_data);
$result=$this->data_uri($data,'image/png');
// echo '';
return $result;
}
/* 消息推送http
* @param $url
* @param $post_data
* @return bool|string
*/
protected function send_post( $url, $post_data ) {
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/json',
//header 需要设置为 JSON
'content' => $post_data,
'timeout' => 60
//超时时间
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
return $result;
}
//二进制转图片image/png
public function data_uri($contents, $mime)
{
$base64 = base64_encode($contents);
return ('data:' . $mime . ';base64,' . $base64);
}
public function getWxAccessToken(){
// $appid='****************';
// $appsecret='*****************';
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
$access_token = $this->makeRequest($url);
$access_token = json_decode($access_token['result'],true);
return $access_token;
}
function fenxiang_img(){
$ticket_id = isset($_REQUEST['ticket_id']) ? intval($_REQUEST['ticket_id']) : 0 ;
$lss_id = isset($_REQUEST['lss_id']) ? intval($_REQUEST['lss_id']) : 0 ;
$tls_id = isset($_REQUEST['tls_id']) ? intval($_REQUEST['tls_id']) : 0 ;
$qiantai = isset($_REQUEST['q']) ? intval($_REQUEST['q']) : 0 ;
$xcxImg = $this->getWxcode($ticket_id,$lss_id,$tls_id,$qiantai,250);
$imgdata = substr($xcxImg,strpos($xcxImg,",") + 1);
$decodedData = base64_decode($imgdata);
$basePath = './fenxiang/'.$lss_id.'_base_'.$qiantai.'.png';
file_put_contents($basePath,$decodedData );
//图片二
$bgImg = 'http://www.aaaaa.com/xiaochengxu/yikatong/fenxiangBg.png';
//创建图片对象
//imagecreatefrompng($filename)--由文件或 URL 创建一个新图象
$basePathImgInfo=getimagesize($basePath);
$basePathImgType=explode('/',$basePathImgInfo['mime']); //Array ( [0] => 430 [1] => 430 [2] => 2 [3] => width="430" height="430" [bits] => 8 [channels] => 3 [mime] => image/jpeg )
switch($basePathImgType[1]){
case "png":
$image_xcx=imagecreatefrompng($basePath);
break;
case "jpeg":
$image_xcx=imagecreatefromjpeg($basePath);
break;
case "jpg":
$image_xcx=imagecreatefromjpeg($basePath);
break;
}
$image_bg = imagecreatefrompng($bgImg);
//合成图片
//imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )---拷贝并合并图像的一部分
//将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。两图像将根据 pct 来决定合并程度,其值范围从 0 到 100。当 pct = 0 时,实际上什么也没做,当为 100 时对于调色板图像本函数和 imagecopy() 完全一样,它对真彩色图像实现了 alpha 透明。
imagecopymerge($image_bg, $image_xcx, 400, 350, 0, 0, imagesx($image_xcx), imagesy($image_xcx), 100);
$imgName = $lss_id.'_fx_'.$qiantai.'.png';
$bgPathImgInfo=getimagesize($bgImg);
$bgPathImgType=explode('/',$bgPathImgInfo['mime']); //Array ( [0] => 430 [1] => 430 [2] => 2 [3] => width="430" height="430" [bits] => 8 [channels] => 3 [mime] => image/jpeg )
switch($bgPathImgType[1]){
case "png":
imagepng($image_bg,'./fenxiang/'.$imgName);
break;
case "jpeg":
imagejpeg($image_bg,'./fenxiang/'.$imgName);
break;
case "jpg":
imagejpg($image_bg,'./fenxiang/'.$imgName);
break;
}
echo 'http://www.aaaa.com/xiaochengxu/yikatong/fenxiang/'.$imgName;
}