python72 Posted January 31, 2011 Share Posted January 31, 2011 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. Quote Link to comment Share on other sites More sharing options...
Anthony` Posted January 31, 2011 Share Posted January 31, 2011 Can you elaborate more on this text data from your database? Are you storing image names or paths and such? Quote Link to comment Share on other sites More sharing options...
litebearer Posted January 31, 2011 Share Posted January 31, 2011 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); ?> Quote Link to comment Share on other sites More sharing options...
Anthony` Posted January 31, 2011 Share Posted January 31, 2011 Ah, right... I thought he was storing image names into a database and wanting them to be outputted on a page using HTML img tags for some reason... Quote Link to comment Share on other sites More sharing options...
phpchamps Posted January 31, 2011 Share Posted January 31, 2011 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); ?> Quote Link to comment Share on other sites More sharing options...
python72 Posted February 25, 2011 Author Share Posted February 25, 2011 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. Quote Link to comment 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.