Converting Text to Image in PHP formatted by alignment

During one of my project works I came across a situation like changing the input of TINYMCE editor to image. Can you believe what can be the user’s requirement? :P Basically what the client needed was to change the text to image and text should be in American Typewriter Font with the option of showing text as image formatted by alignment. i.e Left Alignment, Center Alignment or Right Alignment in a white background image. Then I did some research, this is another way of saying I Googled a lot! lol. I came across two scripts, one which converted text to image and another a function to set alignment. So I thought that why not combine both the codes and publish for others, who may need it as well.

The compiled code

001 <?php
002 /**
003 * Class for converting Text to Image.
004 * Font type can be specified
005 * The alignment where the text will echo can also be set.
006 *
007 * @compiled Subesh Pokhrel from PHP.net and PHPclasses.org
008 *
009 */
010 define("ALIGN_LEFT", "left");
011 define("ALIGN_CENTER", "center");
012 define("ALIGN_RIGHT", "right");
013
014 class TextToImage {
015
016 private $im;
017
018 /**
019 * @name                   : makeImageF
020 *
021 * Function for create image from text with selected font.
022 *
023 * @param String $text     : String to convert into the Image.
024 * @param String $font     : Font name of the text.
025 * @param int    $W        : Width of the Image.
026 * @param int    $H        : Hight of the Image.
027 * @param int    $X        : x-coordinate of the text into the image.
028 * @param int    $Y        : y-coordinate of the text into the image.
029 * @param int    $fsize    : Font size of text.
030 * @param array  $color    : RGB color array for text color.
031 * @param array  $bgcolor  : RGB color array for background.
032 *
033 */
034 public function makeImageF($text, $font="CENTURY.TTF", $W=800, $H=200, $X=0, $Y=0, $fsize=18, $color=array(0x0,0x0,0x0), $bgcolor=array(0xFF,0xFF,0xFF)){
035
036 $this->im = @imagecreate($W, $H)
037 or die("Cannot Initialize new GD image stream");
038
039 $background_color = imagecolorallocate($this->im, $bgcolor[0], $bgcolor[1], $bgcolor[2]); //RGB color background.
040 $text_color = imagecolorallocate($this->im, $color[0], $color[1], $color[2]); //RGB color text.
041
042 $this->imagettftextbox($this->im, $fsize,0, $X,$Y, $text_color, $font, $text,800);
043 }
044
045 /**
046 * This function works to set alignment in image and write image.
047 */
048 public function imagettftextbox(&$image, $size, $angle, $left, $top, $color, $font, $text, $max_width)
049 {
050 $text_lines = explode("\n", $text); // Supports manual line breaks!
051
052 $lines = array();
053 $line_widths = array();
054
055 $largest_line_height = 0;
056
057 foreach($text_lines as $block)
058 {
059 $current_line = ''; // Reset current line
060 $align=ALIGN_CENTER; // Setting Alignment
061 $words = explode(' ', $block); // Split the text into an array of single words
062
063 $first_word = TRUE;
064
065 $last_width = 0;
066
067 for($i = 0; $i < count($words); $i++)
068 {
069 $item = $words[$i];
070 $dimensions = imagettfbbox($size, $angle, $font, $current_line . ($first_word ? '' : ' ') . $item);
071 $line_width = $dimensions[2] - $dimensions[0];
072 $line_height = $dimensions[1] - $dimensions[7];
073
074 if($line_height > $largest_line_height) $largest_line_height = $line_height;
075
076 if($line_width > $max_width && !$first_word)
077 {
078 $lines[] = $current_line;
079
080 $line_widths[] = $last_width ? $last_width : $line_width;
081
082 /*if($i == count($words))
083 {
084 continue;
085 }*/
086
087 $current_line = $item;
088 }
089 else
090 {
091 $current_line .= ($first_word ? '' : ' ') . $item;
092 }
093
094 if($i == count($words) - 1)
095 {
096 $lines[] = $current_line;
097
098 $line_widths[] = $line_width;
099 }
100
101 $last_width = $line_width;
102
103 $first_word = FALSE;
104 }
105
106 if($current_line)
107 {
108 $current_line = $item;
109 }
110 }
111
112 $i = 0;
113 foreach($lines as $line)
114 {
115 if($align == ALIGN_CENTER)
116 {
117 $left_offset = ($max_width - $line_widths[$i]) / 2;
118 }
119 elseif($align == ALIGN_RIGHT)
120 {
121 $left_offset = ($max_width - $line_widths[$i]);
122 }
123 imagettftext($image, $size, $angle, $left + $left_offset, $top + $largest_line_height + ($largest_line_height * $i), $color, $font, $line);
124 $i++;
125 }
126
127 return $largest_line_height * count($lines);
128 }
129
130 /**
131 * @name showAsPng
132 *
133 * Function to show text as Png image.
134 *
135 */
136 public function showAsPng(){
137
138 header("Content-type: image/png");
139 return imagepng($this->im);
140 }
141
142 }
143 ?>

Save this file as TextToImage.class.php.

And then use this code to call the classes instance.

01 <?php
02 /** Downloaded from PHP classes
03 * Please note that KORONG.TTF font file should be present to run the code.
04 */
05
06 ini_set("display_errors",1);
07 require_once('TextToImage.class.php');
08 $_im = new TextToImage();
09 $_im->makeImageF("Thank you ! Subesh Pokhrel \n subesh.com.np","KORONG.TTF");
10 $_im->showAsPng();
11 ?>

Cheers!

Both comments and pings are currently closed.

Comments are closed.

Powered by WordPress | iCellPhoneDeals.com Offers Free Wireless Deals. | Thanks to Bestincellphones.com Verizon Cell Phones, Best CD Rates Online and Fat Burning Furnace Review
Php Programmer Bagesh Singh