Writing programs which create image files
We have seen that an image file just contains a sequence of bytes –We know, in detail, the rules that apply to the sequence of bytes in a BMP file Most programming languages allow us to create new files and to write data into such files PHP is one such programming language We will use it to create some image files
Creating image files with PHP An example PHP program which writes two bytes of data, 42 hex and 4D hex, into a file called file1 <?php $fp=fopen(“file1”,”w”); $byte=chr(0x42); fwrite($fp,$byte); $byte=chr(0x4D); fwrite($fp,$byte); fclose($fp); ?>
Folder before this program is executed
Browser window when this program is executed
Folder after this program is executed The file called file1 has not been created Why not? Because the web server (which executes the PHP program) does not have permission to write into the folder /j.bowen/cs1107/exercises/tests/demo
Unix/Linux Permissions The computer on which the web server runs uses Unix Log in and view the folder Using the command ls –l we can see detailed information about the program demo.php The permissions associated with demo.php are -rw-r--r--
Unix/Linux Permissions (contd.) The set of possible permissions that a file or directory (folder) can have is drwxrwxrwx The first position contains d if and only if the item is a directory (folder) so the hyphen in the first position of the demo.php permission confirms that demo.php is a file, not a folder The other positions come in groups of three -rwxrwxrwx
Unix/Linux Permissions (contd.) The permissions come in sets of three -rwxrwxrwx The first set specifies whether the user who owns the item can read, write or execute the item The second set specifies whether the group to which the owning user belongs can read, write or execute the item The third set specifies whether other users can read, write or execute the item The permissions associated with demo.php are -rw-r--r-- –So the user who owns demo.php can read and write the file; but the group and others can only read it.
Permissions for /j.bowen/cs1107/exercises/tests/demo Use the cd.. command to go up to the parent directory Using the command ls –l we can see detailed information about the contents of the parent directory The permissions associated with our target folder are drwxr-xr-x So only the user can write into the target folder
Permissions for /j.bowen/cs1107/exercises/tests/demo Use the chmod g+w command to let the group write into the folder chmod g+w demo Using the command ls –l we can see that the permissions associated with our target folder are now drwxrwxr-x So the user and his group can write into the target folder
Permissions for /j.bowen/cs1107/exercises/tests/demo The permissions associated with our target folder are now drwxrwxr-x The web server (which runs PHP programs) is part of each user’s group –so a program controlled by the web server can now write into the target folder So, let’s execute our program again and see what happens
Folder before this program is executed
Browser window when this program is executed
Folder after this program is executed The file called file1 has been created
Let’s inspect the file with XVI32 Right-click on the file and download it Then, …
Let’s inspect the file with XVI32 … then open the file with XVI32 We see that the contents are as expected <?php $fp=fopen(“file1”,”w”); $byte=chr(0x42); fwrite($fp,$byte); $byte=chr(0x4D); fwrite($fp,$byte); fclose($fp); ?>
We can use PHP to create BMP files We have just seen that we can use PHP to write arbitrary bytes into a file Therefore, we can use PHP to create BMP files For example, we can easily create a BMP file by following the rules we saw earlier for the absolutely basic 24-bit depth BMP format
Absolutely basic 24-bit depth BMP format The minimum we must do when making a 24-bit depth BMP file is –assign the fixed values shown below to the bytes marked green –put the image width and depth in bytes hex and hex, respectively –specify the pixels from byte 36 hex onwards, 3 bytes per pixel, padding the lines if necessary to make each line a multiple of four bytes –put the overall file size in bytes hex
PHP program to create a 24-bit depth BMP file (part 1) <?php $fp=fopen("file2.bmp","w"); $byte=chr(0x42); fwrite($fp,$byte); $byte=chr(0x4D); fwrite($fp,$byte); // The next four bytes give the file size $byte=chr(0x66); fwrite($fp,$byte); $byte=chr(0x00); fwrite($fp,$byte); $byte=chr(0x36); fwrite($fp,$byte); $byte=chr(0x00); fwrite($fp,$byte); $byte=chr(0x28); fwrite($fp,$byte); $byte=chr(0x00); fwrite($fp,$byte);
Creating a 24-bit depth BMP file (part 1, abbreviated) <?php $fp=fopen("file2.bmp","w"); $byte=chr(0x42); fwrite($fp,$byte); $byte=chr(0x4D); fwrite($fp,$byte); // The next four bytes give the file size $byte=chr(0x66); fwrite($fp,$byte); $byte=chr(0x00); fwrite($fp,$byte); fwrite($fp,$byte); $byte=chr(0x36); fwrite($fp,$byte); $byte=chr(0x00); fwrite($fp,$byte); fwrite($fp,$byte); $byte=chr(0x28); fwrite($fp,$byte); $byte=chr(0x00); fwrite($fp,$byte);
PHP program to create a 24-bit depth BMP file (part 2) fwrite($fp,$byte); // The next four bytes give the width $byte=chr(0x04); fwrite($fp,$byte); $byte=chr(0x00); fwrite($fp,$byte); fwrite($fp,$byte); // The next four bytes give the height $byte=chr(0x04); fwrite($fp,$byte); $byte=chr(0x00); fwrite($fp,$byte); fwrite($fp,$byte); $byte=chr(0x01); fwrite($fp,$byte); $byte=chr(0x00); fwrite($fp,$byte); $byte=chr(0x18); fwrite($fp,$byte); $byte=chr(0x00); fwrite($fp,$byte); fwrite($fp,$byte);
PHP program to create a 24-bit depth BMP file (part 3) fwrite($fp,$byte);
PHP program to create a 24-bit depth BMP file (part 4) fwrite($fp,$byte); //The pixel data start here $byte=chr(0xFF); fwrite($fp,$byte); $byte=chr(0x00); fwrite($fp,$byte); fwrite($fp,$byte); $byte=chr(0xFF); fwrite($fp,$byte); $byte=chr(0x00); fwrite($fp,$byte); fwrite($fp,$byte); $byte=chr(0xFF); fwrite($fp,$byte); $byte=chr(0x00); fwrite($fp,$byte); fwrite($fp,$byte); $byte=chr(0xFF); fwrite($fp,$byte);
PHP program to create a 24-bit depth BMP file (part 5) $byte=chr(0x00); fwrite($fp,$byte); fwrite($fp,$byte); $byte=chr(0xFF); fwrite($fp,$byte); $byte=chr(0x00); fwrite($fp,$byte); fwrite($fp,$byte); $byte=chr(0xFF); fwrite($fp,$byte); $byte=chr(0x00); fwrite($fp,$byte); fwrite($fp,$byte); $byte=chr(0xFF); fwrite($fp,$byte); $byte=chr(0x00); fwrite($fp,$byte); fwrite($fp,$byte); $byte=chr(0xFF); fwrite($fp,$byte); fwrite($fp,$byte); $byte=chr(0x00); fwrite($fp,$byte);
PHP program to create a 24-bit depth BMP file (part 6) fwrite($fp,$byte); $byte=chr(0xFF); fwrite($fp,$byte); $byte=chr(0x00); fwrite($fp,$byte); fwrite($fp,$byte); $byte=chr(0xFF); fwrite($fp,$byte); $byte=chr(0x00); fwrite($fp,$byte); fwrite($fp,$byte); $byte=chr(0xFF); fwrite($fp,$byte); $byte=chr(0x00); fwrite($fp,$byte); fwrite($fp,$byte); $byte=chr(0xFF); fwrite($fp,$byte); $byte=chr(0x00); fwrite($fp,$byte); fwrite($fp,$byte); $byte=chr(0xFF); fwrite($fp,$byte);
PHP program to create a 24-bit depth BMP file (part 7) $byte=chr(0x00);fwrite($fp,$byte); fwrite($fp,$byte); $byte=chr(0xFF);fwrite($fp,$byte); $byte=chr(0x00);fwrite($fp,$byte); fwrite($fp,$byte); $byte=chr(0xFF);fwrite($fp,$byte); fclose($fp); ?>
Folder before this program is executed
Browser window when this program is executed
Folder after this program is executed The file called file2.bmp has been created Let’s open it in the browser …
Browser window when file2.bmp is opened Let’s zoom in …
Browser window when zoom level is 400%
Higher-level PHP constructs for manipulating image files We know how to create image files by specifying individual bytes But this is very tedious PHP provides higher-level constructs for creating and/or editing image files We will look at these in the next set of slides