簡體   English   中英

我可以使用 GD 和 PHP 多行文本嗎?

[英]can I have multiline text with GD and PHP?

我正在嘗試使用 GD+PHP 輸出多行文本,但無法正常工作。 我的 php 知識非常基礎。 這是代碼,知道如何輸出 2 或 3 行文本嗎?

$theText = (isset($_GET['caption']))? stripslashes($_GET['caption']) :'';
imagettftext($baseImage, $textSize, $textAngle, $textXposition, $textYposition, $textColor, $fontName, $theText);
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 位置。 請記住,您的整個圖像應該足夠高和足夠寬以適合文本。

API 不支持它。 這是“手動”執行此操作的代碼:

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

我有一個未知長度的字符串,但只能使用一定的寬度。 所以我想出了這個。 基本上它將句子拆分為字符。 如果它碰到一個空格,它會檢查這個詞是否可以添加到前一行,如果不能,它會開始一個新行。 對於剛剛被砍掉的超長單詞也有一個蹩腳的安全措施,以免脫離圖像。

在我實際將文本打印到圖像的階段,我檢查該行是否小於允許的最大字符數並添加前導 + 尾隨空格,以模擬 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;

        }
    }

不要忘記將最后一個單詞添加到一行中。 您還需要檢查它是否應該在新行上。

在圖像中添加線條:

//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;
    }

您可以每行重復一個 imagettftext; 只需將$theText拆分為一個數組(分隔符是 NewLine)並循環數組中的每個元素,將$textYposition增加行的高度(請參閱$textSize ,但實際上您會使用imageftbbox獲得更好的imageftbbox閱讀頁面在 PHP 手冊中

我的兩分錢......(由於多行文本要求,在循環中添加文本)

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


?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM