Converting Text to Image in PHP formatted by alignment
Posted by Bagesh Singh on April 7th, 2010
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?
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
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. |
007 |
* @compiled Subesh Pokhrel from PHP.net and PHPclasses.org |
010 |
define("ALIGN_LEFT", "left"); |
011 |
define("ALIGN_CENTER", "center"); |
012 |
define("ALIGN_RIGHT", "right"); |
021 |
* Function for create image from text with selected font. |
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. |
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)){ |
036 |
$this->im = @imagecreate($W, $H) |
037 |
or die("Cannot Initialize new GD image stream"); |
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. |
042 |
$this->imagettftextbox($this->im, $fsize,0, $X,$Y, $text_color, $font, $text,800); |
046 |
* This function works to set alignment in image and write image. |
048 |
public function imagettftextbox(&$image, $size, $angle, $left, $top, $color, $font, $text, $max_width) |
050 |
$text_lines = explode("\n", $text); // Supports manual line breaks! |
053 |
$line_widths = array(); |
055 |
$largest_line_height = 0; |
057 |
foreach($text_lines as $block) |
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 |
067 |
for($i = 0; $i < count($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]; |
074 |
if($line_height > $largest_line_height) $largest_line_height = $line_height; |
076 |
if($line_width > $max_width && !$first_word) |
078 |
$lines[] = $current_line; |
080 |
$line_widths[] = $last_width ? $last_width : $line_width; |
082 |
/*if($i == count($words)) |
087 |
$current_line = $item; |
091 |
$current_line .= ($first_word ? '' : ' ') . $item; |
094 |
if($i == count($words) - 1) |
096 |
$lines[] = $current_line; |
098 |
$line_widths[] = $line_width; |
101 |
$last_width = $line_width; |
108 |
$current_line = $item; |
113 |
foreach($lines as $line) |
115 |
if($align == ALIGN_CENTER) |
117 |
$left_offset = ($max_width - $line_widths[$i]) / 2; |
119 |
elseif($align == ALIGN_RIGHT) |
121 |
$left_offset = ($max_width - $line_widths[$i]); |
123 |
imagettftext($image, $size, $angle, $left + $left_offset, $top + $largest_line_height + ($largest_line_height * $i), $color, $font, $line); |
127 |
return $largest_line_height * count($lines); |
133 |
* Function to show text as Png image. |
136 |
public function showAsPng(){ |
138 |
header("Content-type: image/png"); |
139 |
return imagepng($this->im); |
Save this file as TextToImage.class.php.
And then use this code to call the classes instance.
02 |
/** Downloaded from PHP classes |
03 |
* Please note that KORONG.TTF font file should be present to run the code. |
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"); |
Cheers!
Both comments and pings are currently closed.