PHP Convert Image To 16 Level Grayscale, Then Read Each Pixel

I have a client in need of a solution... preferable in PHP as I am not that skilled in ASP.net - yet.

Client is an artist mainly working in acrylic and works in a cross between expressionism and abstract. One of his experimental efforts requires an application to be built so that he can break down tonal information.

Basically, he needs a web based application that will allow him to upload/import a color image or black and white image, have it converted to 16 level gray scale and then identify each pixel value to display/print in line order:

In the above example, Image would be 25 pixels wide, 20 pixels tall - but the function needs to accommodate images of varying widths and heights.

What I need help with is a PHP5 Function to convert the image to 16 Level/Bit Grayscale, and then a separate function to read the pixel of each image. I do know the GD Library is compiled into PHP, but I do not believe ImageMagik is. I checked using PhpInfo() and it isn't in the list.

From the hours of reading I have done, I am pretty sure it is possible - I am just not sure how exactly.

@smadeira
Thank you for the solution. I have not had a chance to try it yet but will do so this weekend.

@Slick812
Your solution seems similar to that of smadeira but I do like that you took the time to also add the output code in as well. As for why groups of 5? I honestly do not have an answer except to say that I think it simply allows the output to be easier to read. I will double check with the client to see if there is any other reason but I suspect that will be the reasoning. I will try your solution this weekend as well and see which one best produces the results required.

Thank you to the both of you. Excellent work and I greatly appreciate the quick response.

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

I was in a hurry when I did it before, I looked at my code and could see that I do not need to loop through the pixels twice, and can do the operation in a single loop, which should be much more efficient, and fast, with alot less code to maintain.
try this out -

@Slick812
The new code you've provided is definitely optimized. I think that should run a lot faster.

I just got word from the client, and the scope of the project has changed slightly. I think I should be able to take your code and amend it - but just to list the changes.

1. If image is larger than 200x200, it needs to be resized down to 200x200 (while maintaining aspect ratio).

2. The resized image from # 1 needs to be saved on the server so that it can be downloaded.

I already know how to do both of those tasks already so I am assuming that after I resize the image, I just load the image name/location into your $img= variable?

I still haven't tested the code yet but from what I see, it should work. If you want to add code in for the extra options, that would be great - but if not, that's okay too. Once I test the code, I'll be able to let you know how it all works out.

I have reworked this php app to the new design specs you have given, however, I had to change the "200x200 (while maintaining aspect ratio" down to "100x100", I did this in order to have the display of an image pixel ROW in the image not to wrap into 2 lines on the screen in the output display, But you can change the $dimention variable to whatever max small size you decide to use. I also had to not use the "spaces" between the HEX letters and numbers, for the same reason, to reduce the horizontal size of a display row.

@Slick812
Just want to say I haven't forgotten about this. Right now, client is away and difficult to reach. From what I see, everything looks great and I should be able to take it from there and fill in the rest of the requirements. I just want to make sure there are no other major changes needed. I shoul be able to clue up this on the weekend.

Featured Post

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.