Jump to content

PHP Javascript Help ~ Text Align/Letter Length


CaseyLiam

Recommended Posts

Is there a way I can use the code

$center =

and insert an alignment of some sort, i.e. center?

 

Also, Is there a way that depending on the length in characters they enter above, thats the type of length.

 

Like, If there are 5 characters, the word is centered, but if there are 10 characters, the word is aligned right?

 

Thanks.

Link to comment
Share on other sites

<?php
 $text = 'This is some text';
 $align = 'center';
 if(strlen($text) >= 10){
   $align = 'right';
 }
 print "<p style=\"text-align:{$align};\">{$text}</p>";
?>

 

or you can shorten up that if to:

$align = (strlen($text) >= 10) ? 'right' : 'center' ;

Link to comment
Share on other sites

<?php
  $text = 'This is some text';
  $align = 'center';
  if(strlen($text) >= 10){
    $align = 'right';
  }
  print "<p style=\"text-align:{$align};\">{$text}</p>";
?>

 

or you can shorten up that if to:

$align = (strlen($text) >= 10) ? 'right' : 'center' ;

 

Didn't work D:

 

Here is the entire code:

<?php
header("Content-type: image/png");
include ('habboClass.php');
$habbo = $_GET['name'];
//Data Retrieval
$habboHome = new habboClass($habbo, 'com');

if ($habboHome->banned())
{
    $im = imagecreatefrompng("banned.png");
}
elseif ($habboHome->pageprivate())
{
    $im = imagecreatefrompng("private.png");
}
elseif (!$habboHome->actual())
{
    $im = imagecreatefrompng("exist.png");
}
else
{
    $im = imagecreatefrompng("profile.png");
    
    $white = imagecolorallocate($im, 250, 255, 255);
    $fontbold = "volterb.ttf";
    $font = "volter.ttf";

    $habboFigure = $habboHome->figure();
    $habboFigure = imagecreatefromgif($habboFigure);
    imagecopy($im, $habboFigure, -10, -15, -1, -1, 50, 108);
    
    imagettftext($im, 7, 0, 15.5, 84.5, $black, $fontbold, $habboHome->name());
    imagettftext($im, 7, 0, 15.5, 86.5, $black, $fontbold, $habboHome->name());
    imagettftext($im, 7, 0, 14.5, 85.5, $black, $fontbold, $habboHome->name());
    imagettftext($im, 7, 0, 16.5, 85.5, $black, $fontbold, $habboHome->name());
    imagettftext($im, 7, 0, 15.5, 85.5, $white, $fontbold, $habboHome->name());

    $habboMotto = $habboHome->motto();
    $habboMotto = eregi_replace(">", ">", $habboMotto);
    $habboMotto = eregi_replace("<", "<", $habboMotto);
    imagettftext($im, 7, 0, 103.5, 58.5, $white, $fontbold, "");
    imagettftext($im, 7, 0, 149.5, 58.5, $white, $font, "");

    if ($habboHome->online())
    {
        $statusText = "Online";
    }
    else
    {
        $statusText = "Offline";
    }
    imagettftext($im, 5, 0, 240, 89, $white, $fontbold, "");
    imagettftext($im, 5, 0, 290, 89, $white, $font, "");

}

//Create Image
imagepng($im);
imagedestroy($im);
?>

 

I need the align center code to be like $fontbold except make the text align center.

Link to comment
Share on other sites

OH...you are writing text on an image....

 

you need to take the total width (usually the width of the image) subtract the width of the string (which you can get with imagettfbbox()), divide that value by two, and that will give you the left offset.

Link to comment
Share on other sites

OH...you are writing text on an image....

 

you need to take the total width (usually the width of the image) subtract the width of the string (which you can get with imagettfbbox()), divide that value by two, and that will give you the left offset.

 

Thanks :) Now can you show me how to do that.. ROFL.

Link to comment
Share on other sites

like this:

<?php
  function imagettftext_center($im, $size, $y, $color, $fontfile, $text){
    $box = imagettfbbox($size,0,$fontfile,$text);
    $txt_w = $box[4] - $box[6];
    $img_w = imagesx($im);
    $offset = floor(($img_w - $txt_w) / 2);
    if($offset < 0)
      $offset = 0;
    imagettftext($im,$size,0,$offset,$y,$color,$fontfile,$text);  
  }
  
  $im = imagecreatetruecolor(300, 200);
  $blue = imagecolorallocate($im, 0, 0, 255);
  $black = imagecolorallocate($im, 0, 0, 0);
  $fontbold = "volterb.ttf";

  imagefill($im,0,0,$blue);
  imagettftext_center($im,10,40,$black,$fontbold,'A Simple Text String');

  header ('Content-type: image/png');
  imagepng($im);
  imagedestroy($im);
?>

Link to comment
Share on other sites

<?php
function imagettftext_center($im, $size, $y, $color, $fontfile, $text){
    $box = imagettfbbox($size,0,$fontfile,$text);
    $txt_w = $box[4] - $box[6];
    $img_w = imagesx($im);
    $offset = floor(($img_w - $txt_w) / 2);
    if($offset < 0)
      $offset = 0;
    imagettftext($im,$size,0,$offset,$y,$color,$fontfile,$text);  
  }
header("Content-type: image/png");
include ('habboClass.php');
$habbo = $_GET['name'];
//Data Retrieval
$habboHome = new habboClass($habbo, 'com');

if ($habboHome->banned())
{
    $im = imagecreatefrompng("banned.png");
}
elseif ($habboHome->pageprivate())
{
    $im = imagecreatefrompng("private.png");
}
elseif (!$habboHome->actual())
{
    $im = imagecreatefrompng("exist.png");
}
else
{
    $im = imagecreatefrompng("profile.png");
    
    $white = imagecolorallocate($im, 250, 255, 255);
    $fontbold = "volterb.ttf";
    $font = "volter.ttf";

    $habboFigure = $habboHome->figure();
    $habboFigure = imagecreatefromgif($habboFigure);
    imagecopy($im, $habboFigure, -10, -15, -1, -1, 50, 108);
    
    imagettftext($im, 7, 0, 15.5, 84.5, $black, $fontbold, $habboHome->name());
    imagettftext($im, 7, 0, 15.5, 86.5, $black, $fontbold, $habboHome->name());
    imagettftext($im, 7, 0, 14.5, 85.5, $black, $fontbold, $habboHome->name());
    imagettftext($im, 7, 0, 16.5, 85.5, $black, $fontbold, $habboHome->name());
    imagettftext_center($im, 7, 0, 15.5, 85.5, $white, $fontbold, $habboHome->name());

    $habboMotto = $habboHome->motto();
    $habboMotto = eregi_replace(">", ">", $habboMotto);
    $habboMotto = eregi_replace("<", "<", $habboMotto);
    imagettftext($im, 7, 0, 103.5, 58.5, $white, $fontbold, "");
    imagettftext($im, 7, 0, 149.5, 58.5, $white, $font, "");

    if ($habboHome->online())
    {
        $statusText = "Online";
    }
    else
    {
        $statusText = "Offline";
    }
    imagettftext($im, 5, 0, 240, 89, $white, $fontbold, "");
    imagettftext($im, 5, 0, 290, 89, $white, $font, "");

}

//Create Image
imagepng($im);
imagedestroy($im);
?>

 

Im not sure that I put it in correctly...

Link to comment
Share on other sites

try this:

<?php
header("Content-type: image/png");
include ('habboClass.php');
$habbo = $_GET['name'];
//Data Retrieval
$habboHome = new habboClass($habbo, 'com');

if ($habboHome->banned())
{
    $im = imagecreatefrompng("banned.png");
}
elseif ($habboHome->pageprivate())
{
    $im = imagecreatefrompng("private.png");
}
elseif (!$habboHome->actual())
{
    $im = imagecreatefrompng("exist.png");
}
else
{
    $im = imagecreatefrompng("profile.png");
    
    $white = imagecolorallocate($im, 250, 255, 255);
    $fontbold = "volterb.ttf";
    $font = "volter.ttf";

    $habboFigure = $habboHome->figure();
    $habboFigure = imagecreatefromgif($habboFigure);
    imagecopy($im, $habboFigure, -10, -15, -1, -1, 50, 108);
    
    $font_size = 7;
    $name = $habboHome->name();
    
    //Get Center Offset
    $box = imagettfbbox($font_size,0,$fontbold,$name);
    $txt_w = $box[4] - $box[6];
    $img_w = imagesx($im);
    $offset = floor(($img_w - $txt_w) / 2);
    if($offset < 0)
      $offset = 0;
    
    imagettftext($im, $font_size, 0, $offset, 84.5, $black, $fontbold, $name);
    imagettftext($im, $font_size, 0, $offset, 86.5, $black, $fontbold, $name);
    imagettftext($im, $font_size, 0, $offset - 1, 85.5, $black, $fontbold, $name);
    imagettftext($im, $font_size, 0, $offset + 1, 85.5, $black, $fontbold, $name);
    imagettftext($im, $font_size, 0, $offset, 85.5, $white, $fontbold, $name);

    $habboMotto = $habboHome->motto();
    $habboMotto = eregi_replace(">", ">", $habboMotto);
    $habboMotto = eregi_replace("<", "<", $habboMotto);
    imagettftext($im, 7, 0, 103.5, 58.5, $white, $fontbold, "");
    imagettftext($im, 7, 0, 149.5, 58.5, $white, $font, "");

    if ($habboHome->online())
    {
        $statusText = "Online";
    }
    else
    {
        $statusText = "Offline";
    }
    imagettftext($im, 5, 0, 240, 89, $white, $fontbold, "");
    imagettftext($im, 5, 0, 290, 89, $white, $font, "");

}

//Create Image
imagepng($im);
imagedestroy($im);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.