well i have started my next project.
its a quick example of how i will generate a quote to a image

http://www.thermomods.com/files/quote.php

but i need some help breaking the quote line into 2 lines, here is my code

Code:

<?php
header ("Content-type: image/png");
$img_handle = imagecreatefrompng("quote.png");
$color = imagecolorallocate ($img_handle, 100, 100, 100);

$random = "quote.txt";
$fp = file($random);
srand((double)microtime()*1000000);
$rl = $fp[array_rand($fp)];
$input_array = explode(' (',$rl);
$line1 = $input_array[0];
$line = explode('; ',$input_array[1]);   
$line2 = $line[2];

imagestring ($img_handle, 3, 10, 5, "$line1", $color);
imagestring ($img_handle, 3, 10, 15, "$line2", $color);
imagepng ($img_handle);
imagedestroy ($img_handle);
?>

can someone help? I have noticed that it does nothing to split the line, but it does read it
Umm, why are you splitting with a parenthesis? Clearly you don't understand how the split( / explode( command works, because you're using what I gave you for a completely different application. Try this:


Code:
<?php
header ("Content-type: image/png");
$img_handle = imagecreatefrompng("quote.png");
$color = imagecolorallocate ($img_handle, 100, 100, 100);

$random = "quote.txt";
$fp = file($random);
srand((double)microtime()*1000000);
$rl = $fp[array_rand($fp)];
$line1 = substr($rl,0,round(strlen($rl)/2,0));
$line2 = substr($rl,round(strlen($rl)/2,0),strlen($rl)-$rl,round(strlen($rl)/2,0));

imagestring ($img_handle, 3, 10, 5, "$line1", $color);
imagestring ($img_handle, 3, 10, 15, "$line2", $color);
imagepng ($img_handle);
imagedestroy ($img_handle);
?>
KermMartian wrote:
Umm, why are you splitting with a parenthesis? Clearly you don't understand how the split( / explode( command works, because you're using what I gave you for a completely different application.


Yea i didn't do much reading yet, it was a quick write.
EDIT
it didn't work gave me errors
Thermoman wrote:
KermMartian wrote:
Umm, why are you splitting with a parenthesis? Clearly you don't understand how the split( / explode( command works, because you're using what I gave you for a completely different application.


Yea i didn't do much reading yet, it was a quick write.
EDIT
it didn't work gave me errors
... and would you care to share what the errors were with us? Razz
well, if you click the link, it says the img can not be displayed.
haha yes rivereye, thats why i didn't post an error...
anyway im going to work on this later tonight when its not as hot.
thanks Kerm for your help tho.
Tell me what errors you get from this:


Code:
<?php
//header ("Content-type: image/png");
$img_handle = imagecreatefrompng("quote.png");
$color = imagecolorallocate ($img_handle, 100, 100, 100);

$random = "quote.txt";
$fp = file($random);
srand((double)microtime()*1000000);
$rl = $fp[array_rand($fp)];
$line1 = substr($rl,0,round(strlen($rl)/2,0));
$line2 = substr($rl,round(strlen($rl)/2,0),strlen($rl)-$rl,round(strlen($rl)/2,0));

imagestring ($img_handle, 3, 10, 5, "$line1", $color);
imagestring ($img_handle, 3, 10, 15, "$line2", $color);
imagepng ($img_handle);
imagedestroy ($img_handle);
?>
Warning: Wrong parameter count for substr() in /home/.cuddles/thermoman/thermomods.com/files/quote.php on line 11

Code:
<?php
header ("Content-type: image/png");
$img_handle = imagecreatefrompng("quote.png");
$color = imagecolorallocate ($img_handle, 100, 100, 100);

$random = "quote.txt";
$fp = file($random);
srand((double)microtime()*1000000);
$rl = $fp[array_rand($fp)];
$line1 = substr($rl,0,round(strlen($rl)/2,0));
$line2 = substr($rl,round(strlen($rl)/2,0),strlen($rl)-round(strlen($rl)/2,0));

imagestring ($img_handle, 3, 10, 5, "$line1", $color);
imagestring ($img_handle, 3, 10, 15, "$line2", $color);
imagepng ($img_handle);
imagedestroy ($img_handle);
?>
wow that works.. i will have to read up more on that function, i think i'll also keep working on learning php, maybe i might just be as good as Kerm is, i hope.
hehe cananyone get rid of the VT at the end of the quote, well i will try to but if i can't i'll come here
Here you go. It's just an extra null character being nailed onto the end of the string:


Code:
<?php
header ("Content-type: image/png");
$img_handle = imagecreatefrompng("quote.png");
$color = imagecolorallocate ($img_handle, 100, 100, 100);

$random = "quote.txt";
$fp = file($random);
srand((double)microtime()*1000000);
$rl = $fp[array_rand($fp)];
$line1 = substr($rl,0,round(strlen($rl)/2,0));
$line2 = substr($rl,round(strlen($rl)/2,0),strlen($rl)-round(strlen($rl)/2,0)-1);

imagestring ($img_handle, 3, 10, 5, "$line1", $color);
imagestring ($img_handle, 3, 10, 15, "$line2", $color);
imagepng ($img_handle);
imagedestroy ($img_handle);
?>
Notice the "-1" in the substr command, for the length argument.
i am having problems w/ the lines just breaking mid word, can we make it detect the words r grouped and have it skip to the next line to stay all on one
Sure. What you would want to do is first get the location of the middle of the string using this stored into a variable:
Code:
round(strlen($rl)/2
The round() is necessary because it eliminates the decimal point if the string has an odd number of characters. Then do a while( loop where you search each character for a space ' '. When you find it, the while loop's conditional will be met, and you'll have the final position to break at in the variable. I know you said not to post code showing the whole thing, so I'll leave it to you to piece it together.
ok Kerm i have been reading.. so i started w/ this, but im not sure if im right or what to do

Code:

$line2 = substr($rl,round(strlen($rl)/2,0),strlen($rl)-round(strlen($rl)/2,0)-1);
while (strrchr($rl," ") {
}

OH! also in the round() things u alrdy had you don't need the ,0) its 0 by default
$line1 = substr($rl,0,round(strlen($rl)/2,0));
so it could just be this: no?
$line1 = substr($rl,0,round(strlen($rl)/2));
Very good! But for your while loop, you need a variable to keep track of position, so try this:


Code:
$rl = $fp[array_rand($fp)];
$n = round(strlen($rl)/2);
while(substr($rl,$n,1) != ' ') { $n++; }
$line1 = substr($rl,0,$n);
$line2 = substr($rl,$n,strlen($rl)-strlen($line1)-1);
lol i would never have gotten that...
but was i on the right track w/ strrchr?
it also didn't occur to me that i could break it up like that, i think i need a small break lol to much in such a short time, i should have seen that
Nope, not on the right track at all. Wink strstr() is a function for looking for strings inside other strings, like for looking for "base" in "all your base are belong to us". It lets you know if there is such a substring, and if so, where it is. You had insufficient arguments for strstr/strchr anyway. (They're the same function)
KermMartian wrote:
...strstr/strchr anyway. (They're the same function)

Which is why PHP is a messy language. Too many functions have duplicates. Also, case sensitive functions and their insensitive ones don't have a set pattern (strstr/stristr, ereg/eregi, str_replace/str_ireplace).
Nice, but your server is reaaaally slow serving that out. Looks relatively nice now though.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 2
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement