In PHP, a simple read and write file can be done by using fread() and fwrite(). The unpack() and pack() operator are used to extract binary information.
The question is, how can I read and write PGM (P5) image in PHP without using any additional PHP extension / library?
Depending on maximum value for gray you will have to use
C*for values up to 255 andn*for greater values.Sample class to read / write all pixel from array:
Test case for your file: