Codice:
<?php
/**
* Class for converting Text to Image.
* Font type can be specified
* The alignment where the text will echo can also be set.
*
* @compiled Subesh Pokhrel from PHP.net and PHPclasses.org
*
*/
define("ALIGN_LEFT", "left");
define("ALIGN_CENTER", "center");
define("ALIGN_RIGHT", "right");
class TextToImage {
private $im;
/**
* @name : makeImageF
*
* Function for create image from text with selected font.
*
* @param String $text : String to convert into the Image.
* @param String $font : Font name of the text.
* @param int $W : Width of the Image.
* @param int $H : Hight of the Image.
* @param int $X : x-coordinate of the text into the image.
* @param int $Y : y-coordinate of the text into the image.
* @param int $fsize : Font size of text.
* @param array $color : RGB color array for text color.
* @param array $bgcolor : RGB color array for background.
*
*/
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)){
$this->im = @imagecreate($W, $H)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($this->im, $bgcolor[0], $bgcolor[1], $bgcolor[2]); //RGB color background.
$text_color = imagecolorallocate($this->im, $color[0], $color[1], $color[2]); //RGB color text.
$this->imagettftextbox($this->im, $fsize,0, $X,$Y, $text_color, $font, $text,800);
}
/**
* This function works to set alignment in image and write image.
*/
public function imagettftextbox(&$image, $size, $angle, $left, $top, $color, $font, $text, $max_width)
{
$text_lines = explode("\n", $text); // Supports manual line breaks!
$lines = array();
$line_widths = array();
$largest_line_height = 0;
foreach($text_lines as $block)
{
$current_line = ''; // Reset current line
$align=ALIGN_CENTER; // Setting Alignment
$words = explode(' ', $block); // Split the text into an array of single words
$first_word = TRUE;
$last_width = 0;
for($i = 0; $i < count($words); $i++)
{
$item = $words[$i];
$dimensions = imagettfbbox($size, $angle, $font, $current_line . ($first_word ? '' : ' ') . $item);
$line_width = $dimensions[2] - $dimensions[0];
$line_height = $dimensions[1] - $dimensions[7];
if($line_height > $largest_line_height) $largest_line_height = $line_height;
if($line_width > $max_width && !$first_word)
{
$lines[] = $current_line;
$line_widths[] = $last_width ? $last_width : $line_width;
/*if($i == count($words))
{
continue;
}*/
$current_line = $item;
}
else
{
$current_line .= ($first_word ? '' : ' ') . $item;
}
if($i == count($words) - 1)
{
$lines[] = $current_line;
$line_widths[] = $line_width;
}
$last_width = $line_width;
$first_word = FALSE;
}
if($current_line)
{
$current_line = $item;
}
}
$i = 0;
foreach($lines as $line)
{
if($align == ALIGN_CENTER)
{
$left_offset = ($max_width - $line_widths[$i]) / 2;
}
elseif($align == ALIGN_RIGHT)
{
$left_offset = ($max_width - $line_widths[$i]);
}
imagettftext($image, $size, $angle, $left + $left_offset, $top + $largest_line_height + ($largest_line_height * $i), $color, $font, $line);
$i++;
}
return $largest_line_height * count($lines);
}
/**
* @name showAsPng
*
* Function to show text as Png image.
*
*/
public function showAsPng(){
header("Content-type: image/png");
return imagepng($this->im);
}
}
?>
Codice:
<?php
/** Downloaded from PHP classes
* Please note that KORONG.TTF font file should be present to run the code.
*/
ini_set("display_errors",1);
require_once('TextToImage.class.php');
$_im = new TextToImage();
$_im->makeImageF("Thank you ! Subesh Pokhrel \n subesh.com.np","KORONG.TTF");
$_im->showAsPng();
?>
Si può fare?