js圖片壓縮預(yù)覽

圖片壓縮預(yù)覽(具體上傳請(qǐng)看另外一篇文章,講解的更詳細(xì)圖片壓縮上傳

html結(jié)構(gòu)

<div class="preview" id="preview">
      ![](img.png)
</div>
<input type="file" name="file" id="imgInput" 
            onchange="previewImage(this,'preview','img','imgInput')" class="dis">

js代碼

//圖片上傳預(yù)覽    IE是用了濾鏡。
function previewImage(file, previewDiv, imgId, imgInputId) {
    var MAXWIDTH = document.getElementById(imgId).width;
    var MAXHEIGHT = document.getElementById(imgId).height;
    var div = document.getElementById(previewDiv);
    if (file.files && file.files[0]) {

        // 格式不正確的錯(cuò)誤提示
        var error = document.createElement('p');
        error.className = 'pic-error';

        //如果不是圖片格式,不往下執(zhí)行
        if (file.files[0].type !== 'image/jpeg' && file.files[0].type !== 'image/jpg' && file.files[0].type !== 'image/png' && file.files[0].type !== 'image/gif') {
            error.innerHTML = '請(qǐng)上傳圖片格式的文件(jpeg/jpg/png/gif)';
            div.appendChild(error);
            return;
        }

        //限制6M以下的圖片
        if(file.size > 6*1024*1026){
            error.innerHTML = '請(qǐng)上傳大小在6M以下的圖片';
            div.appendChild(error);
            return;
        }

        //移除錯(cuò)誤提示
        if(div.childNodes[1]){
            div.removeChild(div.childNodes[1]);
        }

        div.innerHTML = '<img id="' + imgId + '" onclick=$("#' + imgInputId + '").click()>';
        var img = document.getElementById(imgId);
        img.onload = function () {
            var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
            img.width = rect.width;
            img.height = rect.height;
            img.style.marginTop = rect.top + 'px';
        }
        var reader = new FileReader();
        reader.onload = function (evt) {
            img.src = evt.target.result;
        }
        reader.readAsDataURL(file.files[0]);
    }
    else //兼容IE
    {
        var sFilter = 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
        file.select();
        var src = document.selection.createRange().text;
        div.innerHTML = '<img id=' + imgId + '>';
        var img = document.getElementById(imgId);
        img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
        var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
        status = ('rect:' + rect.top + ',' + rect.left + ',' + rect.width + ',' + rect.height);
        div.innerHTML = "<div id=divhead style='width:" + rect.width + "px;height:" + rect.height + "px;margin-top:" + rect.top + "px;" + sFilter + src + "\"'></div>";
    }
}
// 計(jì)算根據(jù)原有img標(biāo)簽寬高計(jì)算裁剪后的圖片寬高
function clacImgZoomParam(maxWidth, maxHeight, width, height) {
    var param = {top: 0, left: 0, width: width, height: height};
    if (width > maxWidth || height > maxHeight) {
        rateWidth = width / maxWidth;
        rateHeight = height / maxHeight;

        if (rateWidth > rateHeight) {
            param.width = maxWidth;
            param.height = Math.round(height / rateWidth);
        } else {
            param.width = Math.round(width / rateHeight);
            param.height = maxHeight;
        }
    }
    param.left = Math.round((maxWidth - param.width) / 2);
    param.top = Math.round((maxHeight - param.height) / 2);
    return param;
}

css代碼

.dis{
    display: none;
}
.preview{
    display: table-cell;
    width: 300px;
    height: 200px;
    border: 1px solid #ccc;
    background-color: #ccc;
    border-radius: 5px;
    text-align: center;
    vertical-align: middle;
}
.preview img{
    border: 0;
}
            

歡迎訪問(wèn)我的博客,同步發(fā)布
感謝閱讀,如有錯(cuò)誤歡迎指正。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容