0

今天我想做一个函数,将文本解析成行,由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);

}
4

1 回答 1

4

即使没有经过全面测试,我还是从PHP 手册中的一个示例开始编写此代码:

<?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);

?>

这是生成的输出图像:

在此处输入图像描述

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

于 2012-08-19T16:31:59.290 回答