PHP Script for Writing WBMP Image Files
PHP can write just about anything. The script below will concentrate on WBMP 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
imagewbmp() 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 WBMP image (as long as you have the correct permissions to do this—ask your host).
The mytext-convert-to-wbmp-image.php script is mostly an HTML input form and the form action is to run this same file, mytext-convert-to-wbmp-image.php. When it does this page refresh, it POSTs the text to an image-wbmp.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-wbmp.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 imagewbmp() function creates this finished image onscreen, and when used with a file name, it creates the WBMP image on the server. The imagedestroy() function frees any memory associated with image creation. This is advisable.
WBMP file types are primarily associated with 'Wireless Bitmap File Format'. WBMP is a WAP graphic format optimized for mobile computing devices. The pictures in the WBMP format are saved in bit format. That is every pixel of an image is saved as 1 bit. Thus an 8 x 8 pixel image will require only 64 bits (8 bytes). The limitation of this format is that it can only save images in Black and White only (no support for color or gray scale). Here is some more info on the WBMP file format.
As mobile phones have become more advanced, the WBMP format has largely been replaced by full color image and video formats.
WBMP files are different from most image files: they deal only in black and white and only with wireless data—like mobile phone graphics. The imagewbmp() function always messes up if anything but black and white is used, so we used only black and white. The imagewbmp() function WBMP support is only available if PHP was compiled against GD-1.8 or later. GD is the PHP graphics library.
If you want to learn PHP because you do not know it, check out a PHP tutorial.
Here is the result, but since browsers cannot see WBMP images, here is a jpeg copy of the image:
Here is the file mytext-convert-to-wbmp-image.php where you input text to be written as a WBMP image:
<html>
<head>
</head>
<body>
<?php
$mytext=$_POST['mytext'];
if(isset($mytext)){print '<img src="image-wbmp.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-wbmp-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-wbmp.php file called by the above file:
<?php
header("Content-type: image/vnd.wap.wbmp");
$mytext = $_GET['mytext'];
$fontsize = 5;
$wide = imagefontwidth($fontsize) * strlen($mytext) + 20;
$high = imagefontheight($fontsize) + 20;
$picture = imagecreatetruecolor($wide,$high);
$white = imagecolorallocate($picture,255,255,255);
imagefill($picture,0,0,$white);
$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,$black);
imagewbmp($picture); //WBMP support is only available if PHP was compiled against GD-1.8 or later
imagewbmp($picture,"mytextpic.wbmp");
imagedestroy($picture);
?>