//Save this as class.WriteToImage.php
<?php
class WriteToImage {
private $image;
public function __construct($image_file){
if (! function_exists ( 'ImageCreate' ))
$this->fatal_error ( 'Error: Server does not support PHP image generation' );
$this->image = imagecreatefrompng($image_file);
}
public function putTextOnImage($font_file, $font_size, $font_color, $x_pos, $y_pos, $text){
if (! is_readable ( $font_file )) {
$this->fatal_error ( 'Error: The server is missing the specified font: '.$font_file );
}
// create and measure the text
$font_rgb = $this->hex_to_rgb($font_color);
$box = ImageTTFBBox ( $font_size, 0, $font_file, $text );
$text_width = abs ( $box [2] - $box [0] );
$text_height = abs ( $box [5] - $box [3] );
if (! $this->image || ! $box)
{
fatal_error ( 'Error: The server could not create this image.' );
}
// allocate colors and measure final text position
$font_color = ImageColorAllocate ( $this->image, $font_rgb['red'], $font_rgb['green'], $font_rgb['blue'] );
$image_width = imagesx ( $this->image );
$put_text_x = $image_width - $text_width - ($image_width - $x_pos);
$put_text_y = $y_pos;
// Write the text
imagettftext ( $this->image, $font_size, 0, $put_text_x, $put_text_y, $font_color, $font_file, $text );
}
private function hex_to_rgb($hex) {
// remove '#'
if (substr ( $hex, 0, 1 ) == '#')
$hex = substr ( $hex, 1 );
// expand short form ('fff') color to long form ('ffffff')
if (strlen ( $hex ) == 3) {
$hex = substr ( $hex, 0, 1 ) . substr ( $hex, 0, 1 ) .
substr ( $hex, 1, 1 ) . substr ( $hex, 1, 1 ) .
substr ( $hex, 2, 1 ) . substr ( $hex, 2, 1 );
}
if (strlen ( $hex ) != 6)
fatal_error ( 'Error: Invalid color "' . $hex . '"' );
// convert from hexidecimal number systems
$rgb ['red'] = hexdec ( substr ( $hex, 0, 2 ) );
$rgb ['green'] = hexdec ( substr ( $hex, 2, 2 ) );
$rgb ['blue'] = hexdec ( substr ( $hex, 4, 2 ) );
return $rgb;
}
public function displayImage(){
header ( 'Content-type: image/png');
ImagePNG ( $this->image );
ImageDestroy ( $this->image );
}
private function fatal_error($message)
{
// send an image
if (function_exists ( 'ImageCreate' ))
{
$width = ImageFontWidth ( 5 ) * strlen ( $message ) + 10;
$height = ImageFontHeight ( 5 ) + 10;
if ($this->image = ImageCreate ( $width, $height ))
{
$background = ImageColorAllocate ( $this->image, 255, 255, 255 );
$text_color = ImageColorAllocate ( $this->image, 0, 0, 0 );
ImageString ( $this->image, 5, 5, 5, $message, $text_color );
header ( 'Content-type: image/png' );
ImagePNG ( $this->image );
ImageDestroy ( $this->image );
exit ();
}
}
// send 500 code
header ( "HTTP/1.0 500 Internal Server Error" );
print ( $message );
exit ();
}
}
?>
//save this as write-to-image.php
<?php
include('class.WriteToImage.php');
// you can edit any of these
$text1 = $_GET['text1'];
$text2 = $_GET['text2'];
$text3 = $_GET['text3'];
$text4 = $_GET['text4'];
$text5 = $_GET['text5'];
$text6 = $_GET['text6'];
$text7 = $_GET['text7'];
$text8 = $_GET['text8'];
// tell the file name of the image to work on
$display_image = new WriteToImage('jump_empty.png');
// make as much of these as you want. Make sure you include font file name.
// FontFileName, FontSize, FontColor, X Position, Y Position, Text from the form or your own text in quotes.
$display_image->putTextOnImage('ambient.ttf',23,'#ffffff',127,103, $text1);
$display_image->putTextOnImage('ambient.ttf',23,'#ffffff',227,107, $text2);
$display_image->putTextOnImage('ambient.ttf',23,'#ffffff',227,110, $text3);
$display_image->putTextOnImage('ambient.ttf',23,'#ffffff',227,115, $text4);
$display_image->putTextOnImage('ambient.ttf',23,'#ffffff',227,120, $text5);
$display_image->putTextOnImage('ambient.ttf',23,'#ffffff',227,125, $text6);
$display_image->putTextOnImage('ambient.ttf',23,'#ffffff',227,130, $text7);
$display_image->putTextOnImage('ambient.ttf',23,'#ffffff',427,157, $text8);
// give final image to browser
$display_image->displayImage();
// and this is your form
<style>
body {margin:0; font-family:garamond; background:#FFFFFF}
table {background:white; font-size:14px}
form {display:inline}
</style>
<br>
<br>
<br>
<br>
<br>
<br>
<script>
function CreateSign() {
var text1 = document.textform.imtext1.value;
var text2 = document.textform.imtext2.value;
var text3 = document.textform.imtext3.value;
var text4 = document.textform.imtext4.value;
var text5 = document.textform.imtext5.value;
var text6 = document.textform.imtext6.value;
var text7 = document.textform.imtext7.value;
var text8 = document.textform.imtext8.value;
document['simage'].src='write-to-image.php?text1='+text1+'&text2='+text2+'&text3='+text3+'&text4='+text4+'&text5='+text5+'&text6='+text6+'&text7='+text7+'&text8='+text8
}
</script>
<center>
<form name=textform>
<input type=text name=imtext1>
<input type=text name=imtext2>
<input type=text name=imtext3>
<input type=text name=imtext4>
<input type=text name=imtext5>
<input type=text name=imtext6>
<input type=text name=imtext7>
<input type=text name=imtext8>
<input type=button value='Show Image' onClick="CreateSign();" \>
</form><br /><br>
<br>
<img src="write-to-image.php?text=0.0" name=simage>
<br>
<br>
</center>
<title>Sign Builder</title>
public function putTextOnImage($font_file, $font_size, $font_color, $x_pos, $y_pos, $text){
if (! is_readable ( $font_file )) {
$this->fatal_error ( 'Error: The server is missing the specified font: '.$font_file );
}
// create and measure the text
$font_rgb = $this->hex_to_rgb($font_color);
$box = ImageTTFBBox ( $font_size, 0, $font_file, $text );
$text_width = abs ( $box [2] - $box [0] );
$text_height = abs ( $box [5] - $box [3] );
if (! $this->image || ! $box)
{
fatal_error ( 'Error: The server could not create this image.' );
}
// allocate colors and measure final text position
$font_color = ImageColorAllocate ( $this->image, $font_rgb['red'], $font_rgb['green'], $font_rgb['blue'] );
$image_width = imagesx ( $this->image );
$text_width = $text_width / 2;
$put_text_x = $image_width - $text_width - ($image_width - $x_pos);
$put_text_y = $y_pos;
// Write the text
imagettftext ( $this->image, $font_size, 0, $put_text_x, $put_text_y, $font_color, $font_file, $text );
}
Open in new window