i wonder, how i could figure out if an image has a transparency effect applied. Is there any way in JavaScript or HTML5? I have a Base64-coded image. Is there a way to read out the transparency-information (alpha-channel). For example, if i load a PNG-Image, then convert it to base64, then drop it to html5-canvas, now how can i know if this has transparency-effect activated? thanx alot okyo
Asked
Active
Viewed 1,278 times
1
-
I wasn't happy with the answer but, http://stackoverflow.com/questions/2569538/detecting-if-a-png-image-file-is-a-transparent-image – kenny Dec 12 '11 at 02:20
1 Answers
0
When you say 'drop it to html5-canvas', I assume you mean using an image element with the 'data:' URI scheme. Also, let's take it as given that you don't want to write javascript code to parse the image files.
You could do something like this pseudo-code:
create 2 off-screen canvases
color one opaque white and the other opaque black
draw the image on both of them
call getImageData on each canvas, using the image bounds
compare the image data
If the image has any transparent or partially-transparent pixels, then presumably the two canvases will end up at least a little different. One exception would be if the image has the transparency feature enabled but is entirely opaque anyway. Another would be if the non-opaque pixels are only very slightly transparent - not enough to alter a white or black background. But this technique would catch images where transparency is noticeable.

James Clark
- 1,765
- 13
- 17
-
thanx for your ideas, i will try to realise it that way! But acctually my intention was to use the base64-string to figure out wich part of it is a representation of the alpha channel!? e.g. Read last 40 signs in Base64 and there you got the transparency-information! no way like this possible? thanx again for your support! – Okyo Dec 14 '11 at 23:42
-
There isn't enough in common between formats for there to be an easy check. You would need to write code to understand each of the file formats you want to support. For instance, http://www.libpng.org/pub/png/spec/ . You might be able to shortcut some of the formats to get quickly to the alpha support information, but still I don't think it will be worth your time to do all that. – James Clark Dec 15 '11 at 01:08