PDA

View Full Version : The GD Function



JamesRouale
10-12-2006, 11:53 PM
I'm not a beginner as such to the language of PHP, however the GD function is puzzling me... I would appreciate a script (with comments if possible) which merged with a HTML based form, would allow me to generate a text based image.

All attempts, working or otherwise, will be appreciated and reputation will be awarded to such users.

Thank you, James.

nets
11-12-2006, 01:44 AM
<?php

header('content-type: image/png');
$m = ($_GET['m']) ? $_GET['m'] : 'No Message';

$image = imagecreate(250, 25);
$black = imagecolorallocate($image, 255, 255, 255);
$white = imagecolorallocate($image, 0, 0, 0);
imagestring($image, 1, 10, 10, $m, $white);
imagepng($image);
imagedestroy($image);

?>
You can change GET to POST, and on your basic form have a field named "m", where the user enters their message. Alternatively, you could use JavaScript to make your page display the image more dynamically:


<html>
<head>
<title>GD Example</title>
<script type="text/javascript">
<!--
function li() {
var m = document.getElementById('message');
var i = document.getElementById('image');
i.src = 'image.php?m='+m.value;
m.value = '';
m.select();
}
-->
</script>
</head>
<body>
<pre>
<input id="message" /> <button onclick="li();">Display</button>
<img src="image.php" id="image" />
</pre>
</body>
</html>

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