我可以使用 GD 和 PHP 生成多行文本吗?

发布于 2024-09-04 03:57:05 字数 289 浏览 4 评论 0原文

我正在尝试使用 GD+PHP 输出多行文本,但无法使其工作。我的 php 知识非常基础。 这是代码,知道如何输出 2 或 3 行文本吗?

$theText = (isset($_GET['caption']))? stripslashes($_GET['caption']) :'';
imagettftext($baseImage, $textSize, $textAngle, $textXposition, $textYposition, $textColor, $fontName, $theText);

I'm trying to output multiline text with GD+PHP but can't get it working. my php knowledge is really basic.
here's the code, any idea on how to output 2 or 3 lines of text?

$theText = (isset($_GET['caption']))? stripslashes($_GET['caption']) :'';
imagettftext($baseImage, $textSize, $textAngle, $textXposition, $textYposition, $textColor, $fontName, $theText);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

习ぎ惯性依靠 2024-09-11 03:57:05
imagettftext($baseImage, $textSize, $textAngle, $textXposition, $textYposition, $textColor, $fontName, $theText);
imagettftext($baseImage, $textSize, $textAngle, $textXposition+(25), $textYposition, $textColor, $fontName, $theText);
imagettftext($baseImage, $textSize, $textAngle, $textXposition+(50), $textYposition, $textColor, $fontName, $theText);

您必须添加 x 像素才能将其向下移动到 X 位置。请记住,整个图像的高度和宽度应足以适合文本。

imagettftext($baseImage, $textSize, $textAngle, $textXposition, $textYposition, $textColor, $fontName, $theText);
imagettftext($baseImage, $textSize, $textAngle, $textXposition+(25), $textYposition, $textColor, $fontName, $theText);
imagettftext($baseImage, $textSize, $textAngle, $textXposition+(50), $textYposition, $textColor, $fontName, $theText);

you have to add x pixel to move it downwards to the X position. keep in mind that your whole image should be high and wide enough to fit the text.

筑梦 2024-09-11 03:57:05

API 不支持它。
以下是“手动”执行此操作的代码:

http://php.net /manual/en/function.imagettftext.php#75718

It's not supported by API.
Here is the code to do it "manually":

http://php.net/manual/en/function.imagettftext.php#75718

彼岸花ソ最美的依靠 2024-09-11 03:57:05

您可以每行重复一个图像;只需将 $theText 拆分为一个数组(分隔符是 NewLine),然后对数组中的每个元素进行循环,将 $textYposition 按行的高度递增(请参阅 $textSize,但实际上使用 imageftbbox 会更好。阅读 PHP 手册中的页面

You can repeat one imagettftext per line; just split $theText into an array (separator is the NewLine) and loop for each element in the array, incrementing $textYposition by the height of the line (see $textSize, but indeed you'll get it better using imageftbbox. Read the page in the PHP manual

爱格式化 2024-09-11 03:57:05

我有未知长度的字符串,但只能使用一定的宽度。所以我想出了这个。基本上它将句子分成字符。如果它碰到空格,它会检查该单词是否可以添加到前一行,如果不能,它会开始一个新行。对于超长的单词也有一个蹩脚的安全措施,它们只是被砍掉,以免脱离图像。

在我实际将文本打印到图像的阶段,我检查该行是否小于允许的最大字符数,并添加前导+尾随空格,以模仿文本对齐:居中。

#  Split up the lines
    $arrMessage = str_split(stripcslashes($strMessage));
    $arrTemp = array();
    $line = 0;
    $word = array();
    $arrTemp[$line] = array();

    foreach($arrMessage as $char){

        //if we hit a space, see if we should continue line, or make a new line
        if($char == " ")
        {
            //calculate numbers of chars currently on line + number of chars in word
            $numTotalChars = (int) count($word) + (int) count($arrTemp[$line]);

            //if total > 14 chars on a line, create new line
            if($numTotalChars > 14)
            {                   
                $line++;
                $arrTemp[$line] = array();

            }
            $word[] = $char;
            //push word-array onto line + empty word array
            $arrTemp[$line] = array_merge($arrTemp[$line], $word);
            $word = array();

        }
        else
        {
       //if word is too long for a line, split it 
            if( count($word) > 16)
            {
                $numTotalChars = (int) count($word) + (int) count($arrTemp[$line]);

                if($numTotalChars > 16)
                {                   
                    $line++;
                    $arrTemp[$line] = array();

                }

                $arrTemp[$line] = array_merge($arrTemp[$line], $word);
                $word = array();

            }

            $word[] = $char;

        }
    }

不要忘记将最后一个单词也添加到一行中。您还需要检查它是否应该换行。

向图像添加线条:

//add some px to x and y for every new line
    $pos_x = $font->position[0];
    $pos_y = $font->position[1];

    $numLineHeight = 20;

    $addToX = 0;

    if($font->angle > 5)
    {
        $addToX = 2;
    }
    else if($font->angle < 0)
    {
        $addToX = -2;
    }

#   ADD MESSAGE
    foreach($arrTemp as $arrLine){

        //leading/trailing whitespace (==center text)
        $numCharsOnThisLine = count($arrLine);          
        $extraWhiteSpace = 14 - $numCharsOnThisLine;
        $frontBackSpace = floor($extraWhiteSpace / 2);

        for($i = 0; $i < $frontBackSpace; $i++){
            array_unshift($arrLine, " ");
            $arrLine[] = " ";
        }
    //make string from char array
        $strLine = implode("", $arrLine);

        imagettftext ($image, $font->size, $font->angle, $pos_x, $pos_y, $tlt, $font->family, $strLine);
        $pos_x = $pos_x + $addToX;
        $pos_y = $pos_y + $numLineHeight;
    }

I had an unknown length of string, but only a certain width to work with. So I came up with this. Basically it splits the sentence in chars. If it bumps on a whitespace, it checks if the word can be added to the previous line, if not, it starts a new line. There is also a lame safety for extra long words, who are just being chopped of, so as not to go out of the image.

In the stage where I actually print the text to the image, I check if the line is smaller than the maximum allowed characters and add leading + trailing whitespaces, to mimic text-align: center.

#  Split up the lines
    $arrMessage = str_split(stripcslashes($strMessage));
    $arrTemp = array();
    $line = 0;
    $word = array();
    $arrTemp[$line] = array();

    foreach($arrMessage as $char){

        //if we hit a space, see if we should continue line, or make a new line
        if($char == " ")
        {
            //calculate numbers of chars currently on line + number of chars in word
            $numTotalChars = (int) count($word) + (int) count($arrTemp[$line]);

            //if total > 14 chars on a line, create new line
            if($numTotalChars > 14)
            {                   
                $line++;
                $arrTemp[$line] = array();

            }
            $word[] = $char;
            //push word-array onto line + empty word array
            $arrTemp[$line] = array_merge($arrTemp[$line], $word);
            $word = array();

        }
        else
        {
       //if word is too long for a line, split it 
            if( count($word) > 16)
            {
                $numTotalChars = (int) count($word) + (int) count($arrTemp[$line]);

                if($numTotalChars > 16)
                {                   
                    $line++;
                    $arrTemp[$line] = array();

                }

                $arrTemp[$line] = array_merge($arrTemp[$line], $word);
                $word = array();

            }

            $word[] = $char;

        }
    }

Don't forget to add the last word to a line as well. You also need to the the check to see if it should be on a new line or not.

Add lines to image:

//add some px to x and y for every new line
    $pos_x = $font->position[0];
    $pos_y = $font->position[1];

    $numLineHeight = 20;

    $addToX = 0;

    if($font->angle > 5)
    {
        $addToX = 2;
    }
    else if($font->angle < 0)
    {
        $addToX = -2;
    }

#   ADD MESSAGE
    foreach($arrTemp as $arrLine){

        //leading/trailing whitespace (==center text)
        $numCharsOnThisLine = count($arrLine);          
        $extraWhiteSpace = 14 - $numCharsOnThisLine;
        $frontBackSpace = floor($extraWhiteSpace / 2);

        for($i = 0; $i < $frontBackSpace; $i++){
            array_unshift($arrLine, " ");
            $arrLine[] = " ";
        }
    //make string from char array
        $strLine = implode("", $arrLine);

        imagettftext ($image, $font->size, $font->angle, $pos_x, $pos_y, $tlt, $font->family, $strLine);
        $pos_x = $pos_x + $addToX;
        $pos_y = $pos_y + $numLineHeight;
    }
鸩远一方 2024-09-11 03:57:05

我的两分钱...
(由于多行文本要求,循环添加文本)

<?php

error_reporting(E_ALL);
error_reporting(-1);
ini_set('error_reporting', E_ALL);

/*
   construction of the tcp scan
   I would prefer building a C C.G.I.
   but let's experiment with php
   (apache user rights over nmap resources)

   no php ressources on google website,
   noor ajax call (worker) other then 'self'
   
   so
   
   I will use a php trick into the <img> html tag.
   (<img src="mysite.com/gdtrick.php">)

   I'm wondering if I can do the same with, let's say,
   gdtrick.cgi... that would be so cool.


*/
// size of the image to create

$x = 300;
$y = 300;


$displacement_y = 24;

//debug
//$zetext = "test";


$image = imagecreate($x,$y);

$white = imagecolorallocate($image, 255,255,255);
$black = imagecolorallocate($image, 0,0,0);

$zeipaddr = "";
$zetext = "";

function getUserIP() {
    $ipaddress = '';
    if (isset($_SERVER['HTTP_CLIENT_IP']))
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_X_FORWARDED']))
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    else if(isset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
        $ipaddress = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
    else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_FORWARDED']))
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    else if(isset($_SERVER['REMOTE_ADDR']))
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
}

$zeipaddr = getUserIP();

//for debug
//echo "IP: " .$zeipaddr."<br>\n";
//$zeipaddr = "192.168.1.18";


$ports = array(21, 22, 23, 80, 8080, 8081, 8082, 5900, 5901, 5902, 3306, 6000, 6001, 6002, 6003);

//$ports = array(80);

$text1 = imagettftext($image, 12, 0, 12, $displacement_y, $black, "/usr/share/fonts/truetype/freefont/FreeSans.ttf", "PHP SCAN ON DEMAND");
$displacement_y += 12;


foreach ($ports as $port)
{
    $connection = @fsockopen($zeipadrr, $port, $errno, $errstr, 1);

    if (is_resource($connection))
    {
        $zetext = $zeipadrr . ':' . $port . ' ' . '(' . getservbyport($port, 'tcp') . ') is open.' ;
        

        $text1 = imagettftext($image, 10, 0, 6, $displacement_y, $black, "/usr/share/fonts/truetype/freefont/FreeSans.ttf", $zetext);

        fclose($connection);
    }

    else
    {
        $zetext = $zeipaddr . ':' . $port . " not active." ;
        
        $text1 = imagettftext($image, 10, 0, 6, $displacement_y, $black, "/usr/share/fonts/truetype/freefont/FreeSans.ttf", $zetext);

    }
    
    $displacement_y += 16; 
}

$displacement_y += 12; 

$text1 = imagettftext($image, 12, 0, 12, $displacement_y, $black, "/usr/share/fonts/truetype/freefont/FreeSans.ttf", "Port scanning done !!!");


header('Content-Type: image/jpeg');
imagejpeg($image);


?>

My two cents...
(adding text in a loop because of multi-line text requirement)

<?php

error_reporting(E_ALL);
error_reporting(-1);
ini_set('error_reporting', E_ALL);

/*
   construction of the tcp scan
   I would prefer building a C C.G.I.
   but let's experiment with php
   (apache user rights over nmap resources)

   no php ressources on google website,
   noor ajax call (worker) other then 'self'
   
   so
   
   I will use a php trick into the <img> html tag.
   (<img src="mysite.com/gdtrick.php">)

   I'm wondering if I can do the same with, let's say,
   gdtrick.cgi... that would be so cool.


*/
// size of the image to create

$x = 300;
$y = 300;


$displacement_y = 24;

//debug
//$zetext = "test";


$image = imagecreate($x,$y);

$white = imagecolorallocate($image, 255,255,255);
$black = imagecolorallocate($image, 0,0,0);

$zeipaddr = "";
$zetext = "";

function getUserIP() {
    $ipaddress = '';
    if (isset($_SERVER['HTTP_CLIENT_IP']))
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_X_FORWARDED']))
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    else if(isset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
        $ipaddress = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
    else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_FORWARDED']))
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    else if(isset($_SERVER['REMOTE_ADDR']))
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
}

$zeipaddr = getUserIP();

//for debug
//echo "IP: " .$zeipaddr."<br>\n";
//$zeipaddr = "192.168.1.18";


$ports = array(21, 22, 23, 80, 8080, 8081, 8082, 5900, 5901, 5902, 3306, 6000, 6001, 6002, 6003);

//$ports = array(80);

$text1 = imagettftext($image, 12, 0, 12, $displacement_y, $black, "/usr/share/fonts/truetype/freefont/FreeSans.ttf", "PHP SCAN ON DEMAND");
$displacement_y += 12;


foreach ($ports as $port)
{
    $connection = @fsockopen($zeipadrr, $port, $errno, $errstr, 1);

    if (is_resource($connection))
    {
        $zetext = $zeipadrr . ':' . $port . ' ' . '(' . getservbyport($port, 'tcp') . ') is open.' ;
        

        $text1 = imagettftext($image, 10, 0, 6, $displacement_y, $black, "/usr/share/fonts/truetype/freefont/FreeSans.ttf", $zetext);

        fclose($connection);
    }

    else
    {
        $zetext = $zeipaddr . ':' . $port . " not active." ;
        
        $text1 = imagettftext($image, 10, 0, 6, $displacement_y, $black, "/usr/share/fonts/truetype/freefont/FreeSans.ttf", $zetext);

    }
    
    $displacement_y += 16; 
}

$displacement_y += 12; 

$text1 = imagettftext($image, 12, 0, 12, $displacement_y, $black, "/usr/share/fonts/truetype/freefont/FreeSans.ttf", "Port scanning done !!!");


header('Content-Type: image/jpeg');
imagejpeg($image);


?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文