getRootPath().'public/static/images/poster-bg1.png'; $srcBga = empty($bgImg) ? $defBga : $bgImg; $bgInfo = @getimagesize($srcBga); if ($bgInfo[0] != 750 || $bgInfo[1] != 1334) { throw new \Exception('海报模板尺寸不正确!'); } if (!$bgInfo) { throw new \Exception('海报背景图资源不存在!'); } $ext = $bgInfo['mime']; $extName = ''; $img = null; switch ($ext) { case 'image/jpeg': $img = @imagecreatefromjpeg($srcBga); $extName = 'jpg'; break; case 'image/png': $img = @imagecreatefrompng($srcBga); $extName = 'png'; break; } if (!$img) { throw new \Exception('无效背景图'); } //2、准备颜色 $black = imagecolorallocate($img,0,0,0); $while = imagecolorallocate($img,255,255,255); $faColor = imagecolorallocate($img,0,104,51); // 邀请人头像 // $headimgurl = 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJXE3Zz0U5edXYI2icicYibSNwwezWe0X92fovRtpUwdCF5lAmjsYK5EWT3R8ItO0BEqynElYhWibRqDg/132'; // $headimg = imagecreatefromstring(file_get_contents($headimgurl)); // imagecopymerge($img, $headimg, 50, 1000, 0, 0, 120, 120, 100); // imagecopyresampled($img, $headimg, 90, 900, 0, 0, 120, 120, 120, 120); // 添加文字 // imagettftext($img, 18, 0, 220, 1100, 250, public_path().'static/simheittf.ttf', '超级凉面...邀您关注'); //填充画布(背景色) imagefill($img,0,0, $while); // 组合二维码图片 ,坐标:x:246; y:959 $prefixPng = 'data:image/png;base64,'; $qrStr = base64_decode(str_replace($prefixPng,"",$srcQr)); $qrImg = @imagecreatefromstring($qrStr); list($qrWidth, $qrHeight) = getimagesize($srcQr); if(!$qrImg) { imagedestroy($img); throw new \Exception('无效二维码'); } $imgQrW = $imgQrH = 274; imagecopyresampled($img, $qrImg, 456, 959, 0, 0, $imgQrW, $imgQrH, $qrWidth, $qrHeight); imagedestroy($qrImg); //4、输出与保存最终图像(保存文件或返回base64) if (empty($savePath)) { /* 返回base64 */ ob_start(); if ($ext == 'image/jpeg') { imagejpeg($img); } else { imagepng($img); } $imgData = ob_get_contents(); ob_end_clean(); imagedestroy($img); $prefix = 'data:image/jpg/png/gif;base64,'; return $prefix.base64_encode($imgData); } else { /* 保存到文件*/ $fileName = md5(microtime(true)).'.'.$extName; $saveFile = $savePath."/".$fileName; if ($ext == 'image/jpeg') { imagejpeg($img, $saveFile); } else { imagepng($img, $saveFile); } } /* * 输出显示 header("content-type: image/png"); imagepng($img); */ //5、释放画布资源 imagedestroy($img); } catch (\Exception $e) { throw $e; } return true; } }