mercredi 12 février 2020

Loosing \n In String After Changing if/else to case Conditional

Somehow changing this conditional from an if/else to a case has caused the \n in certain $String values to be lost so that the textimage() function isn't receiving them. I thought it was the change from $_GET['Name'] to current($_GET) but it seems to also do in on $String values that are created dynamically rather than coming from the $_GET. What might be causing the \n to disappear on the receiving end? It is getting through but is apparently being encoded in some way as it is showing up in the image rather than breaking $String into multiple lines.

An example with \n is in the first case of the conditional.

// This script generates an image using text in the specified TTF font

// Settings for this site
$FontPath = "/internals/truetype/";
$FontName = "vineritc.ttf";
$EnableShadow=TRUE;
$CenterAlign=TRUE;

$GETkey = key($_GET);
$GETvalue = current($_GET);

switch ($GETkey):
    case "AuthorID":
        $Year = date("Y");
        $AuthorName = DBLookup("SELECT `AuthorName` FROM authorbiographies WHERE `ID`=$GETvalue",$siteDB);
        $String = "Copyright © $Year\n$AuthorName";
        $FontSize = 10;
        $Angle = 0;
    break;
    case "CategoryID":
        $String = DBLookup("SELECT `CategoryName` FROM categories WHERE `ID`=$GETvalue",$siteDB);
        $FontSize = 15;
        $Angle = 0;From $_GET['Name'] to current($_GET)
    break;
    case "Home":
        $String = $GETvalue;
        $FontSize = 28;
        $Angle = 0;
    break;
    case "Splash":
        $String = $GETvalue;
        $FontSize = 38;
        $Angle = 0;
    break;
endswitch;

$TextImage = textimage();

header("Pragma: no-cache");
header('Content-Type: image/png');

echo $TextImage;

Here is the textimage() function for reference but only the code above was modified - nothing was changed in the code below:

// Set these values on calling page
function textimage() {
    global $FontPath;
    global $FontName;
    global $String;
    global $FontSize;
    global $Angle;
    global $EnableShadow;
    global $CenterAlign;

//if (Contains("\n",$String)) :
    //$_SESSION['testString'] = $String;
    //unset($_SESSION['testString']);
//endif;

    // Create additional values and calculations
    $Font = "{$_SERVER['DOCUMENT_ROOT']}$FontPath$FontName";
    $String = html_entity_decode($String);

    // Get sizes and positions of text string
    $FontDetails = calculateTextBox($String,$Font,$FontSize,$Angle);

    //$TextLeft = $FontDetails['left']; // REFERENCE ONLY, NOT IN USE
    //$TextTop = $FontDetails['top']; // REFERENCE ONLY, NOT IN USE
    $Width = $FontDetails['width'];
    $Height = $FontDetails['height']; // REFERENCE ONLY, NOT USED HERE

    $LL_X   =   $FontDetails['box'][0];
    $LL_Y   =   $FontDetails['box'][1];
    $LR_X   =   $FontDetails['box'][2];
    //$LR_Y =   $FontDetails['box'][3];  // REFERENCE ONLY, NOT USED HERE
    $UR_X   =   $FontDetails['box'][4];
    $UR_Y   =   $FontDetails['box'][5];
    $UL_X   =   $FontDetails['box'][6];
    //$UL_Y =   $FontDetails['box'][7]; // REFERENCE ONLY, NOT USED HERE

    $Width = abs($Width - $LL_X) + 20;
    $PadHeight = ($EnableShadow === TRUE) ? 8: 0; // If enabled, pad bottom to allow for shadow
    $Image = imagecreatetruecolor($Width,$Height+$PadHeight);
    imagesavealpha($Image, TRUE);
    $TransColor = imagecolorallocatealpha($Image, 0, 0, 0, 127);
    imagefill($Image, 0, 0, $TransColor);
    $TextColor = imagecolorallocate($Image,0x00,0x00,0x55);
    $ShadowColor = imagecolorallocate($Image,0x77,0x77,0x77);
    $Lines = explode("\n",$String);

    foreach ($Lines as $Line):
        if ($EnableShadow === TRUE):
            if ($CenterAlign === TRUE):
                $x_offset = CenterText($FontSize,$Font,$Line,$Width,$Angle);
            else:
                $x_offset = ($Width / 2) - ((min($LR_X,$UR_X) - max($LL_X,$UL_X)) / 2);
            endif;
            imagettftextblur($Image,$FontSize,$Angle,$x_offset+5,abs($UR_Y)+5,$ShadowColor,$Font,$Line,10);
            imagettftextblur($Image,$FontSize,$Angle,$x_offset,abs($UR_Y),$TextColor,$Font,$Line);
        else:
            if ($CenterAlign === TRUE):
                $x_offset = CenterText($FontSize,$Font,$Line,$Width,$Angle);
            else:
                $x_offset = ($Width / 2) - ((min($LR_X,$UR_X) - max($LL_X,$UL_X)) / 2);
            endif;
            imagettftext($Image,$FontSize,$Angle,$x_offset,abs($UR_Y),$TextColor,$Font,$Line);
        endif;
    endforeach;

    imagepng($Image);
    imagedestroy($Image);
}

Aucun commentaire:

Enregistrer un commentaire