PHP Script for Writing GIF Image Files
PHP can write just about anything. The script below will concentrate on GIF Image files.
The one thing you need to watch here is permissions. On some servers, you need permissions (CHMOD) set to 755 to use the PHP fwrite function, which all the file creations below use in their code. But on other servers (like ours, we discovered), you need permissions set to 777. There are people that understand why. We are not those people, nor is our server host. Older servers seem to need 777. Newer servers need only 755—which is obviously more secure. But this is not a hard and fast rule—your mileage may vary. If you find that you need 777, you may find your host blocks this setting. But you may find you can fwrite with only a 755 CHMOD (public_html AND the PHP script file setting). Some hosts disallow fwrite and even PHP file functions in general. You will find that write-nearly-any-type-of-file-with-php.html is the best place you can go for answers if you are testing a PHP script for writing a file and you get the dreaded permission error message.
There are times when you just need to use PHP to write various types of files. The list below shows you how—it's not hard at all. There are even five PHP scripts for creating graphics/image files. For image creation, we concentrated on the simple task of allowing users to input text and using PHP to take this text and put it in a nice box with a border and save it as a graphics/image file. Let us know how you like these scripts! We kept them as simple as we could so you could get the idea in a flash.
- Write Text File with PHP
- Write JS File with PHP
- Write PHP File with PHP
- Write HTML File with PHP
- Write ASP File with PHP
- Write ASPX File with PHP
- Write XHTML File with PHP
- Write XML File with PHP
- Write AJAX File with PHP
- Write CSS File with PHP
- Write Flash Web Page File with PHP
- Write MP3 Web Page File with PHP
- Write YouTube Video Web Page File with PHP
- Write Windows Media Video Web Page File with PHP
- Write MPEG Video Web Page File with PHP
- Write PNG File with PHP
- Write GIF File with PHP
- Write JPEG File with PHP
- Write WBMP File with PHP
- Write XBM File with PHP
First, notice that we are counting on the GD library to have been enabled in your PHP installation. It usually is, but if it is not, get your host to do so. The
imagegif() function from the GD library is needed—along with several other functions—for this image creation to work. What the 2 scripts below do is turn your typed text input and turn it into an actual GIF image (as long as you have the correct permissions to do this—ask your host).
The mytext-convert-to-gif-image.php script is mostly an HTML input form and the form action is to run this same file, mytext-convert-to-gif-image.php. When it does this page refresh, it POSTs the text to an image-gif.php image creation file. Note that the text gets sent via URL query string in the variable "mytext".
Once the text gets to the image-gif.php script, the script GETs it into the PHP variable $mytext. Variables are defined using the imagefontwidth() and imagefontheight() functions from the GD library. The adding of 20 is to give the final image padding so the words are not jammed against the image edge. Next we use imagecreatetruecolor() to create a new true color image of the needed size. Next we use imagecolorallocate() to allocate colors for the image. The numbers in the argument are RGB color numbers. Then we use the imagefill() function to fill the image with one of the colors. The imageline() function is used to draw a border around the image. The argument numbers are X1 Y1 X2 Y2, which is saying to draw a line from X1,Y1 to X2,Y2. The minus ones in some of the arguments keep the function from trying to draw outside of the edge of the image. The imagestring() function types our text into the image. The numbers are x-coordinate of the upper left corner of the text string and y-coordinate of the upper left corner of the text string. Given that we made the image 20 wider and taller than the text, using 10 now as the offset centers the text in the image. The imagegif() function creates this finished image onscreen, and when used with a file name, it creates the GIF image on the server. The imagedestroy() function frees any memory associated with image creation. This is advisable.
If you want to learn PHP because you do not know it, check out a PHP tutorial.
Here is the result:
Here is the file mytext-convert-to-gif-image.php where you input text to be written as a GIF image:
<html>
<head>
</head>
<body>
<?php
$mytext=$_POST['mytext'];
if(isset($mytext)){print '<img src="image-gif.php?mytext=' . $mytext . '">';}
?>
<table width="654" border="1" align="center" cellpadding="3" cellspacing="0" bgcolor="#ffffff">
<tr>
<td align='center'><b><h2>Enter Some Text</h2></b></td>
</tr>
</table>
<table width="650" border="1" align="center" cellpadding="0" cellspacing="1" bgcolor="#ffffff">
<tr>
<form name="form" method="post" action="mytext-convert-to-gif-image.php">
<td>
<table width="650" border="0" cellpadding="3" cellspacing="1" bgcolor="#ffffff">
<tr>
<td><b>Enter text to convert to image</b></td>
</tr>
<tr>
<td><input name="mytext" type="text" size="100" maxlength="100"></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit" value="Reset"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>
Here is the image-gif.php file called by the above file:
<?php
header("Content-type: image/gif");
$mytext = $_GET['mytext'];
$fontsize = 5;
$wide = imagefontwidth($fontsize) * strlen($mytext) + 20;
$high = imagefontheight($fontsize) + 20;
$picture = imagecreatetruecolor($wide,$high);
$gray = imagecolorallocate($picture,223,223,223);
$blue = imagecolorallocate($picture,0,0,255);
imagefill($picture,0,0,$gray);
$black=imagecolorallocate($picture, 0, 0, 0);
imageline($picture, 0, 0, 0, $high, $black);
imageline($picture, 0, 0, $wide, 0, $black);
imageline($picture, $wide-1, 0, $wide-1, $high-1, $black);
imageline($picture, 0, $high-1, $wide-1, $high-1, $black);
imagestring($picture,$fontsize,10,10,$mytext,$blue);
imagegif($picture);
imagegif($picture,"mytextpic.gif");
imagedestroy($picture);
?>