Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does WeChat Mini Programs change the logo in the middle of Mini Program code?

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how WeChat Mini Programs changes the logo in the middle of Mini Program code". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

So change the method, send the avatar back to the background, use the php gd library to operate in the background, and then send it back to the mini program.

/ / initial data preparation define ('PATH', "/ opt/*p/" .date ("Y/m/d/"). "/" .rand (1mai 50). "/"); include_once (' / op*/function.php'); $path = $dir.date ("Y/m/d/"). "/" .rand (1mai 50). "/"; create_dirs (PATH,0777)

one。 Get the incoming original avatar and save it locally.

/ / Save the original avatar $img_file = file_get_contents ($avatarUrl); / / the avatar of the mini program is the network address that needs to be transferred $img_content= base64_encode ($img_file); $file_tou_name = time (). ".png"; $headurl = PATH.$file_tou_name;file_put_contents ($headurl,base64_decode ($img_content))

two。 Get the Mini Program code of a specific page with parameters and save it. By the way, write the method of getting token (token is usually placed in the cache)

/ / get 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'] / / get the 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); / / Save to local} else {$arr = array ('ret'= > 0msgloves = >' ACCESS TOKEN is empty!') ;}

three。 Edit the saved original avatar and save it as a circle (actually not a circle, change its edges to transparent).

/ / header ("content-type:image/png"); / / pass the saved avatar file name $imgg = yuan_img ($headurl); / / the yuan_img () method lists $file_name = "22" .time (). ".png"; imagepng ($imgg,PATH.$file_name); imagedestroy ($imgg) at the end of the text.

four。 Shrink the avatar (the logo of the mini program with the original image of 1080430 is 192)

$target_im = imagecreatetruecolor (192192); / / create a new canvas (scaled) and fill the transparent background imagesavealpha ($target_im, true) from the upper left corner; $trans_colour = imagecolorallocatealpha ($target_im, 0,0,0,127); imagefill ($target_im, 0,0, $trans_colour); $o_image = imagecreatefrompng (PATH.$file_name) / / get the content of the modified avatar saved above, imagecopyresampled ($target_im,$o_image, 0LJ 0,0je 0192,192, 1080, 1080); $file_head_name = "23" .time (). ".png"; $comp_path = PATH.$file_head_name;imagepng ($target_im,$comp_path); imagedestroy ($target_im)

five。 All the preparations are ready. To splice. (use watermarking to cover the processed avatar with logo)

/ / pass the saved QR code address $url = create_pic_watermark (PATH.$file_code_name,$comp_path, "center") / / at the end of the method, $arr = array ('ret'= > 1,' msg'= > 'success',' data'= > array ('url'= > $url) is listed. / / the processed new Mini Program code is saved on the server and returned to the address of the mini program. Echo json_encode ($arr)

Methods:

/ * * [create_pic_watermark add picture watermark] the avatar is posted in the middle of the QR code * @ param [string] $dest_image [picture name that needs to be watermarked] * @ param [string] $watermark [watermark picture name] * @ param [string] $locate [watermark location] Center,left_buttom,right_ buttom3) * @ 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]); / / original image type $wtype=strtolower ($types [$wtype]); / / watermark image type $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': $xroom1; $y = ($dheight-$wheight-2); break; case' right_buttom': $x = ($dwidth-$wwidth-1); $y = ($dheight-$wheight-2); break; default: die ("No watermark location specified!"); break } imagecopy ($imgd,$imgw,$x,$y,0,0, $wwidth,$wheight); $save= "image". $dtype; / / Save to the server $f_file_name = "24" .time (). ".png"; imagepng ($imgd,PATH.$f_file_name); / / Save imagedestroy ($imgw); imagedestroy ($imgd) / / return the processed image $url = 'https://www.qubaobei.com/'.str_replace('/opt/ci123/www/html/markets/app2/baby/','',PATH.$f_file_name); return $url } / * [yuan_img edit picture as circle] cut avatar to circle * @ param [string] $imgpath [picture name after saving] * / 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) / / this sentence must have imagesavealpha ($img, true); / / pick a completely transparent color. The last parameter 127is fully transparent $bg = imagecolorallocatealpha ($img, 255,255,255,127); imagefill ($img, 0,0, $bg); $r = $w / 2; / / circle radius $yardx = $r; / / center X coordinate $yardy = $r / / Center Y coordinate for ($x = 0; $x < $w; $x coordinates +) {for ($y = 0; $y < $h; $y coordinate +) {$rgbColor = imagecolorat ($src_img, $x, $y) If ($x-$r) * ($x-$r) + ($y-$r) * ($y-$r) < ($r * $r) {imagesetpixel ($img, $x, $y, $rgbColor);} return $img;}

Network request:

/ * initiate request * @ param string $url request address * @ param string $data request packet * @ return string request return data * / function sendCmd ($url,$data) {$curl = curl_init (); / / start a CURL session curl_setopt ($curl, CURLOPT_URL, $url) / / address to be accessed curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0); / / Detection curl_setopt of a pair of authentication certificate sources ($curl, CURLOPT_SSL_VERIFYHOST, 2); / / check whether curl_setopt exists in the SSL encryption algorithm from the certificate ($curl, CURLOPT_HTTPHEADER, array ('Expect:')) / / resolve packet size and cannot submit curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, 1); / / use auto-jump curl_setopt ($curl, CURLOPT_AUTOREFERER, 1); / / automatically set Referer curl_setopt ($curl, CURLOPT_POST, 1); / / send a regular Post request curl_setopt ($curl, CURLOPT_POSTFIELDS, $data) / / packet curl_setopt submitted by Post ($curl, CURLOPT_TIMEOUT, 30); / / set timeout limit to prevent curl_setopt ($curl, CURLOPT_HEADER, 0); / / display the returned Header region content curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1); / / return $tmpInfo = curl_exec ($curl) in the form of a file stream / / execute operation if (curl_errno ($curl)) {echo 'Errno'.curl_error ($curl);} curl_close ($curl); / / key CURL session return $tmpInfo; / / return data} "how WeChat Mini Programs changes the logo in the middle of the Mini Program code" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report