【问题标题】:Auto new-line by dimensions按尺寸自动换行
【发布时间】:2012-08-15 04:35:31
【问题描述】:

今天我想做一个函数,将文本解析成行,由imageTTFBbox() 指导。我创建了这段代码,但仅限于 2 行。我想做同样的事情,但是用无限的线条。谢谢你的帮助! :D

function printe($image, $image_width, $string, $font_size, $y, $color, $font){
    $font = "fonts/" . $font . ".ttf";

    $limit = $image_width - 20;
    $tsize = @imageTTFBbox($font_size,0, $font, $string);
    $twidth = abs($tsize[4] - $tsize[0]);

    $words = explode(" ", $string);
    $text = ''; $text1 = '';$a = 0;$o = 0;

    for($i = 0; $i < count($words); $i++){          
        $tsize = @imageTTFBbox($font_size,0, $font, $words[$i]." ");
        $twidth = abs($tsize[4] - $tsize[0]);
        $nw = $twidth + $a;
        if($nw > $limit OR $o > 0){
            $o++;   
            $text1 .= $words[$i]." ";
        }else{
            $text .= $words[$i]. " ";
            $a =$a+$twidth;
        }
    }

    $txtcolor   = processColor($color); 

    $t1size = @imageTTFBbox($font_size,0, $font, $text);
    $t1width = abs($t1size[4] - $t1size[0]);

    $t2size = @imageTTFBbox($font_size, 0, $font, $text1);
    $t2width = abs($t2size[4] - $t2size[0]);

    $center = ceil($image_width / 2);

    $y1 = $y; $y2 = $y; 

    $xcord1 = ($image_width/2)-($t1width/2)+3;
    $xcord2 = ($image_width/2)-($t2width/2)+3;

    $y2 = $y + ($font_size * 1.5);


    imagettftext($image, $font_size, 0, $xcord1, $y1, $txtcolor, $font, $text);
    imagettftext($image, $font_size, 0, $xcord2, $y2, $txtcolor, $font, $text1);

}

【问题讨论】:

    标签: php image width


    【解决方案1】:

    即使没有完全测试,我还是从an example found on the PHP manual开始写了这段代码:

    <?php
    
    function write_multiline_text($image, $font_size, $color, $font, $text, $start_x, $start_y, $max_width) { 
        //split the string 
        //build new string word for word 
        //check everytime you add a word if string still fits 
        //otherwise, remove last word, post current string and start fresh on a new line 
        $words = explode(" ", $text); 
        $string = ""; 
        $tmp_string = ""; 
    
        for($i = 0; $i < count($words); $i++) { 
            $tmp_string .= $words[$i]." "; 
    
            //check size of string 
            $dim = imagettfbbox($font_size, 0, $font, $tmp_string); 
    
            if($dim[4] < ($max_width - $start_x)) { 
                $string = $tmp_string; 
                $curr_width = $dim[4];
            } else { 
                $i--; 
                $tmp_string = ""; 
                $start_xx = $start_x + round(($max_width - $curr_width - $start_x) / 2);        
                imagettftext($image, $font_size, 0, $start_xx, $start_y, $color, $font, $string); 
    
                $string = ""; 
                $start_y += abs($dim[5]) * 2; 
                $curr_width = 0;
            } 
        } 
    
        $start_xx = $start_x + round(($max_width - $dim[4] - $start_x) / 2);        
        imagettftext($image, $font_size, 0, $start_xx, $start_y, $color, $font, $string);
    }
    
    // Create a 300x300 image
    $im = imagecreatetruecolor(300, 300);
    $black = imagecolorallocate($im, 0, 0, 0);
    $white = imagecolorallocate($im, 255, 255, 255);
    
    // Set the background to be white
    imagefilledrectangle($im, 1, 1, 298, 298, $white);
    
    // Path to our font file
    $font = 'c:/windows/fonts/arial.ttf';
    
    $text = "This is a very ";
    $text .= "long long long long long long long long long long long long long long long long ";
    $text .= "long long long long long long long long long long long long long long long long ";
    $text .= "line of text";
    
    write_multiline_text($im, 12, $black, $font, $text, 10, 22, 298);
    
    // Output to browser
    header('Content-Type: image/png');
    
    imagepng($im);
    imagedestroy($im);
    
    ?>
    

    这是生成的输出图像:

    如果您不希望文本居中,则必须在对函数 imagettftext 的两次调用中将变量 $start_xx 替换为 $start_x

    【讨论】:

    • 您好,我也有基本相同的问题,但我想知道如何根据字体长度动态设置字体大小以适应区域?
    猜你喜欢
    • 2013-08-30
    • 1970-01-01
    • 2017-05-22
    • 2015-07-23
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多