Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Allow detecting image type from file signature #4

@ShadyGuy123

Description

@ShadyGuy123

Some websites "lie" about the image type, maybe because a user uploads a PNG that then gets downscaled and converted to a JPEG, but keeps the .png extension and is reported as image/png anyway.

It'd be wonderful if there was an option to instead report the true image type from the file signature.

I've managed to kinda work around this with a (badly coded) userscript that, given an image as an arraybuffer, gets the true image type and replaces the MIME type in the header, however I wouldn't know how to integrate this into the extension code myself.

Partial userscript code
function getImageType(arrayBuffer) {
    var type;
    var dv = new DataView(arrayBuffer, 0, 5);
    var nume1 = dv.getUint8(0, true);
    var nume2 = dv.getUint8(1, true);
    var hex = nume1.toString(16) + nume2.toString(16) ;

    switch(hex) {
        case "8950":
            type = "image/png";
            break;
        case "4749":
            type = "image/gif";
            break;
        case "424d":
            type = "image/bmp";
            break;
        case "ffd8":
            type = "image/jpeg";
            break;
        default:
            type = null;
            break;
    }
    return type;
}

// [...]
document.getElementById("imgData-mime").innerText = imgType;
// Then this extension looks at imgData-mime and reads what I've written there instead

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions