记录一次php合成海报的开发过程

发布于 2020-04-04 17:08:26

记录一次php合成海报的开发过程,主要记录核心函数,方便以后查询。

function create_poster($user){
    global $_W;
    $qrimg = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$user['ticket'];
    $poster = getConfig('poster');
    $config['bg'] = $poster['bg'];
    $config['ticket'] = $user['ticket'];
    $config['filename'] = $_W['uniacid'].'_'.$user['uid'].'.jpg';
    foreach($poster['data'] as $item){
        if($item['type'] == 'qr' || $item['type'] == 'head'){
            if($item['type'] == 'qr') $urlcode = $qrimg;
            if($item['type'] == 'head') $urlcode = $_W['fans']['avatar'];
            $config['image'][] = [
                'url' => $urlcode,     //二维码资源
                'stream' => 0,
                'left' => intval($item['left'])*2 . 'px',
                'top' => intval($item['top'])*2 . 'px',
                'right' => 0,
                'bottom' => 0,
                'width' => intval($item['width'])*2 . 'px',
                'height' => intval($item['height'])*2 . 'px',
                'opacity' => 100
            ];
        }
        if($item['type'] == 'realname'){
            $config['text'][] = [
                'text' => $_W['fans']['nickname'],
                'left' => intval($item['left'])*2 . 'px',
                'top' => intval($item['top'])*2 . 'px',
                'fontPath' => IA_ROOT . '/images/Alibaba-PuHuiTi-Regular.otf',
                'fontSize' => $item['size'],
                'fontColor' =>$item['color'],
                'angle' => 0,
            ];
        }
    }
    $res = setSharePoster($config);
    return $res;
}

function setSharePoster($config = array())
{
    $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 = tomedia($config['bg']);//海报最底层得背景
    //$backgroundInfo = getimagesize($background);
    $background = imagecreatefromstring(file_get_contents($background));
    $backgroundWidth = 640;  //背景宽度
    $backgroundHeight = 1000;  //背景高度
    $imageRes = imageCreatetruecolor($backgroundWidth, $backgroundHeight);
    $color = imagecolorallocate($imageRes, 0, 0, 0);
    imagefill($imageRes, 0, 0, $color);
    imagecopyresampled($imageRes, $background, 0, 0, 0, 0, $backgroundWidth, $backgroundHeight, 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);
            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 (isset($config['text']) && !empty($config['text'])) {
        foreach ($config['text'] as $key => $val) {
            $val = array_merge($textDefault, $val);
            $colors = hex2rgb($val['fontColor']);
            $fontColor = imagecolorallocate($imageRes, $colors['r'], $colors['g'], $colors['b']);
            $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) : $val['left'];
            $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) : $val['top'];
            imagettftext($imageRes, $val['fontSize'], $val['angle'], $val['left'], $val['top'], $fontColor, $val['fontPath'], $val['text']);
        }
    }
    ob_start();
    imagejpeg($imageRes);
    imagedestroy($imageRes);
    $res = ob_get_contents();
    ob_end_clean();
    file_put_contents(IA_ROOT.'/cache/'.$config['filename'],$res);
    return tomedia('/cache/'.$config['filename']);
}

function hex2rgb($hexColor){
    $color=str_replace('#','',$hexColor);
    if (strlen($color)> 3){
        $rgb=array(
            'r'=>hexdec(substr($color,0,2)),
            'g'=>hexdec(substr($color,2,2)),
            'b'=>hexdec(substr($color,4,2))
        );
    }else{
        $r=substr($color,0,1). substr($color,0,1);
        $g=substr($color,1,1). substr($color,1,1);
        $b=substr($color,2,1). substr($color,2,1);
        $rgb=array(
            'r'=>hexdec($r),
            'g'=>hexdec($g),
            'b'=>hexdec($b)
        );
    }
    return $rgb;
}

Bug天天改,头发日日疏,码字不易,如果有帮助到你,就点击"下方感谢"支持一下把.

©声明:本站所有文章,如无特殊说明或标注,均为izhnagbo.cn原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。本文链接 https://izhangbo.cn/article/34.html
0 条评论

学习
记录

发布
问题