Jump to content

Archived

This topic is now archived and is closed to further replies.

_SAi_

help please with creating image from ttf

Recommended Posts

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:
[code]
<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>
[/code]

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

[code]
<?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);

?>
[/code]

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!

Share this post


Link to post
Share on other sites
What exactly isn't working.  The code looks good to me.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).
[code]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\" />";[/code]

...
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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.