Jump to content


Photo

help please with creating image from ttf


  • Please log in to reply
4 replies to this topic

#1 _SAi_

_SAi_
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 18 September 2006 - 03:30 AM

Hello,

I could use some help to get something working.  I am trying to create an image using truetype fonts given a user's input for a phrase and a font selection.

Here is what I have so far for the input form:
<html>
<head>
<title>FONT PREVIEW</title>

<style type="text/css">
p {font-family:"Century Gothic", "Sans Serif"; font-size:12pt; color:black;}
</style>
</head>

<body>
<br>
<br>
<br>
<p>Enter some text and Select a Font then press PREVIEW to see it:</p>
<form method="get" action="index.php">


<input type="text" name="text_input">

Font:
<select name="font_input">
<option value="ttf/arial.ttf">Arial</option>
<option value="ttf/chick.ttf">Chick</option>
</select>

Preview Size:
<select name="size_input">
<option value = "small">Small</option>
<option value = "medium">Medium</option>
<option value = "large">Large</option>
</select>


<input type="submit" value="PREVIEW">
</form>

<?php

if(!empty($_GET['font_input'])) {
    // make sure to validate the GET variable
    // before calling it into your script
    $img = 'section_head2.php?font_input='.$_GET['font_input'];
} else {
    $img = 'section_head2.php';
}
echo "<img src=\"$img\" />";

?>


</body>
</html>

And here is the code I have to create it as of now:

<?php
 
// if no text is provided, we will use some default text
 if (!isset($_GET["text_input"]))
	$text = "Please Enter Some Text!!";
 else
	$text = $_GET["text_input"];
 
// set this to the location of the TrueType font file that you want to use
 if (!isset($_GET["font_input"]))
	$font = "ttf/arial.ttf";
 else
	$font = $_GET["font_input"];

// angle of the font in degrees
$font_angle = 0;

// the weight of the font stroke
$stroke = 0;

// the size of the font
$get_font_size = $_GET["size_input"];

if ($get_font_size == "large")
{
$font_size = 100;
$startx = 10;
$starty = 110;
$im = imagecreate(750,125);
}

if ($get_font_size == "medium")
{
$font_size = 70;
$startx = 10;
$starty = 80;
$im = imagecreate(750,95);
}

if ($get_font_size == "small")
{
$font_size = 40;
$startx = 10;
$starty = 45;
$im = imagecreate(750,50);
}


$TextColour = imagecolorallocate($im, 150, 0, 0);
$BackgroundColour = imagecolorallocate($im, 255, 255, 255);
$OutlineColour = imagecolorallocate($im, 255, 255, 255);
 
imagefilltoborder($im, 0, 0, $BackgroundColour, $BackgroundColour);
 
// now draw out the outline (stroke) on the text
for ($ox = -$stroke; $ox <= $stroke; $ox++) {
	for ($oy = -$stroke; $oy <= $stroke; $oy++) {
		imagettftext($im, $font_size, $font_angle, $startx+$ox, $starty+$oy, 
				$OutlineColour, $font, $text);
	}
}
 
imagettftext($im, $font_size, $font_angle, $startx, $starty, $TextColour, $font, $text);
 
// set the correct HTTP header for a PNG image
header("Content-type: image/png");
 
imagepng($im);
 
// remember to free up the memory used on the server to create the image!
imagedestroy($im);
 
?>

I was able to get this working (with help) with just an input of the phrase, but now that I am trying to have a font pulldown too I cannot for the life of me get it to work.

And help will be greatly appreciated!!

Thanks!

#2 _SAi_

_SAi_
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 20 September 2006 - 10:08 PM

Wow! I've stumped em!!

Is there any other info I can add to make this solveable?

#3 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 21 September 2006 - 01:24 AM

What exactly isn't working.  The code looks good to me.

Info: PHP Manual


#4 _SAi_

_SAi_
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 21 September 2006 - 03:21 AM

Well, the image doesn't appear.  And I'm not sure how to make a default image if not text or font is selected.  If you notice in the form code there is just a default font right now at the bottom.  And that doesn't even work.

#5 _SAi_

_SAi_
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 25 September 2006 - 03:39 AM

Ok,

Let's start with this...

What is the proper syntax for my form's image if I have a user input a font AND a phrase?

I currently have this... but I don't know how to add in for the font (only the phrase - if that's even correct).
if(!empty($_GET['text_input']) && !empty($_GET['font_input'])) {
    // make sure to validate the GET variable
    // before calling it into your script
    $img = 'section_head2.php?text_input='.$_GET['text_input']'&font_input='.$_GET['font_input']'';
} else {
    $img = 'section_head2.php';
}
echo "<img src=\"$img\" />";

...
I get an error on the &font= portion of the $img declaration:

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /var/www/html/FontPreviews/001b/index.php on line 41




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users