小程式用戶在生成分享二維碼時,默認使用的是小程式設定的icon圖片,但是我們有些時候往往想自定義圖片進而換成我們自己想要的,微信小程式開發檔案上也沒有提供相應的方法,那么我們只能自行進行替換,下面開始代碼:
一、代碼入口檔案,接受小程式二維碼及需要替換成的logo圖,并輸出最終結果
/** * [GetBgCode 處理小程式二維碼,中間logo自定義,入口檔案] */ public function GetBgCode(){ $headimg=ROOT_PATH . 'upload/headimg.jpg';//要替換的logo地址,網圖則需要保存到本地再進行處理,這里處理的本地圖片 $qrcode=ROOT_PATH . 'upload/qrcode.jpg';//小程式已經獲取到的二維碼圖
$imgg = $this->GetYuanImg($headimg); //保存處理之后的圓圖 $file_name = "yuan".time().rand(10000, 99999).".png"; imagepng($imgg,ROOT_PATH . 'upload' . DS . 'qrcode1_y1/'.$file_name); imagedestroy($imgg); //縮小圓圖 $comp_path=$this->NarrowHeadImg($file_name); //拼接圖片 $url =$this->CreateImgWatermark($qrcode,$comp_path,"center"); //處理完的新小程式碼 保存在服務器,傳回地址 $arr = array('ret'=>1, 'msg'=>'success', 'data'=>array('url'=>$url), ); echo json_encode($arr); }
二、剪切圖片為原型
/** * [GetYuanImg 編輯圖片為原型,剪切圖片為原型] * @param [string] $imgpath [頭像保存之后的圖片名] */ public function GetYuanImg($imgpath) { $ext = pathinfo($imgpath); $src_img = null; switch ($ext['extension']) { case 'jpg': $src_img = imagecreatefromjpeg($imgpath); break; case 'png': $src_img = imagecreatefrompng($imgpath); break; case "jpeg": $img_r=imagecreatefromjpeg($imgpath); break; case "gif": $img_r=imagecreatefromgif($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; }
三、縮小剪切之后的圓圖
1 /** 2 * [NarrowHeadImg 縮小圖片] 3 * @param [type] $file_name [縮小圖片的最終保存地址] 4 */ 5 public function NarrowHeadImg($file_name){ 6 $target_width=192; //定義縮小圖片的大小 7 $target_height=192; 8 9 $target_img=ROOT_PATH . 'upload' . DS . 'qrcode1_y1/'.$file_name; 10 $img_info=getimagesize($target_img); // 獲取原圖尺寸 13 14 15 $original_width=$img_info[0]; //原圖片寬度 16 $original_height=$img_info[1]; //原圖片高度 17 $original_mime=$img_info['mime']; 19 20 $target_scale = $target_height/$target_width; //目標影像長寬比 22 $original_scale = $original_height/$original_width; // 原圖片長寬比 24 if ($original_scale>=$target_scale){ // 過高 25 $w = intval($original_width); 26 $h = intval($target_scale*$w); 28 $x = 0; 29 $y = ($original_height - $h)/3; 30 } else { // 過寬 31 $h = intval($original_height); 32 $w = intval($h/$target_scale); 34 $x = ($original_width - $w)/2; 35 $y = 0; 36 } 38 39 $target_im = imagecreatetruecolor($target_width,$target_height);//固定圖片大小 192 40 imagesavealpha($target_im, true); 41 $trans_colour = imagecolorallocatealpha($target_im, 0, 0, 0, 127); 42 imagefill($target_im, 0, 0, $trans_colour); 43 //獲取上文已保存的修改之后頭像的內容 44 $o_image = imagecreatefrompng(ROOT_PATH . 'upload' . DS . 'qrcode1_y1/'.$file_name); 45 46 imagecopyresampled($target_im,$o_image, 0, 0,0, 0, $target_width,$target_height, $w, $h); 47 $file_head_name = "yuan".time().rand(10000, 99999).".png"; 48 $comp_path =ROOT_PATH . 'upload' . DS . 'qrcode1_y2/'.$file_head_name; 49 imagepng($target_im,$comp_path); 50 imagedestroy($target_im); 51 return $comp_path; 52 53 }
四、拼接小程式二維碼及自定義圖片 ,獲取最后的地址
1 /** 2 * [CreateImgWatermark 拼接頭像貼在二維碼中間] 5 * @param [type] $dest_image [小程式二維碼圖片地址] 6 * @param [type] $watermark [logo圖] 7 * @param [type] $locate [水印位置,center,left_buttom,right_buttom 三選一] 8 */ 9 public function CreateImgWatermark($dest_image,$watermark,$locate){ 11 list($dwidth,$dheight,$dtype)=getimagesize($dest_image); 12 list($wwidth,$wheight,$wtype)=getimagesize($watermark); 13 $types=array(1 => "GIF",2 => "JPEG",3 => "PNG", 14 4 => "SWF",5 => "PSD",6 => "BMP", 15 7 => "TIFF",8 => "TIFF",9 => "JPC", 16 10 => "JP2",11 => "JPX",12 => "JB2", 17 13 => "SWC",14 => "IFF",15 => "WBMP",16 => "XBM"); 18 $dtype=strtolower($types[$dtype]);//原圖型別 19 $wtype=strtolower($types[$wtype]);//水印圖片型別 20 $created="imagecreatefrom".$dtype; 21 $createw="imagecreatefrom".$wtype; 22 $imgd=$created($dest_image); 23 $imgw=$createw($watermark); 24 switch($locate){ 25 case 'center': 26 $x=($dwidth-$wwidth)/2; 27 $y=($dheight-$wheight)/2; 28 break; 29 case 'left_buttom': 30 $x=1; 31 $y=($dheight-$wheight-2); 32 break; 33 case 'right_buttom': 34 $x=($dwidth-$wwidth-1); 35 $y=($dheight-$wheight-2); 36 break; 37 default: 38 die("未指定水印位置!"); 39 break; 40 } 41 imagecopy($imgd,$imgw,$x,$y,0,0, $wwidth,$wheight); 42 $save="image".$dtype; 43 //保存到服務器 44 $f_file_name = "bgaa".time().rand(10000, 99999).".png"; 45 imagepng($imgd,ROOT_PATH . 'upload' . DS . 'qrcode1_bg/'.$f_file_name); //保存 46 imagedestroy($imgw); 47 imagedestroy($imgd); 50 //傳回處理好的圖片 51 $url =str_replace('/opt/****baby/','',ROOT_PATH . 'upload' . DS . 'qrcode1_bg/'.$f_file_name); 52 return $url; 53 }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/6585.html
標籤:PHP
