Shoutbox

PHP help needed - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: PHP help needed (/showthread.php?tid=43041)

PHP help needed by Ezra on 04-17-2005 at 05:22 PM

I already posted this on a php forum, but it's been a day without an answer so i thought I might post it here...

I'm trying to make a random quote script, I have a txt file with the quotes, every line a new one, this works... But I want to output it on a image and this is where  I need the help.

I want the image to get the size of the line automaticly (so ig the text is Ezra it should be as long as that, and if it's longer it should get wider automaticly), but i also want it too not get too long, like max 300px and if the line is longer, I want it to continue on the next line.
Like this:
Ezra is
cool

This is the code I have so far:

code:
<?
//Header information
header("Content-type: image/gif");

//Get the quote from the file
function randomNummer($van, $tot)
{
    srand ((double) microtime() * 1000000); // seed the generator
    $return = rand($van,$tot);
     
    return($return);
}

$textLijst = 'randomquote.txt'; // the quotes are in here, on every line a new quote..

$textArray = file($textLijst); // The lines in an array.

$textCount = count($textArray); // How many lines are there??

$randomRegelNummer = randomNummer(0, ($textCount - 1)); // the number of a line.

$textRegel = $textArray[$randomRegelNummer]; // a random line number from the file

//Variabels
$im = @imagecreate(100, 100);
$background_color = imagecolorallocate($im, 100, 100, 100);
$text_color = imagecolorallocate($im, 000, 000, 200);

//Image Code
imagecolortransparent($im, $background_color);
imagestring($im, 5, 7, 3, $textRegel, $text_color);
imagegif($im);
imagedestroy($im);

?>

RE: PHP help needer by KeyStorm on 04-17-2005 at 05:53 PM

I did something like this for a shoutcast radio, let's see if it helps:

Variables you need:
$text: the actual string you want to output
$size: the size of the font
$angle: the angle of the font (0 for horizontal)
$maxlen: the maximum length that should be allowed (for 300px it's ok to set 290 in here)

code:
$slices = explode(' ',$text); 
                        $split = count($slices);
                        do{
                                $split--;
                                $out1 = NULL;
                                for($i=0;$i<=$split;$i++)
                                    {
                                        $out1 .= $slices[$i]." ";
                                    }
                               
                                $break = imagettfbbox($size,$angle,$fontpath,$out1);
                            } while ($break[2] > $maxlen);
$out2 = NULL;
                        for($j=$i; $j<count($slices); $j++)
                            {
                                $out2 .= $slices[$j].' ';
                            }


Once this is run, you will have following values:
$break[2] will tell you the width of the line
$out1 will be the first line and its width will be $break[2]
$out2 the second line

After this you can use both strings and print them into the image using imagettftext:

code:
$im = imagecreate($break[2]+10,50);
$background_color = imagecolorallocate($im, 100, 100, 100);
$text_color = imagecolorallocate($im, 000, 000, 200);
imagecolortransparent($im, $background_color);
imagettftext($img,$size,$angle, 5, 15, $text_color, $font, $out1."\n\r".$out2);
imagegif($im);
imagedestroy($im);


This may work, but if you are intending to do a transparent image with text you will get very ugly text borders. Either use a background or a PNG24 format.
RE: PHP help needer by Ezra on 04-17-2005 at 07:32 PM

Thanks for the reply KeyStorm :), However, i'm still having trouble :(

I added your code to mine and ended up with this:

code:
<?
//Header information
header("Content-type: image/gif");

//Get the quote from the file
function randomNummer($van, $tot)
{
    srand ((double) microtime() * 1000000); // seed the generator
    $return = rand($van,$tot);
   
    return($return);
}

$textLijst = 'randomquote.txt'; // the quotes are in here, on every line a new quote..

$textArray = file($textLijst); // The lines in an array.

$textCount = count($textArray); // How many lines are there??

$randomRegelNummer = randomNummer(0, ($textCount - 1)); // the number of a line.

$textRegel = $textArray[$randomRegelNummer]; // a random line number from the file

//Variabels
//$text = $textRegel; //the actual string you want to output
$size = 4; //the size of the font
$angle = 0; //the angle of the font (0 for horizontal)
$maxlen = 290; //the maximum length that should be allowed (for 300px it's ok to set 290 in here)

// Keystorms Improvemend by slicing

$slices = explode(' ',$textRegel); 
$split = count($slices);
do{
$split--;
$out1 = NULL;
for($i=0;$i<=$split;$i++)
{
$out1 .= $slices[$i]." ";
}

$break = imagettfbbox($size,$angle,$fontpath,$out1);
} while ($break[2] > $maxlen);
$out2 = NULL;
for($j=$i; $j<count($slices); $j++)
{
$out2 .= $slices[$j].' ';
}

//Image code, edited by Keystorm

$im = imagecreate($break[2]+10,50);
$background_color = imagecolorallocate($im, 100, 100, 100);
$text_color = imagecolorallocate($im, 000, 000, 200);
imagecolortransparent($im, $background_color);
imagettftext($img,$size,$angle, 5, 15, $text_color, $font, $out1."\n\r".$out2);
imagegif($im);
imagedestroy($im);
?>

But the image doesn't get outputted, this is what I get:
http://school.tsdme.nl/randomquote/plaatje2.php

Yes, i'm a php n00b :P, and I'm trying to do things that I can't, but i'm still learning...
RE: PHP help needed by KeyStorm on 04-17-2005 at 08:56 PM

Learning is better than just asking ;)

What you can try to do to see if everything's allright with the code is to comment the header() function, because we may be missing all PHP warnings and errors it may output.


RE: PHP help needed by Ezra on 04-17-2005 at 09:25 PM

No errors, just this: GIF89a  and some things that I can't copy :S


RE: PHP help needed by KeyStorm on 04-17-2005 at 09:42 PM

Btw, the server will not allow you to hotlink it (to use it in the forums or whatever outside the server).

Leave it like this and add following line at the end of the script:

echo "</br>".$out1."</br>".$out2;


RE: PHP help needed by Ezra on 04-17-2005 at 09:47 PM

I know about the hotlink protection I added it myself :d, I'll upload it to another server when it's finished

EDIT: Added that last echo and it echo's the string normal...

I'm going to bed now... I'll look into it again tomorrow. G'Nite!

EDIT2: Replaced that last echo and it gave this error:

code:
Parse error: parse error, unexpected T_BREAK in /data/members/paid/t/s/tsdme.nl/htdocs/school/randomquote/plaatje2.php on line 61

RE: PHP help needed by KeyStorm on 04-17-2005 at 09:54 PM

Replace it now with
echo "</br>".$out1."</br>".$out2."</br>".break[2];


RE: PHP help needed by Ezra on 04-18-2005 at 12:07 PM

Allright, I got it working but I got one problem... My server is linux and doesn't work with .ttf files, do you know how to fix this?

And, if you could explain a little how that slice piece of the script works so I can edit it, to be more lines than 2, like 4 lines.


RE: PHP help needed by KeyStorm on 04-18-2005 at 12:56 PM

It does work with ttf files if you're using GD2, you just need to put the absolute path to the ttf (I've experienced problems when refering to a relative path).

Thas algorithm first separated all word in the string and put them into an array (splitting the spaces). After that we rejoin the whole array into a new variable ($out1) and check the length. If it's too long, we decrement $split (which is the last piece of the array we will join into $out1). This means thatwe will try each time one word less until it no longer isbigger than maxlen.

Once this done, we keep $out1 and rejoin the rest of the array into $out2.

Having $out2, you can repeat the process as if it was the original text: split, join decrementally until it fits, join the rest into a new variable $out3. And so on

code:
// Keystorms Improvemend by slicing

$slices = explode(' ',$textRegel); 
$split = count($slices);
do{
$split--;
$out1 = NULL;
for($i=0;$i<=$split;$i++)
{
$out1 .= $slices[$i]." ";
}

$break = imagettfbbox($size,$angle,$fontpath,$out1);
} while ($break[2] > $maxlen);
$out2 = NULL;
for($j=$i; $j<count($slices); $j++)
{
$out2 .= $slices[$j].' ';
}

$slices = explode(' ',$out2); 
$split = count($slices);
do{
$split--;
$out2 = NULL;
for($i=0;$i<=$split;$i++)
{
$out2 .= $slices[$i]." ";
}

$break = imagettfbbox($size,$angle,$fontpath,$out2);
} while ($break[2] > $maxlen);
$out3 = NULL;
for($j=$i; $j<count($slices); $j++)
{
$out3 .= $slices[$j].' ';
}
...


Edit: I forgot to remind you about the height of the image. Check whether $out2, $out3 and $out4 are filled with something (strlen($outX) > 0, for example) and decide the height from those checks.