php 图片合并



        //获取用户信息
       

        $user_path = BASE_PATH . '/data/wx_mini/share_qrcode/';
        $user_head_img = $user_path .'user'. $user_info['user_id'] . '.jpg';
        
        if (!file_exists($user_path)) {
            mkdir($user_path, 0777, true);
        }
        //缩放图片
        imagepress($user_info['headimg'], 100, 100, $user_head_img);
        
        


        //获取商品信息
        $goods_info['goods_image'] = get_imgurl_oss($goods_info['goods_image']);
        $goods_info['start_price'] = empty($goods_info['bid_price']) ? $goods_info['start_price']:$goods_info['bid_price'];
        $goods_info['end_time'] = local_date('m月d日 H:i', $goods_info['end_time']);
        
        //缩放商品图片
        $goods_img = $user_path .'goods'. $goods_info['goods_id'] . '.jpg';
        imagepress($goods_info['goods_image'], 620, 620, $goods_img);
        
        
        //获取二维码图片
        $qrcode = wx_gong_QRCode($user_id, $act_id, 'pai', 3);
        $qrcode_img = $qrcode['save_path'];

       
        //字体文件,字体文件放在相对路径下访问。
        $font = 'msyhl.ttc'; //普通字体
        $fontb = 'msyh.ttc'; //加粗字体
        $fontbb = 'msyhbd.ttc'; //更粗字体
        //字体颜色(RGB)
        //字体大小
        $fontSize_22 = 22;
        $fontSize_26 = 26;
        $fontSize_36 = 36;
        //旋转角度
        $circleSize = 0;
        //左边距
        $left = 50;
        //上边距
        $top = 150;

        $background_img = './data/paimaibg/paimaibg.jpg';  //大背景图
//        $background_img = './data/paimaibg/4.png';  //大背景图
        $target_user_img = $user_head_img;  //用户头像
        $target_goods_img = $goods_img; //商品图片
        $target_qrcode_img = $qrcode_img; //二维码
        $paimaitxt_img = './data/paimaibg/paimaitxt.png'; //拍卖文字图片
        $timebg_img = './data/paimaibg/paimaitxtbg.png'; //拍卖计时背景图

        $background_img_str = imagecreatefromstring(file_get_contents($background_img));
        $target_user_img_str = imagecreatefromstring(file_get_contents($target_user_img));
        $target_goods_img_str = imagecreatefromstring(file_get_contents($target_goods_img));
        $target_qrcode_img_str = imagecreatefromstring(file_get_contents($target_qrcode_img));
        $target_paimaitxt_str = imagecreatefromstring(file_get_contents($paimaitxt_img));
        $target_timebg_str = imagecreatefromstring(file_get_contents($timebg_img));


        

        list($background_img_width, $background_img_height, $background_img_type) = getimagesize($background_img);
        list($user_img_width, $user_img_height, $user_img_type) = getimagesize($target_user_img);
        list($goods_img_width, $goods_img_height, $goods_img_type) = getimagesize($target_goods_img);
        list($qrcode_img_width, $qrcode_img_height, $qrcode_img_type) = getimagesize($target_qrcode_img);
        list($paitxt_img_width, $paitxt_img_height, $paitxt_img_type) = getimagesize($paimaitxt_img);
        list($timebg_img_width, $timebg_img_height, $timebg_img_type) = getimagesize($timebg_img);
        
        //设置商品图片的位置
        $goods_w = ( $background_img_width - $goods_img_width ) *0.5;
        $goods_h = 200;
        
        //用户头像圆角处理
        $w = $user_img_width;
        $h = $user_img_height;
        $c = imagecolorallocate($target_user_img_str, 255, 0, 0);
        imagearc($target_user_img_str, $w/2, $h/2, $w, $h, 0, 360, $c);
        imagefilltoborder($target_user_img_str, 0, 0, $c, $c); 
        imagefilltoborder($target_user_img_str, $w, 0, $c, $c); 
        imagefilltoborder($target_user_img_str, 0, $h, $c, $c); 
        imagefilltoborder($target_user_img_str, $w, $h, $c, $c); 
        imagecolortransparent($target_user_img_str, $c);
        
        
        //合并图片,用户头像,商品图,二维码
        imagecopymerge($background_img_str, $target_user_img_str, 50, 50, 0, 0, $user_img_width, $user_img_height, 100);
        imagecopymerge($background_img_str, $target_goods_img_str, $goods_w, $goods_h, 0, 0, $goods_img_width, $goods_img_height, 100);
        imagecopy($background_img_str, $target_paimaitxt_str, $goods_w, $goods_h, 0, 0, $paitxt_img_width, $paitxt_img_height);
        imagecopy($background_img_str, $target_timebg_str, $goods_w+($goods_img_width-$timebg_img_width), $goods_h+$goods_img_height-$timebg_img_height, 0, 0, $timebg_img_width, $timebg_img_height);
        imagecopymerge($background_img_str, $target_qrcode_img_str, $background_img_width - $qrcode_img_width - 47, $background_img_height - $qrcode_img_height - 106, 0, 0, $qrcode_img_width, $qrcode_img_height, 100);
        
        
        //添加文字
        $black = imagecolorallocate($background_img_str, 0, 0, 0);  //文字颜色
        $red = imagecolorallocate($background_img_str, 255, 0, 0);
        $white = imagecolorallocate($background_img_str, 255, 255, 255);
        
        //文字内容
        imagefttext($background_img_str, $fontSize_22, $circleSize, $user_img_width+50+15, 80, $black, $fontb, $user_info['alias']);
        imagefttext($background_img_str, $fontSize_26, $circleSize, $user_img_width+50+15, 135, $black, $fontb, '为你推荐好物');
        
        imagefttext($background_img_str, $fontSize_22, $circleSize, $goods_w+($goods_img_width*0.5)-40,$goods_img_height + $goods_h-20, $white, $fontb, $goods_info['end_time'].' 拍卖结束');
        
        //当前价格
        imagefttext($background_img_str, $fontSize_26, $circleSize, $goods_w,$goods_img_height + $goods_h + 80, $black, $fontb, sub_str($goods_info['act_name'], 14));
        imagefttext($background_img_str, $fontSize_26, $circleSize, $goods_w,$goods_img_height + $goods_h + 150, $red, $fontb, '当前价:');
        imagefttext($background_img_str, $fontSize_36, $circleSize, $goods_w, $goods_img_height + $goods_h + 240, $red, $fontb, '¥'.intval($goods_info['start_price']));
        

        $out_img_path = BASE_PATH . '/data/wx_mini/share_he_img/';
        

        if (!is_dir($out_img_path)) {
            mkdir($out_img_path, 0777, true);
        }
        
        switch ($background_img_type) {
            case 1://gif
                header('Content-Type:image/gif', './1.gif');
                $file_name = 'he' . gmtime() . '.gif';
                $out_img_file_path = $out_img_path . $file_name;
                imagegif($background_img_str, $out_img_file_path);
                break;
            case 2://jpg
                header('Content-Type:image/jpg', './1.jpg');
                $file_name = 'he' . gmtime() . '.jpg';
                $out_img_file_path = $out_img_path . $file_name;
                imagejpeg($background_img_str, $out_img_file_path);
                break;
            case 3://jpg
                header('Content-Type:image/png', './1.png');
                $file_name = 'he' . gmtime() . '.png';
                $out_img_file_path = $out_img_path . $file_name;
                imagepng($background_img_str, $out_img_file_path);
                break;
            default:
                break;
        }

        // //销毁图片
        imagedestroy($background_img_str);
        imagedestroy($target_user_img_str);
        imagedestroy($target_goods_img_str);
        imagedestroy($target_qrcode_img_str);
        imagedestroy($target_paimaitxt_str);
        imagedestroy($target_timebg_str);

 

//缩放图


//$filepath图片路径,$percent缩放百分比
function imagepress($filepath,$w,$h,$new_path=''){
    // 图片类型,这里的二维码是PNG的   所以使用PNG类型
    header('Content-Type: image/jpg');
    // 获得新的图片大小
    list($width, $height) = getimagesize($filepath);  //取图片信息
//    $new_width = $width * $percent;     也可以设置百分比,我这里写死了
//    $new_height = $height * $percent;
    $new_width = $w;
    $new_height = $h;
    // 重新取样
    $image_p = imagecreatetruecolor($new_width, $new_height);
//    $image = imagecreatefrompng($filepath);
    $image = imagecreatefromjpeg($filepath);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    
    if(!empty($new_path)){
        $file = $new_path;
    }else{
        $file = $filepath;
    }
    
    // 输出
    return imagejpeg($image_p, $file, 100);  //这里的名命可根据需要设置
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值