ciumpalac Posted March 24, 2008 Share Posted March 24, 2008 Hello guys i'm new here. I'm trying to divide a text from a form into more lines not just one and then print it to an image. With some help from google i made a php script that limits the text to 15 characters but now i need to figure out how to create a new line after 15 and continue with the text until the next 15 characters and so on. This is my little script: <?php header("Content-Type: image/jpeg"); $im = ImageCreateFromJpeg("hello.jpg"); $color = ImageColorAllocate($im, 0, 0, 0); $x = 400; $y = 400; $font_size = 30; $font = 'arial.ttf'; $long = "Perfect, now i need to figure out how to divide lines"; $short = limit_text( $long, 15); $angle = 0; Imagettftext($im, $font_size, $angle, $x, $y, $color, $font, $short); Imagejpeg($im, '', 100); ImageDestroy($im); function limit_text( $text, $limit ) { if( strlen($text)>$limit ) { $text = substr( $text,0,$limit ); $text = substr( $text,0,-(strlen(strrchr($text,' '))) ); } return $text; } ?> Sorry for my bad english hope you understand my problem. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/97585-how-to-divide-text-lines/ Share on other sites More sharing options...
ansarka Posted March 24, 2008 Share Posted March 24, 2008 try this $newtext = wordwrap($text, 15, "\n", true); Quote Link to comment https://forums.phpfreaks.com/topic/97585-how-to-divide-text-lines/#findComment-499295 Share on other sites More sharing options...
ciumpalac Posted March 24, 2008 Author Share Posted March 24, 2008 That worked perfectly thank you so much. Another question: the padding between the lines is a little to much how can i fix that? Quote Link to comment https://forums.phpfreaks.com/topic/97585-how-to-divide-text-lines/#findComment-499331 Share on other sites More sharing options...
MadTechie Posted March 24, 2008 Share Posted March 24, 2008 the kerning isn't very good with imagettftext, your need to write them line by line adjusting the Y coordinate, Quick example (untested) <?php $text = wordwrap($text, 15, "\n", true); $textArray = explode("\n",$text); $gap = 0; foreach($textArray as $theText) { $gap = $gap + 30; //whaever! Imagettftext($im, $font_size, $angle, $x, $y+$gap, $color, $font, $theText); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/97585-how-to-divide-text-lines/#findComment-499340 Share on other sites More sharing options...
ciumpalac Posted March 25, 2008 Author Share Posted March 25, 2008 Thank you MadTechie works just fine. I created a function that saves the image with the text from the form with a random number name ex: 936982.jpg in a folder named /img. My question is: Is there a quick way to show the last 3, for example, images that were created? and also the link to the very last one in an external file.html? without creating an entire script? Thank you again for your help Quote Link to comment https://forums.phpfreaks.com/topic/97585-how-to-divide-text-lines/#findComment-500176 Share on other sites More sharing options...
MadTechie Posted March 25, 2008 Share Posted March 25, 2008 if your creating a log in mysql you could just add a timestamp, if your just creating files then your need to get the file from the folder and sort by creation date, then use the last 3, or create a textfile with the 3 filenames, adding the new one and removing the last one as for the last part of you question also the link to the very last one in an external file.html? without creating an entire script? i am not sure what your asking.. try this example - untested (may work!) <?php $dir = "/uploaded/images/"; $theFile = array(); if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { $theFile[filectime($file)] = $file; } closedir($dh); } } ksort($theFile); //or krsort($theFile); echo $theFile[0]."<br>"; echo $theFile[1]."<br>"; echo $theFile[2]."<br>"; echo "<br><br>All files<pre>"; print_r($theFile); ?> Quote Link to comment https://forums.phpfreaks.com/topic/97585-how-to-divide-text-lines/#findComment-500557 Share on other sites More sharing options...
ciumpalac Posted March 25, 2008 Author Share Posted March 25, 2008 Doesn't seems to work, maybe because i'm trying the script on windows? Anyway i managed to do it with a free image gallery script so i dont need it anymore. As for my other question, and the last one I promise Atm my script works like this: And i have this files: index.php - that includes the form and the head.jpg send.php - that includes the script that creates the image. images/ - folder that keeps the created images with random names. When I press Submit I want to have something like this: I don't want to go to /send.php when i press Submit but insted refresh the page, change the head.jpg with generatedName.jpg and show the link to the generatedName.jpg. I can't put or better, i don't know how to include send.php in index.php because of header("Content-Type: image/jpeg"); i think. I went to php.net and of course google.com and tried some functions but no luck. Thank you for your time i really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/97585-how-to-divide-text-lines/#findComment-500691 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.