小程序合成海报图片,高度自适应

将一篇带有图片的文章合成一张图片,由于无法直接一起添加图片和文字,将图片和文字拆分开,挨个添加到画布上,可根据文字和图片的高度计算出需要创建画布的高度,并生成合适大小的画布 

/**
     *  获取要添加到画布的展示内容
     * @param $text  内容
     * @param int $fontsize 文字大小
     * @param int $width  要展示的最大宽度
     * @param int $height 要展示的最大高度
     * @param int $fontHeight  字体高度
     * @param int $eol_height  EOL高度
     * @return array
     */
    public static function getShowText($text, $fontsize = 24, $width = 650, $height = 800, $fontHeight = 60, $eol_height = 10)
    {
        $fontFile = public_path() . '/static/fonts/msyh.ttf';
        $content = str_replace(" ", "", $text);
        $arr = Util::spiltHtml($content);
        $h = 0;
        $contArr = array();

        foreach ($arr as $item) {
            if ($item['type'] == 'text') {
                $conTxt = self::autoWrap($fontsize, $fontFile, $item['text'], $width);
                $size = imagettfbbox($fontsize, 0, $fontFile, $conTxt);

                $strArr = explode(PHP_EOL, $conTxt);
                foreach ($strArr as $key => $item) {
                    if (empty($item)) {
                        $h += $eol_height;
                        $contArr[] = ['type' => 'text', 'str' => $item, 'lineheight' => $eol_height];
                    } else {
                        $h += $fontHeight;
                        $contArr[] = ['type' => 'text', 'str' => $item, 'lineheight' => $fontHeight];
                    }
                    if ($h >= $height) {
                        break 2;
                    }
                }
            } else if ($item['type'] == 'img') {
                try{
                    $imginfo = getimagesize($item['src']);
                    $maxImgWidth = ceil($width / 2);
                    if ($imginfo[0] > $maxImgWidth) {
                        $srcH = ceil($width * $imginfo[1] / $imginfo[0]);
                        $h += intval(20 + $srcH);
                        $contArr[] = ['type' => 'img', 'str' => $item['src'], 'w' => $width, 'h' => $srcH];
                    } else {
                        $h += $imginfo[1];
                        $contArr[] = ['type' => 'img', 'str' => $item['src'], 'w' => $imginfo[0], 'h' => $imginfo[1]];
                    }
                    if ($h >= $height) {
                        break;
                    }
                } catch (\Exception $e){
                    continue;
                }

            }
        }

        $res = ['h' => $h, 'txtArr' => $contArr];
        return $res;

    }

 /**
     * @param $fontsize   字体大小
     * @param $ttfpath   字体文件
     * @param $str      字符串
     * @param $width    最大宽度
     * @param int $fontangle  将被度量的角度大小
     * @param string $charset   编码
     * @return string
     */
    public static function autoWrap($fontsize, $ttfpath, $str, $width, $fontangle = 0, $charset = 'utf-8')
    {
        $len = mb_strlen($str);
        $arr = array();
        $tmpArr = array();
        $tmpStr = '';
        for ($i = 1; $i <= $len; $i++) {
            $tmpStr = mb_substr($str, 0, $i);
            $size = imagettfbbox($fontsize, $fontangle, public_path() . '/static/fonts/msyh.ttf', $tmpStr);
            $w = $size[2];
            if ($w < $width) {
                if ($i < $len) {
                    continue;
                } else {
                    return $tmpStr;
                }
            } else {
                $arr[] = $tmpStr;
                $str2 = mb_substr($str, $i);
                $tmpStr .= PHP_EOL . self::autoWrap($fontsize, $ttfpath, $str2, $width);
            }
            break;
        }
        return trim($tmpStr, PHP_EOL);
    }


/**
     * 将html文字和图片分类成数组
     * @param $str
     * @return array
     */
    public static function spiltHtml($str){
        $content = stripslashes(strip_tags($str, ""));
        preg_match_all('//',$content);
        foreach ($res as $key => $value) {
            if($value != ""){
                $result[] = array(
                    'type' => 'text',
                    'text' => htmlspecialchars($value, ENT_QUOTES, 'utf-8')
                );
            }
            if(!empty($imgs[$key])){
                $src = htmlspecialchars($imgs[$key], ENT_COMPAT , 'utf-8');
                $result[] = array(
                    'type' => 'img',
                    'src' => $src
                );
            }
        }
        return $result;
    }

 

你可能感兴趣的:(Font)