jackwh Posted September 4, 2009 Share Posted September 4, 2009 Hey folks, Basically my problem is a frustrating one, and before you asked, I have ! I have created a basic tool that takes a user's latest Status update and saves it to an image. I need the image to regenerate every 15 minutes with the latest update to the status. I have created a script called regenerate.php that I plan to set on a Cron Job. The user's info is stored in the database. Regenerate.php is meant to scroll through the data row by row, query the status update, save it to an image, and move on to the next row. I got the code from the book "Head first PHP & MySQL", a good but different book. I have just adapted it slightly to fit my scenario instead. Here is the important portion of regenerate.php code: <?php // First, we connect to database (with variable $cxn) include($_SERVER['DOCUMENT_ROOT'] . "/mysql/connecttodb.php"); // Select the username and password for each user from the table $query = "SELECT username, password, designid, fontid, tweet FROM twitteraccounts"; // Query it $result = mysqli_query($cxn, $query); // Begin the loop that goes through each user while ($row = mysqli_fetch_array($result)) // LINE 18 { // We have all the user's information $username = $row['username']; echo $username; echo "\n"; $password = $row['password']; $designid = $row['designid']; echo $designid; echo "\n"; $fontid = $row['fontid']; echo $fontid; echo "\n"; // Fetch their latest tweet include($_SERVER['DOCUMENT_ROOT'] . "/fetchtweetfromdb.php"); // Tweet has been fetched and written to database. Now fetch it again from the database $tweet = $row['tweet']; // This part of the script checks which designid the user is using, then calls the generator script appropriate for it if ($designid == 'design1') { include("design1.php"); } else { echo "Add more designs to the controller, regenerate.php, roughly line 42ish"; } } ?> Sorry about the messy code, there are quite a few 'placeholders' in there for adding options later on. Thing is, I've done the design, put it into XHTML and CSS, done all the rest of the code, this is just the last hurdle! When I run the file regenerate.php, this is what appears in the browser window: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\Tweetle\site\alpha\userimages\regenerate.php on line 18 (I've commented line 18 in my earlier code example). Why won't it work, and why do I get this error?! Sorry if this is a n00bish question but I'm really struggling here! I thought parameter 1 WAS mysqli_result?! Quote Link to comment https://forums.phpfreaks.com/topic/173137-code-isnt-working-yet-is-practically-copied-out-of-a-php-book/ Share on other sites More sharing options...
rhodesa Posted September 4, 2009 Share Posted September 4, 2009 change this: $result = mysqli_query($cxn, $query); to $result = mysqli_query($cxn, $query) or die(mysqli_error($cxn)); Quote Link to comment https://forums.phpfreaks.com/topic/173137-code-isnt-working-yet-is-practically-copied-out-of-a-php-book/#findComment-912558 Share on other sites More sharing options...
jackwh Posted September 6, 2009 Author Share Posted September 6, 2009 Hey rhodesa, thanks for the help. I have changed that line in regenerate.php but now the plot thickens. The browser output, with $result = mysqli_query($cxn, $query); looks like this: JackWebbHeller design1 font1 GIF87aàP„‚ü„ÂüD¢üÄâü$’ü¤Òüd²üäòüŠü”ÊüTªüÔêü4šü´Úütºüôúü†üŒÆüL¦üÌæü,–ü¬Öül¶üìöüŽüœÎü\®üÜîü<žü¼Þü|¾üüþü,àPþà'Ždižhª®lë¾p,Ïtmßx®ï|ïÿÀ pH,ȤrÉl:ŸÐ¨tJ Z¯Ø¬vËíz¿à°xL.›Ïè´zÍn»ßð¸|N¯Ûïø¼~Ïïûÿ€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£. ¦¦$«G®A«©¤´•«§%°E½?·§µÃ‘Æ'¿CÌ=ÉÆÄÒŽ¿°"Ù#" &¦ëáÚ§°©ïù"ÄÃààDµkä¦)t´MX:XЮ€"Pü@€6¶ÁƒÄÁþXÑÛ€C†0(Ѐ@¶4ÈPr¡ÏK 7<€PP„ "xÁ5Fh`À€8|ÀÅRØJS DdP@DÞ™-û³ ¤ ì{샤\5P®#;Œû eÀz«\uAÁn#C j £_>Ä]°A.= C[V<×®\§^Iz‚ÐÚIž ®gºÊ>ðÀšf²‚Ï}x[O >‹Ê«=¯×*P€@»ú¡ ,5šÐE‡†«€8½í²®´9dóŘ(€ð‡³ö ëôƒˆL4Ï€Ö×@ãÁ äT€W@À@` œsnЙ4@V׸߉xôÇ‘†(xÐcE”7’&"ÈÒ!iÄ™±†â‘8ƒä’L*Éä“': å”TViå•Xf©å–\véå—`†)æ˜d–iæ™h¦©æšl¶éæ›pÆ)çœtÖiçxæ©çž|öé矀*è „j衈&ªè¢Œ6êè£F*餔Vj饘fªé¦œvêé§ †*ꨤ–jꩨ¦ªêª¬¶êê«°Æ*무Öjë ¸æªë®¼z; Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\Tweetle\site\alpha\userimages\regenerate.php on line 18 connection closed I'm assuming all that gobbledygook is the GIF image outputting weirdly? I'm just ignoring that for the time being. The output when using your fix, $result = mysqli_query($cxn, $query) or die(mysqli_error($cxn)); looks like this: JackWebbHeller design1 font1 GIF87aàP„‚ü„ÂüD¢üÄâü$’ü¤Òüd²üäòüŠü”ÊüTªüÔêü4šü´Úütºüôúü†üŒÆüL¦üÌæü,–ü¬Öül¶üìöüŽüœÎü\®üÜîü<žü¼Þü|¾üüþü,àPþà'Ždižhª®lë¾p,Ïtmßx®ï|ïÿÀ pH,ȤrÉl:ŸÐ¨tJ Z¯Ø¬vËíz¿à°xL.›Ïè´zÍn»ßð¸|N¯Ûïø¼~Ïïûÿ€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£. ¦¦$«G®A«©¤´•«§%°E½?·§µÃ‘Æ'¿CÌ=ÉÆÄÒŽ¿°"Ù#" &¦ëáÚ§°©ïù"ÄÃààDµkä¦)t´MX:XЮ€"Pü@€6¶ÁƒÄÁþXÑÛ€C†0(Ѐ@¶4ÈPr¡ÏK 7<€PP„ "xÁ5Fh`À€8|ÀÅRØJS DdP@DÞ™-û³ ¤ ì{샤\5P®#;Œû eÀz«\uAÁn#C j £_>Ä]°A.= C[V<×®\§^Iz‚ÐÚIž ®gºÊ>ðÀšf²‚Ï}x[O >‹Ê«=¯×*P€@»ú¡ ,5šÐE‡†«€8½í²®´9dóŘ(€ð‡³ö ëôƒˆL4Ï€Ö×@ãÁ äT€W@À@` œsnЙ4@V׸߉xôÇ‘†(xÐcE”7’&"ÈÒ!iÄ™±†â‘8ƒä’L*Éä“': å”TViå•Xf©å–\véå—`†)æ˜d–iæ™h¦©æšl¶éæ›pÆ)çœtÖiçxæ©çž|öé矀*è „j衈&ªè¢Œ6êè£F*餔Vj饘fªé¦œvêé§ †*ꨤ–jꩨ¦ªêª¬¶êê«°Æ*무Öjë ¸æªë®¼z; Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\Tweetle\site\alpha\userimages\regenerate.php on line 18 connection closed Exactly the same! Any ideas what's going on, anyone? Quote Link to comment https://forums.phpfreaks.com/topic/173137-code-isnt-working-yet-is-practically-copied-out-of-a-php-book/#findComment-913559 Share on other sites More sharing options...
Jnerocorp Posted September 6, 2009 Share Posted September 6, 2009 try this <?php // First, we connect to database (with variable $cxn) include($_SERVER['DOCUMENT_ROOT'] . "/mysql/connecttodb.php"); // Select the username and password for each user from the table $query = "SELECT username, password, designid, fontid, tweet FROM twitteraccounts"; // Query it $result = mysqli_query($cxn, $query)"; // Begin the loop that goes through each user while ($row = mysqli_fetch_array($result)) // LINE 18 { // We have all the user's information $username = $row['username']; echo $username; echo "\n"; $password = $row['password']; $designid = $row['designid']; echo $designid; echo "\n"; $fontid = $row['fontid']; echo $fontid; echo "\n"; // Fetch their latest tweet include($_SERVER['DOCUMENT_ROOT'] . "/fetchtweetfromdb.php"); // Tweet has been fetched and written to database. Now fetch it again from the database $tweet = $row['tweet']; // This part of the script checks which designid the user is using, then calls the generator script appropriate for it if ($designid == 'design1') { include("design1.php"); } else { echo "Add more designs to the controller, regenerate.php, roughly line 42ish"; } } ?> What I changed: This: $query = "SELECT username, password, designid, fontid, tweet FROM twitteraccounts"; // Query it $result = mysqli_query($cxn, $query); to this $query = "SELECT username, password, designid, fontid, tweet FROM twitteraccounts"; // Query it $result = mysqli_query($cxn, $query)"; Quote Link to comment https://forums.phpfreaks.com/topic/173137-code-isnt-working-yet-is-practically-copied-out-of-a-php-book/#findComment-913567 Share on other sites More sharing options...
gevans Posted September 6, 2009 Share Posted September 6, 2009 You ahve your query and connection variables the wrong way around; $result = mysqli_query($cxn, $query); should be $result = mysqli_query($query, $cxn); Quote Link to comment https://forums.phpfreaks.com/topic/173137-code-isnt-working-yet-is-practically-copied-out-of-a-php-book/#findComment-913569 Share on other sites More sharing options...
gevans Posted September 6, 2009 Share Posted September 6, 2009 What I changed: This: $query = "SELECT username, password, designid, fontid, tweet FROM twitteraccounts"; // Query it $result = mysqli_query($cxn, $query); to this $query = "SELECT username, password, designid, fontid, tweet FROM twitteraccounts"; // Query it $result = mysqli_query($cxn, $query)"; Don't make that chage. Adding the quote after the function parnthesis will cause a syntax error Quote Link to comment https://forums.phpfreaks.com/topic/173137-code-isnt-working-yet-is-practically-copied-out-of-a-php-book/#findComment-913570 Share on other sites More sharing options...
jackwh Posted September 6, 2009 Author Share Posted September 6, 2009 OK guys this is a little confusing to a n00b like me, all different people telling me different things! I really appreciate your help, but let's get things straight first: OK: Jnerocorp: I believe gevans is correct, adding a quote afterwards will generate a syntax error. You can see it in your code highlighting, almost everything afterwards is red (part of the quote). But thank you for trying anyway. Maybe you did not mean to put that? gevans, I believe I did NOT get mysqli_query $cxn and $query the wrong way around. I am using Dreamweaver and Dreamweaver's autofill says it is the way I put it, as does the PHP manual. I quote from the manual: mixed mysqli_query ( mysqli $link , string $query [, int $resultmode ] ) I had $result = mysqli_query($cxn, $query); which matches with the manual's example. When I DO however try it your way around, I get this error on the browser output: Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\wamp\www\Tweetle\site\alpha\userimages\regenerate.php on line 14 Meaning that I was correct, sorry. So does anyone have any further ideas? This is all a little confusing for me... Quote Link to comment https://forums.phpfreaks.com/topic/173137-code-isnt-working-yet-is-practically-copied-out-of-a-php-book/#findComment-913574 Share on other sites More sharing options...
Jnerocorp Posted September 6, 2009 Share Posted September 6, 2009 true i didnt realize that I will see if i can figure anything else out sorry about that Quote Link to comment https://forums.phpfreaks.com/topic/173137-code-isnt-working-yet-is-practically-copied-out-of-a-php-book/#findComment-913578 Share on other sites More sharing options...
PFMaBiSmAd Posted September 6, 2009 Share Posted September 6, 2009 Back to reply #2 (the other posts after that contain wrong information and are not relevant - the quote is just plain wrong and the $cxn, $query order is correct for mysqli) If you added the or die(...) and did not get any different result, then the code you are changing is not the code that is being executed. I'm assuming all that gobbledygook is the GIF image outputting weirdly? I'm just ignoring that for the time being. What GIF? None of the code you have posted concerns outputting an image and since you cannot output image data and echo text from php on one page, that makes no sense. What is the actual and complete code you are using on this page? xxxxx out any sensitive information but don't change any of the syntax in the code. And since the php error message you have been posting is likely in one of the include files, you will need to post them as well if you expect to get actual help with what is causing the problem. Quote Link to comment https://forums.phpfreaks.com/topic/173137-code-isnt-working-yet-is-practically-copied-out-of-a-php-book/#findComment-913579 Share on other sites More sharing options...
jackwh Posted September 6, 2009 Author Share Posted September 6, 2009 What GIF? Nothing you have posted concerns outputting an image and since you cannot output image data and echo text from php on one page, that makes no sense. What is the actual and complete code you are using on this page? xxxxx out any sensitive information but don't change an of the syntax of the page. And since the php error message you have been posting is likely in one of the include files, you will need to post them as well if you expect to get actual help with what is causing the problem. Aha, sorry, didn't really explain did I?! I'll walk you through. Basically my tool connects to a user's Twitter account, takes their tweet and, using GD Library, puts it into an image. The user begins by going to the website, putting in their Twitter username and password, choosing a font and design (that's what the 'design1' and 'font1' variables are), and clicking Go. The script I've written uses the Twitter API to connect to their account, read their latest update, and write it onto an image. Now this is all good for a few minutes, but then when the user Tweets again next time, I need the image to be updated with their newest tweet. This is what the script 'regenerate.php' does (or at least, is meant to do!). So now you have a rough idea of what I need to achieve. I'm pretty happy because I've coded and designed the entire site myself, which in my opinion is a reasonably good achievement for a 15 year old who's only been learning PHP for about a month and a half So now you know, I'll post all the scripts I'm using for regenerate.php and my database formatting, and hopefully someone really kind can piece this together and work out what's wrong. I've commented most of my documents quite heavily as I'm still learning. Let's begin with regenerate.php: <?php // SCRIPT: regenerate.php // FUNCTION: Scrolls through the user database every 15 minutes (CRON JOB) and refreshes their Tweets // First, we connect to database (with variable $cxn) include($_SERVER['DOCUMENT_ROOT'] . "/mysql/connecttodb.php"); // Select the username and password for each user from the table $query = "SELECT username, password, designid, fontid, tweet FROM twitteraccounts"; // Query it $result = mysqli_query($cxn, $query) or die(mysqli_error($cxn)); // Begin the loop that goes through each user while ($row = mysqli_fetch_array($result)) { // We have all the user's information $username = $row['username']; echo $username; echo "\n"; $password = $row['password']; $designid = $row['designid']; echo $designid; echo "\n"; $fontid = $row['fontid']; echo $fontid; echo "\n"; // Fetch their latest tweet include($_SERVER['DOCUMENT_ROOT'] . "/fetchtweetfromdb.php"); // Tweet has been fetched and written to database. Now fetch it again from the database $tweet = $row['tweet']; // This part of the script checks which designid the user is using, then calls the generator script appropriate for it if ($designid == 'design1') { include("design1.php"); } else { echo "Add more designs to the controller, regenerate.php, roughly line 42ish"; } } include($_SERVER['DOCUMENT_ROOT'] . ("/mysql/closeconnection.php")); echo "connection closed"; ?> if ($designid == 'design1') { include("design1.php"); The file 'design1.php' is called if the user chose Design 1. design1.php contains all the GD Library code to create their tweet with the formatting the user chose for Design 1. I'll post design1.php in a moment. Next file: fetchtweetfromdb.php <?php // fetchtweetfromdb.php // Fetch's the newest Tweet and stores it to database! include_once($_SERVER['DOCUMENT_ROOT'] . '/class.twitter.php'); $t = new Twitter; $t->username = $username; $t->password = $password; //$t->type = 'xml'; $data = $t->userTimeline(); $count = 0; $usertweet = "I'm using xxxx to display my latest Tweets in an image! AWESOME!"; //This is the default messsage, is replaced by user's actual tweet if exists foreach($data as $tweet) { // Cycle through the user's timeline... //echo $tweet->text; // Echoing tweets as text... $usertweet = $tweet->text; // Setting $usertweet variable to equal their tweet... //echo "<br/>"; // Adding a linebreak... $count = ($count + 1); // Increasing $count by one if (!$count = 0) { // If $count doesn't equal 0... break; // Exit the loop. } } // ET VOILA! We have the user's single newest tweet! // Now we have the variable $usertweet which contains the user's latest Tweet (if they don't have one a default message appears). // We will now store this to the database // Connect to database include("mysql/connecttodb.php"); mysqli_real_escape_string($cxn,$usertweet); $query = "UPDATE twitteraccounts SET tweet = \"$usertweet\" WHERE username = '$username' "; $result = mysqli_query($cxn,$query) or die (mysqli_error($cxn) . " - Couldn't execute MySQL query. Contact [email protected]"); if (!$result) { echo "MySQL query failed in file fetchtweet.php. Please contact webmaster@xxxxxx"; } // Close connection // include("mysql/closeconnection.php"); ?> Note that class.twitter.php is the PHP file that provides the Twitter API functions. Next file: design1.php <?php // Design 1 // We need to modify the user's tweet so that it will fit nicely into the image size //$ftweet = formatted Tweet $wraptweet = wordwrap($tweet, 62, "\n", true); $ftweet = "\"" . $wraptweet . "\""; //Now we'll create the actual image itself //Start by setting the widths and heights in constants define('SIG_WIDTH', 480); define('SIG_HEIGHT', 80); //Now create the image with the constant sizes $img = ImageCreateTrueColor(SIG_WIDTH,SIG_HEIGHT); // Creates the 400px x 50px image //Set a white background $bg_color = imagecolorallocate($img, 255, 255, 255); //White //Set a blue text colour $text_color = imagecolorallocate($img, 0, 128, 255); //Blue //Set a black text colour $black_color = imagecolorallocate($img, 0, 0, 0); //Black //Fill the background imagefilledrectangle($img, 0, 0, SIG_WIDTH, SIG_HEIGHT, $bg_color); //Write the user's tweet intro imagettftext($img, 8, 0, 5, 12, $black_color, "arial.ttf", $introtext); //Now write the user's Latest Tweet imagettftext($img, 12, 0, 20, 30, $text_color, "arial.ttf", $ftweet); //Now write the outro text imagettftext($img, 8, 0, 200, 70, $black_color, "arial.ttf", $outrotext); // Save the file ImageGif($img,$SaveFile); // Get the image out of memory ImageDestroy($img); ?> I'll assume that somehow this file is outputting all the gibberish to regenerate.php? I.E. this stuff: GIF87aàP„‚ü„ÂüD¢üÄâü$’ü¤Òüd²üäòüŠü”ÊüTªüÔêü4šü´Úütºüôúü†üŒÆüL¦üÌæü,–ü¬Öül¶üìöüŽüœÎü\®üÜîü<žü¼Þü|¾üüþü,àPþà'Ždižhª®lë¾p,Ïtmßx®ï|ïÿÀ pH,ȤrÉl:ŸÐ¨tJ Z¯Ø¬vËíz¿à°xL.›Ïè´zÍn»ßð¸|N¯Ûïø¼~Ïïûÿ€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£. ¦¦$«G®A«©¤´•«§%°E½?·§µÃ‘Æ'¿CÌ=ÉÆÄÒŽ¿°"Ù#" &¦ëáÚ§°©ïù"ÄÃààDµkä¦)t´MX:XЮ€"Pü@€6¶ÁƒÄÁþXÑÛ€C†0(Ѐ@¶4ÈPr¡ÏK 7<€PP„ "xÁ5Fh`À€8|ÀÅRØJS DdP@DÞ™-û³ ¤ ì{샤\5P®#;Œû eÀz«\uAÁn#C j £_>Ä]°A.= C[V<×®\§^Iz‚ÐÚIž ®gºÊ>ðÀšf²‚Ï}x[O >‹Ê«=¯×*P€@»ú¡ ,5šÐE‡†«€8½í²®´9dóŘ(€ð‡³ö ëôƒˆL4Ï€Ö×@ãÁ äT€W@À@` œsnЙ4@V׸߉xôÇ‘†(xÐcE”7’&"ÈÒ!iÄ™±†â‘8ƒä’L*Éä“': å”TViå•Xf©å–\véå—`†)æ˜d–iæ™h¦©æšl¶éæ›pÆ)çœtÖiçxæ©çž|öé矀*è „j衈&ªè¢Œ6êè£F*餔Vj饘fªé¦œvêé§ †*ꨤ–jꩨ¦ªêª¬¶êê«°Æ*무Öjë ¸æªë®¼z; This confuses me a little bit, as I don't actually see anywhere in the script where it 'echoes' etc. the contents of the GIF image. Could someone explain this to me? OK, finally, the database structure I'll show you: The second account below JackWebbHeller (my account) is a test account for my project, and just provides a second one to try out. Sorry for the messy scribbling but I've basically just blanked out anything that I wouldn't want just anyone to see. Since this forum's a public place, and I do trust you guys though. If there's anything more you need to know, please ask and I'll be happy to provide you with the info. Thanks so much! ~ Jack Quote Link to comment https://forums.phpfreaks.com/topic/173137-code-isnt-working-yet-is-practically-copied-out-of-a-php-book/#findComment-913585 Share on other sites More sharing options...
DavidAM Posted September 6, 2009 Share Posted September 6, 2009 OK, let's think about what we see here: We're using a loop to process all of the results from the query [quote]while ($row = mysqli_fetch_array($result)) // LINE 18[/quote] and the output you showed us (it would have helped if you showed all of it in the first post) JackWebbHeller design1 font1 GIF87a ...; Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\Tweetle\site\alpha\userimages\regenerate.php on line 18 connection closed It looks like the while loop runs once, so the error occurs on the second time through the loop. It would appear that your include include($_SERVER['DOCUMENT_ROOT'] . "/fetchtweetfromdb.php"); is changing one of the variables used in the conditional part of the loop, specifically $result. Quote Link to comment https://forums.phpfreaks.com/topic/173137-code-isnt-working-yet-is-practically-copied-out-of-a-php-book/#findComment-913586 Share on other sites More sharing options...
PFMaBiSmAd Posted September 6, 2009 Share Posted September 6, 2009 I'll second that, now that all has been reveled. Code you are including inside of the loop is reusing the $result variable. Files that you include should contain things like functions, classes, configuration information... that you then use in your main application code. They should not contain pieces of your main application code. Quote Link to comment https://forums.phpfreaks.com/topic/173137-code-isnt-working-yet-is-practically-copied-out-of-a-php-book/#findComment-913590 Share on other sites More sharing options...
jackwh Posted September 6, 2009 Author Share Posted September 6, 2009 I think I've fathomed it out, YAY! Maybe I'm being too hasty here BUT I really think I have. Thinking about it, I've realised what I've done wrong, and it was pretty goofy too. So basically I moved some files around a week or so ago to try and make the site's folder structure a bit more logical. The file, fetchtweetfromdb.php, was in the root folder. However I forgot to move that to the /userimages/ folder... Once I'd moved it's folder correctly about 10 minutes ago, I no longer got the MySQLI error, which is a good thing. however this whole gif-amawhatsit was still outputting, and, to make matters worse, the user image wasn't updating with my latest Test Tweets. The problem was that I stupidly defined the filename to save the user image in a file called generate.php, that runs the first time a user uses my service. Generate.php includes the file design1.php, which read the image file name from generate.php. When just plain calling design1.php, it didn't actually have ANY variables for the image's filename. So I think it just outputted some GIF87 gibberish. So to fix it I created a new file called design1regen.php that is now called by regenerate.php. Design1regen.php: <?php //FILENAME: design1regen.php //DESCRIPTION: Regenerates design1.php $FileName = "$username" . '.gif'; // Name of file (username.gif) $SaveFile = "$FileName"; // Path to save image to (/userimages/username.gif) // This is the intro text for the sig $introtext = "My latest tweet:"; // This is the outro text for the sig $outrotext = "Click here to follow me on Twitter: @" . $username; // We need to modify the user's tweet so that it will fit nicely into the image //$ftweet = formatted Tweet $wraptweet = wordwrap($tweet, 62, "\n", true); $ftweet = "\"" . $wraptweet . "\""; //Now we'll create the actual image itself //Start by setting the widths and heights in constants define('SIG_WIDTH', 480); define('SIG_HEIGHT', 80); //Now create the image with the constant sizes $img = ImageCreateTrueColor(SIG_WIDTH,SIG_HEIGHT); // Creates the 400px x 50px image //Set a white background $bg_color = imagecolorallocate($img, 255, 255, 255); //White //Set a blue text colour $text_color = imagecolorallocate($img, 0, 128, 255); //Blue //Set a black text colour $black_color = imagecolorallocate($img, 0, 0, 0); //Black //Fill the background imagefilledrectangle($img, 0, 0, SIG_WIDTH, SIG_HEIGHT, $bg_color); //Write the user's tweet intro imagettftext($img, 8, 0, 5, 12, $black_color, "arial.ttf", $introtext); //Now write the user's Latest Tweet imagettftext($img, 12, 0, 20, 30, $text_color, "arial.ttf", $ftweet); //Now write the outro text imagettftext($img, 8, 0, 200, 70, $black_color, "arial.ttf", $outrotext); // Save the file ImageGif($img,$SaveFile); // Get the image out of memory ImageDestroy($img); ?> Now, when this is called, no longer does the browser output GIF87 gibberish, it instead outputs the contents of the database loop quite neatly, like so: JackWebbHeller design1 font1 XXXXXX design1 font1 connection closed And then goes on to update each image, one at a time, with the user's latest Tweet. WHOOP! Thank you guys for your help. I'll leave this topic as Unsolved until I've tested thoroughly. Once I've made sure it's completely fixed, I'll mark as solved. Thank you loads! Quote Link to comment https://forums.phpfreaks.com/topic/173137-code-isnt-working-yet-is-practically-copied-out-of-a-php-book/#findComment-913594 Share on other sites More sharing options...
jackwh Posted September 6, 2009 Author Share Posted September 6, 2009 Files that you include should contain things like functions, classes, configuration information... that you then use in your main application code. They should not contain pieces of your main application code. The reason I've organised as I did was simply because I knew my application code would have to use pieces of the main application code at least twice (generate first time/update every 15 mins), if that makes any sense! Although thank you for the pointer, I will bare this in mind for the next project I work on (I'll try and keep my working copy of my project intact for now, lol ) Quote Link to comment https://forums.phpfreaks.com/topic/173137-code-isnt-working-yet-is-practically-copied-out-of-a-php-book/#findComment-913596 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.