Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Writing programs which create image files

2 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

3 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); ?>

4 Folder before this program is executed

5 Browser window when this program is executed

6 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

7 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--

8 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

9 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.

10 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

11 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

12 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

13 Folder before this program is executed

14 Browser window when this program is executed

15 Folder after this program is executed The file called file1 has been created

16 Let’s inspect the file with XVI32 Right-click on the file and download it Then, …

17 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); ?>

18 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

19 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 12-15 hex and 16-19 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 02-05 hex

20 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);

21 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);

22 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);

23 PHP program to create a 24-bit depth BMP file (part 3) fwrite($fp,$byte);

24 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);

25 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);

26 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);

27 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); ?>

28 Folder before this program is executed

29 Browser window when this program is executed

30 Folder after this program is executed The file called file2.bmp has been created Let’s open it in the browser …

31 Browser window when file2.bmp is opened Let’s zoom in …

32 Browser window when zoom level is 400%

33 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


Download ppt "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."

Similar presentations


Ads by Google