writing programs which create image files

33
Writing programs which create image files

Upload: idola-barry

Post on 04-Jan-2016

39 views

Category:

Documents


0 download

DESCRIPTION

Writing programs which create image files. 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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Writing programs which create image files

Writing programs which create image files

Page 2: Writing programs which create image files

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

Page 3: Writing programs which create image files

Creating image files with PHP

• An example PHP program which writes two bytes of data, 42hex and 4Dhex, into a file called file1

<?php $fp=fopen(“file1”,”w”);

$byte=chr(0x42); fwrite($fp,$byte);

$byte=chr(0x4D); fwrite($fp,$byte);

fclose($fp); ?>

Page 4: Writing programs which create image files

Folder before this program is executed

Page 5: Writing programs which create image files

Browser window when this program is executed

Page 6: Writing programs which create image files

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

Page 7: Writing programs which create image files

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

Page 8: Writing programs which create image files

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

Page 9: Writing programs which create image files

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.

Page 10: Writing programs which create image files

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

Page 11: Writing programs which create image files

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

Page 12: Writing programs which create image files

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

Page 13: Writing programs which create image files

Folder before this program is executed

Page 14: Writing programs which create image files

Browser window when this program is executed

Page 15: Writing programs which create image files

Folder after this program is executed

• The file called file1 has been created

Page 16: Writing programs which create image files

Let’s inspect the file with XVI32

• Right-click on the file and download it• Then, …

Page 17: Writing programs which create image files

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

Page 18: Writing programs which create image files

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

Page 19: Writing programs which create image files

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-15hex and 16-19hex, respectively

– specify the pixels from byte 36hex 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-05hex

Page 20: Writing programs which create image files

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(0x00); fwrite($fp,$byte);$byte=chr(0x00); fwrite($fp,$byte);$byte=chr(0x00); fwrite($fp,$byte);$byte=chr(0x00); fwrite($fp,$byte);$byte=chr(0x00); fwrite($fp,$byte);$byte=chr(0x00); fwrite($fp,$byte);$byte=chr(0x36); fwrite($fp,$byte);$byte=chr(0x00); fwrite($fp,$byte);$byte=chr(0x00); fwrite($fp,$byte);$byte=chr(0x00); fwrite($fp,$byte);$byte=chr(0x28); fwrite($fp,$byte);$byte=chr(0x00); fwrite($fp,$byte);

Page 21: Writing programs which create image files

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);fwrite($fp,$byte);fwrite($fp,$byte);fwrite($fp,$byte);fwrite($fp,$byte);fwrite($fp,$byte);$byte=chr(0x36); fwrite($fp,$byte);$byte=chr(0x00); fwrite($fp,$byte);fwrite($fp,$byte);fwrite($fp,$byte);$byte=chr(0x28); fwrite($fp,$byte);$byte=chr(0x00); fwrite($fp,$byte);

Page 22: Writing programs which create image files

PHP program to create a 24-bit depth BMP file (part 2)

fwrite($fp,$byte);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);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);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);fwrite($fp,$byte);

Page 23: Writing programs which create image files

PHP program to create a 24-bit depth BMP file (part 3)

fwrite($fp,$byte);fwrite($fp,$byte);fwrite($fp,$byte);fwrite($fp,$byte);fwrite($fp,$byte);fwrite($fp,$byte);fwrite($fp,$byte);fwrite($fp,$byte);fwrite($fp,$byte);fwrite($fp,$byte);fwrite($fp,$byte);fwrite($fp,$byte);fwrite($fp,$byte);fwrite($fp,$byte);fwrite($fp,$byte);fwrite($fp,$byte);

Page 24: Writing programs which create image files

PHP program to create a 24-bit depth BMP file (part 4)

fwrite($fp,$byte);fwrite($fp,$byte);fwrite($fp,$byte);fwrite($fp,$byte);fwrite($fp,$byte);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);

Page 25: Writing programs which create image files

PHP program to create a 24-bit depth BMP file (part 5)

$byte=chr(0x00); fwrite($fp,$byte);fwrite($fp,$byte);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);

Page 26: Writing programs which create image files

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

Page 27: Writing programs which create image files

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

?>

Page 28: Writing programs which create image files

Folder before this program is executed

Page 29: Writing programs which create image files

Browser window when this program is executed

Page 30: Writing programs which create image files

Folder after this program is executed

• The file called file2.bmp has been created• Let’s open it in the browser …

Page 31: Writing programs which create image files

Browser window when file2.bmp is opened

• Let’s zoom in …

Page 32: Writing programs which create image files

Browser window when zoom level is 400%

Page 33: Writing programs which create image files

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