if (!function_exists('gene_poster')) { function gene_poster($param) { $user = User::where(['id' => $param['user_id']])->find(); $link = "http://".$_SERVER['SERVER_NAME'] . '/index/user/register/pid/' . $user['id']; /* echo '<pre>'; print_r($link); exit;*/ $background = $param['background']; $nickname = $user['nickname']; $mobile = $user['mobile']; $avatar = $user['avatar']; $path = "../public/uploads/" . date('Ymd') . "/qrcode/"; $poster_path = "../public/uploads/" . date('Ymd') . "/poster/"; if (!is_dir($path)) { mkdir($path, 0777, true); } if (!is_dir($poster_path)) { mkdir($poster_path, 0777, true); } if (!$user['qrcode']) { /*1生成二維碼存儲*/ $qr = new Endroid\QrCode\QrCode(); $qr_code_img = $qr->setText($link) ->setSize(300) ->setPadding(10) ->setErrorCorrection('high') ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0)) ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0)) ->setLabel('') ->setLabelFontSize(16) ->setImageType(Endroid\QrCode\QrCode::IMAGE_TYPE_PNG); //$path = ""; $qrcode_file_name = $path . date('YmdHis') . $param['user_id'] . ".png"; $re = $qr_code_img->save($qrcode_file_name); User::where(['id' => $param['user_id']])->update(['qrcode' => $qrcode_file_name]); } else { $qrcode_file_name = $user['qrcode']; } $font_path = '../public/assets/fonts/captcha.ttf'; $font_path = 'D:\WORK\project\kangaroocollege\public\assets\fonts\fangzhengheitijianti.TTF'; /*echo '<pre>'; print_r(is_file($font_path)); exit;*/ /*2畫圖 保存*/ $config = array( 'text' => array( array( 'text' => $nickname, 'left' => 350, 'top' => 2050, 'fontPath' => $font_path, //字體文件 'fontSize' => 50, //字號 'fontColor' => '255,255,255', //字體顏色 'angle' => 0, ), array( 'text' => $mobile, 'left' => 350, 'top' => 2150, 'fontPath' => $font_path, //字體文件 'fontSize' => 50, //字號 'fontColor' => '255,255,255', //字體顏色 'angle' => 0, ), array( 'text' => '我是你的專屬客服', 'left' => 160, 'top' => 2250, 'fontPath' => $font_path, //字體文件 'fontSize' => 60, //字號 'fontColor' => '255,255,255', //字體顏色 'angle' => 0, ), array( 'text' => '可向我咨詢', 'left' => 160, 'top' => 2350, 'fontPath' => $font_path, //字體文件 'fontSize' => 60, //字號 'fontColor' => '255,255,255', //字體顏色 'angle' => 0, ) ), 'image' => array( array( // 'url' => $avatar, //圖片資源路徑 'url' => border_radius($avatar), //圖片資源路徑 'left' => 160, 'top' => 2000, 'stream' => 0, //圖片資源是否是字符串圖像流 'right' => 0, 'bottom' => 0, 'width' => 150, 'height' => 150, 'opacity' => 100 ), array( 'url' => $qrcode_file_name, 'left' => 900, 'top' => 2000, 'right' => 0, 'stream' => 0, 'bottom' => 0, 'width' => 400, 'height' => 400, 'opacity' => 100 ), ), 'background' => $background, ); $post_file_name = $poster_path . date('YmdHis') . $param['user_id'] . ".png";; //echo createPoster($config,$filename); return createPoster($config, $post_file_name); } } /** * 生成宣傳海報 * @param array 參數,包括圖片和文字 * @param string $filename 生成海報文件名,不傳此參數則不生成文件,直接輸出圖片 * @return bool|string [type] [description] */ function createPoster($config = array(), $filename = "") { // echo '<pre>'; // print_r($config); // // print_r($config['background']); // exit; //如果要看報什么錯,可以先注釋調這個header //if (empty($filename)) header("content-type: image/png"); $imageDefault = array( 'left' => 0, 'top' => 0, 'right' => 0, 'bottom' => 0, 'width' => 100, 'height' => 100, 'opacity' => 100 ); $textDefault = array( 'text' => '', 'left' => 0, 'top' => 0, 'fontSize' => 32, //字號 'fontColor' => '255,255,255', //字體顏色 'angle' => 0, ); $background = $config['background'];//海報最底層得背景 //背景方法 $backgroundInfo = getimagesize($background); $backgroundFun = 'imagecreatefrom' . image_type_to_extension($backgroundInfo[2], false); $background = $backgroundFun($background); $backgroundWidth = imagesx($background); //背景寬度 $backgroundHeight = imagesy($background); //背景高度 $imageRes = imageCreatetruecolor($backgroundWidth, $backgroundHeight); $color = imagecolorallocate($imageRes, 0, 0, 0); imagefill($imageRes, 0, 0, $color); // imageColorTransparent($imageRes, $color); //顏色透明 imagecopyresampled($imageRes, $background, 0, 0, 0, 0, imagesx($background), imagesy($background), imagesx($background), imagesy($background)); //處理了圖片 if (!empty($config['image'])) { foreach ($config['image'] as $key => $val) { $val = array_merge($imageDefault, $val); $info = getimagesize($val['url']); $function = 'imagecreatefrom' . image_type_to_extension($info[2], false); if ($val['stream']) { //如果傳的是字符串圖像流 $info = getimagesizefromstring($val['url']); $function = 'imagecreatefromstring'; } $res = $function($val['url']); $resWidth = $info[0]; $resHeight = $info[1]; //建立畫板 ,縮放圖片至指定尺寸 $canvas = imagecreatetruecolor($val['width'], $val['height']); imagefill($canvas, 0, 0, $color); /*echo '<pre>'; print_r(1); exit;*/ /* --- 用以處理縮放png圖透明背景變黑色問題 開始 --- */ if($key == 0){ $color = imagecolorallocatealpha($canvas,255,255,255,127); imagecolortransparent($canvas,$color); imagefill($canvas,0,0,$color); } /* --- 用以處理縮放png圖透明背景變黑色問題 結束 --- */ //關鍵函數,參數(目標資源,源,目標資源的開始坐標x,y, 源資源的開始坐標x,y,目標資源的寬高w,h,源資源的寬高w,h) imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight); $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) - $val['width'] : $val['left']; $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) - $val['height'] : $val['top']; //放置圖像 imagecopymerge($imageRes, $canvas, $val['left'], $val['top'], $val['right'], $val['bottom'], $val['width'], $val['height'], $val['opacity']);//左,上,右,下,寬度,高度,透明度 } } //處理文字 if (!empty($config['text'])) { foreach ($config['text'] as $key => $val) { $val = array_merge($textDefault, $val); list($R, $G, $B) = explode(',', $val['fontColor']); $fontColor = imagecolorallocate($imageRes, $R, $G, $B); $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) : $val['left']; $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) : $val['top']; // echo '<pre>'; // print_r($val['fontPath']); // print_r(is_file($val['fontPath'])); // exit; imagettftext($imageRes, $val['fontSize'], $val['angle'], $val['left'], $val['top'], $fontColor, $val['fontPath'], $val['text']); } } //這一句一定要有 imagealphablending( $imageRes,false ); imagesavealpha($imageRes, true); //生成圖片 if (!empty($filename)) { $res = imagepng($imageRes, $filename, 5); //保存到本地 imagedestroy($imageRes); if (!$res) return false; return $filename; } else { imagejpeg($imageRes); //在瀏覽器上顯示 imagedestroy($imageRes); } } /** *圖片修圓角 */ function border_radius($imgpath) { $ext = pathinfo($imgpath); /* echo '<pre>'; print_r($ext); exit;*/ $src_img = null; if(isset($ext['extension'])){ switch ($ext['extension']) { case 'jpg': $src_img = imagecreatefromjpeg($imgpath); break; case 'png': $src_img = imagecreatefrompng($imgpath); break; } }else{ $src_img = imagecreatefromjpeg($imgpath); } $wh = getimagesize($imgpath); $w = $wh[0]; $h = $wh[1]; $w = min($w, $h); $h = $w; $img = imagecreatetruecolor($w, $h); //這一句一定要有 imagealphablending( $img,false ); 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); } } } $avatar_path = "../public/uploads/" . date('Ymd') . "/avatar/"; if (!is_dir($avatar_path)) { mkdir($avatar_path, 0777, true); } // echo '<pre>'; // print_r($img); // exit; // file_put_contents($avatar_path.'avatar'.date('Ymd').".jpg",$img); $filename = $avatar_path.'avatar'.date('Ymd').".png"; header("content-type:image/png"); imagepng($img, $filename, 5); //保存到本地 /* imagepng($img); imagedestroy($img); exit;*/ return $filename; }