Dies ist ein Quellcode in reinem JS, um das weit verbreitete Webp -Format in das JPG -Format umzuwandeln (PNG auch möglich).
Nach dem Ausführen einer Website mit diesem Code und dem Einstellen von URL-to-Webp-Image konvertiert das WebPtoJPG das Bild von WebP in JPG und platziert es in die IMG. Es funktioniert nur im Browser, das WebP unterstützt, also hauptsächlich Chrome.
<img id='a' src='URL-TO-WEBP-IMAGE' />
<script>
function webpToJpg(id) {
var image = new Image();
image.onload = function () {
var canvas = document.createElement('canvas');
canvas.width = this.naturalWidth;
canvas.height = this.naturalHeight;
canvas.getContext('2d').drawImage(this, 0, 0);
document.getElementById(id).src = canvas.toDataURL('image/jpeg');
};
image.src = document.getElementById(id).src;
}
webpToJpg('a');
</script>