国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

javascript - How to determine whether an image is base64 in js
巴扎黑
巴扎黑 2017-07-05 11:05:26
0
6
2115

Multiple img tags, the src of each tag is different,

Now we need to do different processing for images whose src is base64 encoded and non-base64 images,

How should javascript distinguish whether the image is base64?

巴扎黑
巴扎黑

reply all(6)
漂亮男人

BASE64 codes always start with the form data:image/xxx;base64,xxxxxx..., so just write a regular expression and test src

阿神
$('img').each((i,item)=>{
    let src = item.src
    if(src.indexOf('data:image/jpg;base64,')>-1){
        // base64 圖片操作
    }else{
        //path 圖片操作
    }
})
為情所困

Are non-base64 images all URL addresses?

給我你的懷抱

Just match according to the beginning of src

$('img').each((i,item)=>{
    let src = item.src
    if(src.indexOf('data:image')>-1){
        // base64 圖片操作
    }else{
        //path 圖片操作
    }
})
我想大聲告訴你

You need to use startWith, which is more efficient:

$('img').each((i,item)=>{
    let src = item.src
    if(src.startWith('data:image')){
        // base64 圖片操作
    }else{
        //path 圖片操作
    }
})
phpcn_u1582
function validDataUrl(s) {
    return validDataUrl.regex.test(s);
}
validDataUrl.regex = /^\s*data:([a-z]+\/[a-z0-9-+.]+(;[a-z-]+=[a-z0-9-]+)?)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@\/?%\s]*?)\s*$/i;

module.exports = validDataUrl;
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template