PDA

View Full Version : [PHP] How do i... ?



Recursion
30-03-2007, 07:57 PM
Hello

How would I make a script where you would input some text, then that would make images from a font file ??

Thanks
Tom

Invent
30-03-2007, 07:59 PM
PHP GD :)

Couldn't be bothered to write my own code, so yeah here is the PHP.net tutorial with a change to make it save the image.

Make a file called text.php and inside it put:



<?php
// Set the content-type
header("Content-type: image/gif");

// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The text to draw
$text = $_POST['text'];
// Replace path by your own font path
$font = 'font.ttf';

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

$rand = rand(0, 60000);

// Using imagepng() results in clearer text compared with imagejpeg()
imagegif($im,'$rand.gif');
imagedestroy($im);
?>Make a file called index.php and in it have a form send the data to text.php and name the form which you type the text into called "text".

Enjoy.

Recursion
30-03-2007, 08:00 PM
I know but i dunno GD xD

Invent
30-03-2007, 08:02 PM
Look at my post, I editied it ;]

Recursion
30-03-2007, 08:09 PM
That wouldn't let me use a say... .ttf font file?

Invent
30-03-2007, 08:10 PM
Yes it would.

Just upload the font to your site

And then just change: $font = 'font.ttf';

Recursion
30-03-2007, 08:13 PM
ahh yes didn't see that line ;)

Thanks :) Ill see if i can get something going xD

Recursion
30-03-2007, 08:14 PM
Now... How would I make it so that they could choose a fill and line colour like in Word for this ???

Tomm
30-03-2007, 08:16 PM
Now... How would I make it so that they could choose a fill and line colour like in Word for this ???

Fill and line are the same thing :S

To create new colours:

imagecolorallocate($im, red, green, blue);

Change the values of red green blue.

Recursion
30-03-2007, 08:18 PM
Fill and line are the same thing :S

No say in like Fireworks, you have line and fill colours for text...

Blob
30-03-2007, 08:22 PM
Text colour and stroke you mean.

Tomm
30-03-2007, 08:25 PM
Ahhh, if you do mean stroke then I am not sure how to do it or even if it is posible.


Text colour and stroke you mean.

Recursion
30-03-2007, 08:29 PM
I get:

<br />
<b>Fatal error</b>: Call to undefined function: imagettftext() in <b>/home/revision/public_html/generator/gen.php</b> on line <b>20</b><br />

www.revisionarea.info/generator (http://www.revisionarea.info/generator) is the link...

And ahh ok NVM about the stroke :) Ill try the colour bits when I get this working...

Index.php:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.style1 {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
font-weight: bold;
}
.style2 {font-size: 10px; font-family: Verdana, Arial, Helvetica, sans-serif;}
input { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; }
select { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; }
-->
</style>
</head>
<body>
<p><span class="style1">Text Generator using fonts!
<br>
</span><span class="style2">Coded by <strong>***********.co.uk</strong></span><span class="style2"><br>
<br>
</span></p>
<form method="post" action="gen.php">
<div align="center" style="border: dashed 1px; background-color: #F7F7F7">
<p><span class="style1">Text:</span><br>
<input type="text" name="text">
<br />
<br />
<span class="style1">Font:</span><br>
<select name="font">
<option value="habbo" selected="selected">Habbo Original Font</option>
</select>
<br />
<br />
<input name="Submit" type="submit" class="style2" value="Submit"> </p>
</form>
<div align="center">
<p><span class="style2">Provided by </span><span class="style1">TFN::<em>TeeGen</em></span><em class="style2"> - </em><span class="style2">Created by<strong> ***********.co.uk ;) </strong><strong><br />
</strong></span></p>
</div>
</body>
</html>


Gen.php


<?php
// Set the content-type
header("Content-type: image/gif");
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = $_POST['text'];
// Replace path by your own font path
$font = 'habbo.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
$rand = rand(0, 60000);
// Using imagepng() results in clearer text compared with imagejpeg()
imagegif($im,'$rand.gif');
imagedestroy($im);
?>



Im only just starting to learn PHP bit by bit so I am a complete noob xD

Blob
30-03-2007, 08:29 PM
Ahhh, if you do mean stroke then I am not sure how to do it or even if it is posible.

Ye, the only GD function ive come along for stroke is like a rectangle stroke

Recursion
30-03-2007, 08:31 PM
Check my above post, Just above Dunko's :)

Tomm
30-03-2007, 08:33 PM
You need the GD libary installed.

If you are on shared hosting then I SERIOUSLY recommend you move hosts as the GD libary is standard.

Recursion
30-03-2007, 08:36 PM
Ahh ok, I didn't think Speed-networks had GD -.-

on 1and1 it just comes up with a blank page...

On Eclipse.co.uk hosting it just comes up with
The Image cannot be displayed because it contains errors

Tomm
30-03-2007, 08:42 PM
1and1 suck to start with, they probs aint even heard of GD.

Drop me a PM with the script and ill test it on my server (GD is fully working, check out my msn sig ^^)

Recursion
30-03-2007, 08:45 PM
1and1 do have GD, they are hosting my MSN Sig :P

Want to hide these adverts? Register an account for free!