Hi,
i'd like to add to the webp FAQ a very simple JS script to detect webp features, for reference.
Here's what i was thinking of:
<script language="javascript">
// 'feature' can be one of 'lossy', 'lossless' or 'alpha'.
// callback(result) will be passed back the detection result
function check_webp_feature(feature, callback) {
var kTestImages = {
// VP8 (lossy.webp)
lossy: "UklGRiIAAABXRUJQVlA4IBYAAAAwAQCdASoBAAEADsD+JaQAA3AAAAAA",
// VP8L (lossless.webp)
lossless: "UklGRhoAAABXRUJQVlA4TA0AAAAvAAAAEAcQERGIiP4HAA==",
// VP8X+ALPH+VP8 (alpha.webp)
alpha: "UklGRkoAAABXRUJQVlA4WAoAAAAQAAAAAAAAAAAAQUxQSAwAAAARBxAR/Q9ERP8DAABWUDggGAAAABQBAJ0BKgEAAQAAAP4AAA3AAP7mtQAAAA==",
};
var img = new Image();
img.src = "data:image/webp;base64," + kTestImages[feature];
img.onload = img.onerror = function() {
var result = (img.width == 1) && (img.width == 1);
callback(result);
}
}
</script>
(tested it on most browsers, including the webp-enabled mozilla-central-webp fork).
i'm attaching the small webp files i used there, for completeness.
Any objections? Is there anything wrong, overlooked or missing in the script?
For completeness, here's an HTML body example you can toy with to make use of the script:
<body>
<h3>Javascript-based WebP detection</h3>
<form name="webp_form">
<input type="button" name="test" value="click to check webp features"
onClick="check_webp_feature('lossy', function(result) { document.forms.webp_form.lossy.checked = result; });
check_webp_feature('lossless', function(result) { document.forms.webp_form.lossless.checked = result; });
check_webp_feature('alpha', function(result) { document.forms.webp_form.alpha.checked = result; });
"><br/>
<input type="checkbox" name="lossy"> lossy<br>
<input type="checkbox" name="lossless"> lossless<br>
<input type="checkbox" name="alpha"> alpha+lossy<br>
</form>
</body>
I'll check that in in the FAQ in few days unless something is wrong, so please comment if needed!
thx,
skal