Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/97585-how-to-divide-text-lines/
Share on other sites

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);
}

?>

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

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);

?>

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  ;D

 

Atm my script works like this:

69501565pl7.jpg

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:

84656939ck4.jpg

 

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.

 

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.