php图像中的中心/中间文本

时间:2013-09-24 16:04:08

标签: php

我想用php创建一个图像。我有背景图像。在它上面,我想写文字。文本应位于中间(中间)(垂直对齐)。如果文本出现在边界外,文本也应该中断。

我该怎么做?

如果我有这个字符串:

$text = "This is a sample text. This is a second sample text.";

这应该是结果:

希望你明白我的需要。


修改

这就是我所做的: 它位于中间和中间。但文本没有中断。我怎么能这样做?

<?php

header("Content-type: image/png");

//settings
$text = 'This is a sample text. This is a second sample text.';
$width = 200;
$height = 200;

//create image
$im = imagecreate($width, $height);

//colors
$colorWhite = imagecolorallocate($im, 255, 255, 255);
$colorBlack = imagecolorallocate($im, 0, 0, 0);
$colorGrey = imagecolorallocate($im, 207, 199, 199);

//border
imagerectangle($im, 0, 0, $width - 1, $height - 1, $colorGrey);

//fontsize
$fontSize = 3;
$font_width = imagefontwidth($fontSize);
$font_height = imagefontheight($fontSize);

//text size
$text_width = $font_width * strlen($text);
$text_height = $font_height;

//align: center 
$position_center = ceil(($width - $text_width) / 2);

//valign: middle
$position_middle = ceil(($height - $text_height) / 2);

imagestring($im, $fontSize, $position_center, $position_middle, $text, $colorBlack);
imagepng($im);

?>

这是代码的结果:

Result

1 个答案:

答案 0 :(得分:6)

我做到了:

<?php

header("Content-type: image/png");

//settings
$fontSize = 35;
$backgroundPath = "./back.png";
$font = "./MyriadWebPro-Bold.ttf";
$text = "This is a sample text. This is a second sample text.";
$padding = 50; //from edges

//create image
$im = imagecreatefrompng($backgroundPath);
$imageSize = getimagesize($backgroundPath);
$width = $imageSize[0];
$height = $imageSize[1];

//get textRows
$textRows = GetTextRowsFromText($fontSize, $font, $text, $width - ($padding * 2));

//colors
$colorWhite = imagecolorallocate($im, 255, 255, 255);
$colorBlack = imagecolorallocate($im, 0, 0, 0);
$colorGrey = imagecolorallocate($im, 130, 130, 130);

//border
//imagerectangle($im, 0, 0, $width - 1, $height - 1, $colorGrey);

for($i = 0; $i < count($textRows); $i++)
{
    //text size
    $line_box = imagettfbbox ($fontSize, 0, $font, $textRows[$i]);
    $text_width = GetTextWidth($fontSize, $font, $textRows[$i]); 
    $text_height = GetMaxTextHeight($fontSize, $font, $textRows) * 3;

    //align: center 
    $position_center = ceil(($width - $text_width) / 2);

    //valign: middle
    $test = (count($textRows) - $i) - ceil(count($textRows) / 2);
    $position_middle = ceil(($height - ($text_height * $test)) / 2);

    imagettfstroketext($im, $fontSize, 0, $position_center, $position_middle, $colorWhite, $colorGrey, $font, $textRows[$i], 2);
}
imagepng($im);

function imagettfstroketext(&$image, $size, $angle, $x, $y, &$textcolor, &$strokecolor, $fontfile, $text, $px) {

    for($c1 = ($x-abs($px)); $c1 <= ($x+abs($px)); $c1++)
        for($c2 = ($y-abs($px)); $c2 <= ($y+abs($px)); $c2++)
            $bg = imagettftext($image, $size, $angle, $c1, $c2, $strokecolor, $fontfile, $text);

   return imagettftext($image, $size, $angle, $x, $y, $textcolor, $fontfile, $text);
}

function GetTextWidth($fontSize, $font, $text)
{
    $line_box = imagettfbbox ($fontSize, 0, $font, $text);
    return ceil($line_box[0]+$line_box[2]); 
}

function GetTextHeight($fontSize, $font, $text)
{
    $line_box = imagettfbbox ($fontSize, 0, $font, $text);
    return ceil($line_box[1]-$line_box[7]); 
}

function GetMaxTextHeight($fontSize, $font, $textArray)
{
    $maxHeight = 0;
    for($i = 0; $i < count($textArray); $i++)
    {       
        $height = GetTextHeight($fontSize, $font, $textArray[$i]);
        if($height > $maxHeight)
            $maxHeight = $height;

    }

    return $maxHeight;
}

function GetTextRowsFromText($fontSize, $font, $text, $maxWidth)
{   
    $text = str_replace("\n", "\n ", $text);
$text = str_replace("\\n", "\n ", $text);
    $words = explode(" ", $text);

    $rows = array();
    $tmpRow = "";
    for($i = 0; $i < count($words); $i++)
    {

        //last word
        if($i == count($words) -1)
        {
            $rows[] = $tmpRow.$words[$i];
            break;;
        }


        if(GetTextWidth($fontSize, $font, $tmpRow.$words[$i]) > $maxWidth) //break
        {
            $rows[] = $tmpRow;
            $tmpRow = "";
        }
        else if(StringEndsWith($tmpRow, "\n ")) //break in text
        {
            $tmpRow = str_replace("\n ", "", $tmpRow);
            $rows[] = $tmpRow;
            $tmpRow = "";
        }

        //add new word to row   
        $tmpRow .= $words[$i]." ";

    }

    return $rows;
}

function StringEndsWith($haystack, $needle)
{
    return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
}

?>