php把图片画成圆形的并给背景图加LOGO 加分享二维码

这个是把文字和两张图片加载在背景图上面的

public function  tu(){
        header("content-type:image/jpeg;charset=GBK");//表明请求页面的内容是jpeg格式的图像  即当前页面会以图片的新式展示   去掉这行就可以显示正常的网页了

        $logos = "123456.jpg";
        $img = $this->yuan_img($logos);
        // var_dump($img);exit;
        imagepng($img,'666.png');
        imagedestroy($img);
        // imagepng($img,'555.png');
        $logo = '666.png';
        $code = "http://ezhao.bonnidee.cn/1.png";
        $image  = imagecreatefrompng ("bg.png");//打开一张jpg图片
        $b1 = imagecolorallocate ( $image ,  3,171,189);//为该图片定义一个颜色
        $b2 = imagecolorallocate ( $image ,  16,16,16);//为该图片定义一个颜色
        $b3 = imagecolorallocate ( $image ,  166,166,166);//为该图片定义一个颜色
        $font = "./Public/assetsshop/fonts/arialuni.ttf";//已有的子图路径
         //打印出坐标 方便布局
        // for ($i = 0;$i < $width;$i += 25)
        // {
        //     imagettftext ( $image ,  5 ,  0 ,  $i ,  5 ,  $black ,  $font ,  $i );
        // }

        // for ($i = 0;$i < $height;$i += 25)
        // {
        //     imagettftext ( $image ,  5 ,  0 ,  0 ,  $i ,  $black ,  $font ,  $i );
        // }
        //使用指定的字体文件绘制文字
        //参数2:字体大小
        //参数3:字体倾斜的角度
        //参数4、5:文字的x、y坐标
        //参数6:文字的颜色
        //参数7:字体文件
        //参数8:绘制的文字
        imagettftext ( $image ,  30 ,  0 ,  270 ,  190 ,  $b2 ,  $font ,  "王胖子" );//往图片上添加文字
        imagettftext ( $image ,  22 ,  0 ,  269 ,  263 ,  $b2 ,  $font ,  "上海邦伲德·产品经理" );//往图片上添加文字
        imagettftext ( $image ,  22 ,  0 ,  99 ,  434 ,  $b2 ,  $font ,  "正在招聘" );//往图片上添加文字
        imagettftext ( $image ,  22 ,  0 ,  100 ,  912 ,  $b2 ,  $font ,  "邦伲德 期待你的加入" );//往图片上添加文字


        imagettftext ( $image ,  40 ,  0 ,  99 ,  525 ,  $b2 ,  $font ,  "产品经理" );//往图片上添加文字
        imagettftext ( $image ,  45 ,  0 ,  100 ,  635 ,  $b1 ,  $font ,  "20K-30K" );//往图片上添加文字
        imagettftext ( $image ,  22 ,  0 ,  99 ,  731 ,  $b2 ,  $font ,  "上海·普陀区|3-5年|本科" );
        imagettftext ( $image ,  22 ,  0 ,  99 ,  974 ,  $b2 ,  $font ,  "2020/05/21" );

        // imagettftext ( $image ,  14 ,  0 ,  115 ,  215 ,  $black ,  $font ,  "¥20-30K" );//往图片上添加文字
        

        // $file_name="456.jpg";//设置图片文件名
        $file_name="456.png";//设置图片文件名
        $file_name=iconv("utf-8","gb2312",$file_name);   //解决中文乱码问题
        // imagejpeg($image,$file_name);//如果只要第一个参数就是输出到页面  此时就必须使用 header说明页面内容为图片 ,如果有第二个参数就是 输出到文件,  此时页面就不会有图片输出,就可以去掉header头
        // imagepng($image,$file_name);
        imagepng($image,$file_name);
        if ($logo !== FALSE) {
            $QR = imagecreatefromstring(file_get_contents($file_name));
            $logo = imagecreatefromstring(file_get_contents($logo));
            $code = imagecreatefromstring(file_get_contents($code));
            $QR_width = imagesx($QR);//背景图片宽度
            $QR_height = imagesy($QR);//背景图片高度

            $logo_width = imagesx($logo);//logo图片宽度
            $logo_height = imagesy($logo);//logo图片高度



            $logo_qr_width = $QR_width / 6;
            $scale = $logo_width/$logo_qr_width;
            $logo_qr_height = $logo_height/$scale;
            
            //重新组合图片并调整大小


            $code_width = imagesx($code);//logo图片宽度
            $code_height = imagesy($code);//logo图片高度

            $logo_qr_widths = $QR_width / 5;
            $scales = $code_width/$logo_qr_widths;
            $code_qr_height = $code_height/$scales;
            //背景图片,logo图片,logo图片的X坐标,logo图片的Y坐标,logo图片的X坐标载入位置,LOGO图片的Y坐标载入位置,logo图片的宽,logo图片的高,背景图片的宽,背景图片的高
            // imagecopyresampled($QR, $logo, $from_width, $from_width, 10, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);
            imagecopyresampled($QR, $code, 550, 1405, 0, 0, $logo_qr_widths,$code_qr_height, $code_width, $code_height);

            
            imagecopyresampled($QR, $logo, 110, 155, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);

            imagepng($QR);


            
        }
        

        imagedestroy($image);
        
        exit;  
    }

这个是把图片处理成圆形的

public function yuan_img($imgpath) {
        $ext     = pathinfo($imgpath);
        $src_img = null;
        switch ($ext['extension']) {
        case 'jpg':
        $src_img = imagecreatefromjpeg($imgpath);
        break;
        case 'png':
        $src_img = imagecreatefrompng($imgpath);
        break;
        }
        $wh  = getimagesize($imgpath);
        // $w   = 140;
        $w   = $wh[0];
        $h   = $wh[1];
        // $h   = 140;
        $w   = min($w, $h);
        $h   = $w;
        // $img = imagecreatetruecolor($w, $h);
        $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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值