Blame view

public/js/common/helpers/image2.js 1.65 KB
ccbikai(👎🏻🍜) authored
1
module.exports = function(imageUrl, opts) {
2
    if (imageUrl) {
姜枫 authored
3
        let params = opts.hash;
4
        let urls = imageUrl.split('?');
姜枫 authored
5
        let query = urls[1] || '';
姜枫 authored
6 7 8 9 10
        let uri = urls[0];

        if (uri.indexOf('http:') === 0) {
            uri = uri.replace('http:', '');
        }
姜枫 authored
11 12

        if (query) {
陈峰 authored
13 14 15
            query = query.replace(/{width}/g, params.w)
                .replace(/{height}/g, params.h)
                .replace(/{mode}/g, (params.mode || 2));
姜枫 authored
16 17 18 19 20 21 22 23 24 25 26 27 28

            if (query.indexOf('imageView2') === 0) {
                if (params.q && query.indexOf('/q/') > 0) {
                    query = query.replace(/\/q\/\d+/g, '/q/' + params.q);
                } else {
                    query += '/q/' + params.q;
                }
            } else if (query.indexOf('imageMogr2') === 0) {
                if (params.q && query.indexOf('/quality/') > 0) {
                    query = query.replace(/\/quality\/\d+/g, '/quality/' + params.q);
                } else {
                    query += '/quality/' + params.q;
                }
29 30 31 32 33 34 35 36 37 38
            } else if (query.indexOf('imageView/') === 0) {
                if (params.q && query.indexOf('/q/') > 0) {
                    query = query.replace(/\/q\/\d+/g, '/q/' + params.q);
                } else {
                    query += '/q/' + params.q;
                }

                if (params.mode) {
                    query = query.replace(/imageView\/\d{1}\//, 'imageView/' + params.mode + '/');
                }
姜枫 authored
39 40 41 42
            }
        } else {
            query = 'imageView2/2/interlace/1/q/' + (params.q || 75);
        }
姜枫 authored
43
        return uri + '?' + query;
姜枫 authored
44
    } else {
45
        return imageUrl;
姜枫 authored
46 47
    }
};