Jump to content

text to jpg?


python72

Recommended Posts

I would like to turn text into jpg image. Basically I would like to grab text data from my data base and encapsulate it into jpg image. I am assuming I will have to take background and place my text on top but have no clue where to start. Could you point me to some info or give me some ideas how to do it and which functions to use? Any help will be greatly appreciated.

Link to comment
Share on other sites

Here is a starting point...

(example in action http://nstoia.com/sat/textonimage/aindex.php)

<?php
/* Text to be placed as a paragraph */
$text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer non nunc lectus. Curabitur hendrerit bibendum enim dignissim tempus. Suspendisse non ipsum auctor metus consectetur eleifend. Fusce cursus ullamcorper sem nec ultricies. Aliquam erat volutpat. Vivamus massa justo, pharetra et sodales quis, rhoncus in ligula. Integer dolor velit, ultrices in iaculis nec, viverra ut nunc.';
$num_c = strlen($text);
$text = $text . " - " . $num_c;

/*  Break it up into pieces */
$lines = explode('|', wordwrap($text, 40, '|'));

/* use white template for new image */
$im = imagecreatefromjpeg("template2.jpg");

/*  Create some colors */
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 220, 220, 220);
$black = imagecolorallocate($im, 0, 0, 0);

/* Replace path by your own font path */
$font = 'mono_cursive.ttf';

/* Basic font settings */
$font_size = 18;

/* Starting Y position */
$y = 20;

/* X position */
$x = 20;

/* shadow offset */
$z = 2;

/* Loop through the lines and place them on the image */
foreach ($lines as $line)
{
    imagettftext($im, $font_size, 0, $x+$z, $y+$z, $grey, $font, $line); /* this is making the drop shadow */
    imagettftext($im, $font_size, 0, $x, $y, $black, $font, $line);

    /* Increment Y so the next line is below the previous line */
    $y += 30;
}


/* Using imagepng() results in clearer text compared with imagejpeg() */
$save = "test4.png";
imagepng($im, $save);

$image = imagecreatefrompng($save);
$save_jpg = "test4.jpg";

imagejpeg($image, $save_jpg, 100);
imagedestroy($image);
unlink($save);



?>
<img src="<?PHP echo $save_jpg; ?>" alt="">
<?PHP
imagedestroy($im);
?>

Link to comment
Share on other sites

Below is the code for the PNG...

 


<?php
header("Content-type: image/png"); // CONTENT TYPE OF OUTPUT FILE
$string = 'phpchamps'; // STRING TO BE PRINTED ON THE IMAGE
$im     = imagecreatefrompng("button1.png"); // BACKGROUND IMAGE ON WHICH TEXT TO BE PRINTEND
$orange = imagecolorallocate($im, 0, 0, 60); // COLOR OF THE TEXT
$px     = (imagesx($im) - 9 * strlen($string)) / 2; // MARGIN TO BE SET
imagestring($im, 9, $px, 29, $string, $orange);
imagepng($im);
imagedestroy($im);
?>

Link to comment
Share on other sites

  • 4 weeks later...

Just got back to it, thanks everyone for your responses and all the valuable information.

Just couple more questions related to the examples provided.

Looking at the code for both jpg and png the image (background) is fixed. Is there a way to adjust the size of background based on the size of text to be saved? Would be nice if both height and width could be adjusted based on number or lines/number of characters per line. I can obviously count that before placing the text on the image.

How can the file be renamed and/or saved in different directory? What I think these examples do is basically place text on image and save it under the original name. If I can change the name and/or directory I can use one canvas to produce few different messages which in the end can be displayed as images.

Thanks a lot for your time.

Link to comment
Share on other sites

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.