php生成圆形头像,合并到其他图片,以及二维码的合并

   public function planetshareAction()
    {
        putenv('GDFONTPATH=' . realpath(__DIR__ . '/../../assets'));
        $param = $_GET;
        header("Content-Type:application/json;");
        $token = !empty($param['token']) ? $param['token'] : '';
//        if(empty($token)){
//            $result=array('code'=>2,'message'=>'参数有误');
//            echo json_encode($result,JSON_UNESCAPED_UNICODE);
//            return true;
//        }
        $loginArr= JWTAuth::checkLoginV2($token);
        $uid=empty($loginArr['uid'])?0:$loginArr['uid'];
        $muid=empty($loginArr['muid'])?0:$loginArr['muid'];

        $uid = 147;$muid = 0;
        $rpcParam['user'] = array('module'=>'user_user','method'=>'getUserInfoByUidOrMuid','param'=>array($uid,$muid));
        $rpcParam['code'] = array('module'=>'user_user','method'=>'getUserInviteCodeByMuidAndUid','param'=>array($uid,$muid));
        $rpcRes = RpcClient2::multiResult($rpcParam);
        foreach($rpcRes as $k=>$res){
            if($res['code']!= ServiceCode::SUCCESS){
                $this->logger->error("RpcClient2::multiResult error",array('param'=>$rpcParam[$k],'res'=>$res));
                echo json_encode(array('code'=>2,'message'=>'系统错误'),JSON_UNESCAPED_UNICODE);
                return true;
            }
        }

        $userInfo = !empty($rpcRes['user']['res']) ? $rpcRes['user']['res'] : [];
        $codeInfo = !empty($rpcRes['code']['res']) ? $rpcRes['code']['res'] : [];
        $userName = $userInfo['real_name'] ? $userInfo['real_name'] : $userInfo['nickname'];
        $headimgurl = $userInfo['headimgurl'];
        $inviteCodeText = '邀请码:'.$codeInfo['invite_code'];

        $totalWdith = 280 * 2;
        $totalHeight = 518 * 2;
        $headimgOneWidth = 280 * 2;
        $headimgOneHeight = 78 * 2;
        $headimgTwoWidth = 270 * 2;
        $headimgTwoHeight = 318 * 2;
        $currentHeight = $headimgOneHeight + $headimgTwoHeight;
        $img = imagecreatetruecolor($totalWdith, $totalHeight);
        $white = imagecolorallocate($img, 255, 255, 255);
        $black = imagecolorallocate($img, 0, 0, 0);
        $grey = imagecolorallocate($img, 136, 136, 136);
        //背景色
        imagefilledrectangle($img, 0, 0, $totalWdith, $totalHeight, $white);
        //头部带阴影图片
        $headerimg = imagecreatefrompng(__DIR__ . '/../../assets/planet_one.png');
        imagecopy($img, $headerimg, 0, -1, 0, 0, $headimgOneWidth, $headimgOneHeight);
        imagedestroy($headerimg);
        //头部第二张图片
        $headerimgTwo = imagecreatefrompng(__DIR__ . '/../../assets/planet_two.png');
        imagecopy($img, $headerimgTwo, 5*2, 78 * 2, 0, 0, $headimgTwoWidth, $headimgTwoHeight);
        imagedestroy($headerimg);
        //用户头像
        //圆形图像参数
        $p_tmpWidth = 400;
        $p_tmpHeight = 300;
        $p_cutWidth = 0;
        $p_cutHeight = 0;
        $p_cutX = 0;
        $p_cutY = 0;
        $p_finalWidth = 45 * 2;
        $p_finalHeight = 45 * 2;
        $headImg = $this->cutPicture($headimgurl,$p_tmpWidth, $p_tmpHeight, $p_cutWidth, $p_cutHeight, $p_cutX, $p_cutY, $p_finalWidth, $p_finalHeight);
        imagecopy($img, $headImg, 16.5 * 2, $currentHeight + 15 * 2, 0, 0, $p_finalWidth, $p_finalHeight);
        imagefttext($img, 15 * 1.5, 0, 71.5 * 2, $currentHeight + 45 * 2, imagecolorallocate($img, 51, 51, 51), 'PingFang', $userName);

        imagefttext($img, 11 * 1.5, 0, 16.5 * 2, $currentHeight + 80 * 2, imagecolorallocate($img, 51, 51, 51), 'PingFang', $inviteCodeText);
        imagefttext($img, 11 * 1.5, 0, 103 * 2, $currentHeight + 80 * 2, imagecolorallocate($img, 136, 136, 136), 'PingFang', '(仅限5名)');
        //矩形框
        $leftSpace = 16.5 * 2;
        imagerectangle($img,$leftSpace,$currentHeight + 90 * 2, $leftSpace + 129.5 * 2, $currentHeight + 108 * 2,$grey);
        imagefttext($img, 11 * 1.5, 0, 21 * 2, $currentHeight + 103 * 2, imagecolorallocate($img, 136, 136, 136), 'PingFang', '长按二维码查看我的星球');
        //二维码
        $url = "https://m.bxcc.vip/notes/note1";
        $qr = new PhpQrcode();
        $qrCodeUrl =__DIR__ . '/../../assets/planetQrCode.png';
        $qr->generateQrCode($url, $qrCodeUrl, QR_ECLEVEL_L,5.5,2,true);
        //二维码图片合并
        $qrcodeImgWidth = 80 * 2;
        $qrcodeImgHeight = 80 * 2;
        $qrcodeImg = imagecreatefrompng($qrCodeUrl);
        imagecopy($img,  $qrcodeImg, 187 * 2, $currentHeight + 32 * 2, 0, 0, $qrcodeImgWidth, $qrcodeImgHeight);

        header("Content-Type:image/png");
        imagepng($img);
        imagedestroy($img);
        unlink($qrCodeUrl);
    }


    /**
     * 裁剪图片处理
     *
     * 图片裁剪的原理:
     * 1、先建立一个和前端裁剪控件宽高相同的基础画板。
     * 2、再把原图按原图自身的宽高比,以画板的宽或高为缩放基准等比缩放在画板上。
     * 3、再按照前端控件传过来的裁切的宽高和相对于前端画板原点的坐标,在后端生成画板中裁切出图片。
     * 4、再把裁剪出的图片按它自身的宽高比,以最终图像(200*200的图片)的宽或高为缩放基准等比缩放在最终图像上,且生成图像是透明背景。
     *
     * @param  [type]  $tmpname       图片上传成功后的临时目录
     * @param  [type]  $saveName      处理后的图片的保存路径和名称
     * @param  integer $p_tmpWidth    画板宽
     * @param  integer $p_tmpHeight   画板高
     * @param  integer $p_cutWidth    裁剪/选区的宽
     * @param  integer $p_cutHeight   裁剪/选区的高
     * @param  integer $p_cutX        选区左上角的点相对于等比缩放后的图片的原点的横坐标
     * @param  integer $p_cutY        选区左上角的点相对于等比缩放后的图片的原点的纵坐标
     * @param  integer $p_finalWidth  最终图像的宽
     * @param  integer $p_finalHeight 最终图像的高
     * @return [type]                 [description]
     */
    public function cutPicture($tmpname,$p_tmpWidth = 400, $p_tmpHeight = 300, $p_cutWidth = 0, $p_cutHeight = 0, $p_cutX = 0, $p_cutY = 0, $p_finalWidth = 200, $p_finalHeight = 200)
    {
        // 原图信息
        $imgInfo   = getimagesize($tmpname);
        $imgWidth  = $imgReWidth =  $imgInfo['0'];
        $imgHeight = $imgReHeight = $imgInfo['1'];
        $imgMime   = $imgInfo['mime'];

        // 打开原图
        if ($imgMime == 'image/gif') {
            $imgSource = imagecreatefromgif($tmpname);
        } elseif ($imgMime == 'image/jpeg' || $imgMime == 'image/jpg') {
            $imgSource = imagecreatefromjpeg($tmpname);
        } elseif ($imgMime == 'image/png') {
            $imgSource = imagecreatefrompng($tmpname);
        }

        // 原图exif信息,判断是否需要旋转图片
//        if (function_exists('exif_read_data')) {
//            $exif = @exif_read_data($tmpname);
//            if (isset($exif['Orientation'])) {
//                if ($exif['Orientation'] == 1) {
//                    // 不需要旋转
//                } elseif ($exif['Orientation'] == 6) {
//                    // 顺时针旋转90度
//                    $imgSource = imagerotate($imgSource, -90, 0);
//                    // 图片旋转90度以后,原图宽和高要调换
//                    $imgWidth  = $imgReWidth = $imgInfo['1'];
//                    $imgHeight = $imgReHeight= $imgInfo['0'];
//                } elseif ($exif['Orientation'] == 8) {
//                    // 逆时针旋转90度
//                    $imgSource = imagerotate($imgSource, 90, 0);
//                    // 图片旋转90度以后,原图宽和高要调换
//                    $imgWidth  = $imgReWidth = $imgInfo['1'];
//                    $imgHeight = $imgReHeight= $imgInfo['0'];
//                } elseif ($exif['Orientation'] == 3) {
//                    // 逆时针旋转180度
//                    $imgSource = imagerotate($imgSource, 180, 0);
//                }
//            }
//        }

        // 计算等比缩放到画板上时,原图的宽高
        $tmpWidth     = $p_tmpWidth;  			 // 画板的宽高(与前端裁剪控件的画板宽高相同)
        $tmpHeight    = $p_tmpHeight; 			 // 画板的宽高(与前端裁剪控件的画板宽高相同)
        $ratio_1      = $imgWidth / $imgHeight;  // 原图宽高比
        $ratio_2      = $tmpWidth / $tmpHeight;  // 画板宽高比
        if ($ratio_1  >= $ratio_2) {
            $imgWidth = $tmpWidth; 			 	 // 以画板宽为基准,计算等比缩放到画板上的原图的高
            $imgHeight= $imgWidth / $ratio_1;
        } else {
            $imgHeight= $tmpHeight;		 	 	 // 以画板高为基准,计算等比缩放到画板上的原图的宽
            $imgWidth = $imgHeight * $ratio_1;
        }

        // 创建画板
        $im_2 = imagecreatetruecolor($tmpWidth, $tmpHeight);
        // 将原图等比缩放到画板$im_2上,然后再在画板上裁剪需要的图片。注意:这种缩放并不一定完全占用整个画板的区域。
        imagecopyresampled($im_2, $imgSource, 0, 0, 0, 0, $imgWidth, $imgHeight, $imgReWidth, $imgReHeight);
        // 要裁剪的宽高(选区的宽高)
        $cutWidth  = $p_cutWidth;
        $cutHeight = $p_cutHeight;

        // 如果没有选择要裁剪的区域,默认把等比缩放后的整张图片作为选区,即裁剪整张图片
        if (empty($cutWidth) || empty($cutHeight)) {
            $cutWidth = $imgWidth;
            $cutHeight= $imgHeight;
        }

        // 创建与选区(要裁剪的图片)宽高相同的底版
        $im_3  = imagecreatetruecolor($cutWidth, $cutHeight);

        // 选区左上角的点相对于等比缩放后的图片的原点的坐标
        $cutX = $p_cutX;
        $cutY = $p_cutY;

        // 从画板中按坐标、裁切的宽高裁剪图片,并将裁剪的图片偏复制到$im_3上
        imagecopy($im_3, $im_2, 0, 0, $cutX, $cutY, $cutWidth, $cutHeight);

        // 最终图像的宽高
        $finalWidth  = $p_finalWidth;
        $finalHeight = $p_finalHeight;

        // 计算等比缩放到最终上图像时,裁切的图片的宽高
        $cutRatio    = $cutWidth / $cutHeight;
        $finalRatio  = $finalWidth / $finalHeight;
        if ($cutRatio >= $finalRatio) {
            $finalCutWidth = $finalWidth; // 以最终图像的宽为基准,计算等比缩放到最终图像上的裁切图的高
            $finalCutHeight= $finalCutWidth / $cutRatio;
        } else {
            $finalCutHeight= $finalHeight;// 以最终图像的高为基准,计算等比缩放到最终图像上的裁切图的宽
            $finalCutWidth = $finalHeight * $cutRatio;
        }

        // 居中的坐标值
        $finalX = ($finalWidth  - $finalCutWidth) / 2;
        $finalY = ($finalHeight - $finalCutHeight) / 2;

        // 最终图像
        $im_final = imagecreatetruecolor($finalWidth, $finalHeight);

        // 设置透明背景色,使$im_final底版透明
        $bgColor  = imagecolorallocatealpha($im_final, 0, 0, 0, 127);
        imagealphablending($im_final, false);
        imagefill($im_final, 0, 0, $bgColor);
        imagesavealpha($im_final, true);

        // 将从画板中裁切的图片居中并等比缩放到(默认200*200)的图像中
        imagecopyresampled($im_final, $im_3, $finalX, $finalY, 0, 0, $finalCutWidth, $finalCutHeight, $cutWidth, $cutHeight);

        //上面代码是缩放图片的
        //下面部分是圆形图片代码
        $w   = $finalWidth;
        $h   = $finalHeight;
        $w   = min($w, $h);
        $h   = $w;
        $img = imagecreatetruecolor($w, $h);
        imagesavealpha($img, true);
        $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);//背景色 白色
//        $bg = imagecolorallocatealpha($img, 255, 255, 255, 255);//背景色 黑色
        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($im_final, $x, $y);
                if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
                    imagesetpixel($img, $x, $y, $rgbColor);
                }
            }
        }

        return $img;
//        header("Content-Type:image/png");
//        imagepng($img);
        imagedestroy($im_final);
        imagedestroy($imgSource);
        imagedestroy($im_2);
        imagedestroy($im_3);
    }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值