[php] 이미지 파일 미리보기 (preview local image file) > 팁앤테크

본문 바로가기
사이트 내 전체검색

팁앤테크

[php] 이미지 파일 미리보기 (preview local image file)

페이지 정보

본문

html 상의 input:file에서 선택한 이미지 파일을 서버에 업로드하지 않고 미리보기하는 방법은 크게 2가지임.
하나는 FileReader를 이용하는 방법이고, 하나는 IE 전용 filter를 이용하는 방법.

FileReader의 경우 왠만한 최신 브라우저는 다 지원함.

http://caniuse.com/#feat=filereader <- 지원하는 브라우저는 여기서 확인가능.
IE의 경우 IE10부터 FileReader가 있고 그 하위버전은 filter를 이용해야 함.


개인적으로 로컬 이미지 파일 미리보기를 테스트해서 성공한 브라우저는 다음과 같음.

Chrome 37.0.2062.124

FireFox 32.0.3

Opera 24.0.1558.64

IE11 (10,9,8,7은 에뮬레이션 모드로 테스트)

아이폰 Safari


Safari5 의 경우 FileReader도 없고 filter도 없어서 미리보기가 불가능했음. (Safari6 부터는 FileReader 있음)


아래는 jQuery용으로 만든 로컬 이미지 미리보기.

소스만 긁어서 바로 테스트할 수 있도록 html 파일 전체 소스를 올림.


<!doctype html>
 
 
<head>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
 
<script>
$.fn.setPreview = function(opt){
    "use strict"
    var defaultOpt = {
        inputFile: $(this),
        img: null,
        w: 200,
        h: 200
    };
    $.extend(defaultOpt, opt);
 
    var previewImage = function(){
        if (!defaultOpt.inputFile || !defaultOpt.img) return;
 
        var inputFile = defaultOpt.inputFile.get(0);
        var img       = defaultOpt.img.get(0);
 
        // FileReader
        if (window.FileReader) {
            // image 파일만
            if (!inputFile.files[0].type.match(/image\//)) return;
 
            // preview
            try {
                var reader = new FileReader();
                reader.onload = function(e){
                    img.src = e.target.result;
                    img.style.width  = defaultOpt.w+'px';
                    img.style.height = defaultOpt.h+'px';
                    img.style.display = '';
                }
                reader.readAsDataURL(inputFile.files[0]);
            } catch (e) {
                // exception...
            }
        // img.filters (MSIE)
        } else if (img.filters) {
            inputFile.select();
            inputFile.blur();
            var imgSrc = document.selection.createRange().text;
 
            img.style.width  = defaultOpt.w+'px';
            img.style.height = defaultOpt.h+'px';
            img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enable='true',sizingMethod='scale',src=\""+imgSrc+"\")";          
            img.style.display = '';
        // no support
        } else {
            // Safari5, ...
        }
    };
 
    // onchange
    $(this).change(function(){
        previewImage();
    });
};
 
 
$(document).ready(function(){
    var opt = {
        img: $('#img_preview'),
        w: 200,
        h: 200
    };
 
    $('#input_file').setPreview(opt);
});
</script>
</head>
 
 
<body>
<input type="file" id="input_file" />
<br />
<img id="img_preview" style="display:none;"/>
 
</body>
</html>

 

좋은 정보가 있어서 담아 왔습니다.  

***************************************

높이값을 자동으로 하려면

 

h: 200 를 h: 'auto'로 바꾸시고

img.style.height = defaultOpt.h+'px'; 에서

px단위를 지워주시면 됩니다.

 

셈플소스

*******************************************

 
<!doctype html>
 
 
<head>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
 
<script>
$.fn.setPreview = function(opt){
    "use strict"
    var defaultOpt = {
        inputFile: $(this),
        img: null,
        w: 200,
        h: 'auto'
    };
    $.extend(defaultOpt, opt);
 
    var previewImage = function(){
        if (!defaultOpt.inputFile || !defaultOpt.img) return;
 
        var inputFile = defaultOpt.inputFile.get(0);
        var img       = defaultOpt.img.get(0);
 
        // FileReader
        if (window.FileReader) {
            // image 파일만
            if (!inputFile.files[0].type.match(/image\//)) return;
 
            // preview
            try {
                var reader = new FileReader();
                reader.onload = function(e){
                    img.src = e.target.result;
                    img.style.width  = defaultOpt.w+'px';
                    img.style.height = defaultOpt.h;
                    img.style.display = '';
                }
                reader.readAsDataURL(inputFile.files[0]);
            } catch (e) {
                // exception...
            }
        // img.filters (MSIE)
        } else if (img.filters) {
            inputFile.select();
            inputFile.blur();
            var imgSrc = document.selection.createRange().text;
 
            img.style.width  = defaultOpt.w+'px';
            img.style.height = defaultOpt.h;
            img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enable='true',sizingMethod='scale',src=\""+imgSrc+"\")";          
            img.style.display = '';
        // no support
        } else {
            // Safari5, ...
        }
    };
 
    // onchange
    $(this).change(function(){
        previewImage();
    });
};
 
 
$(document).ready(function(){
    var opt = {
        img: $('#img_preview'),
        w: 200,
        h: 'auto'
    };
 
    $('#input_file').setPreview(opt);
});
</script>
</head>
 
 
<body>
<input type="file" id="input_file" />
<br />
<img id="img_preview" style="display:none;"/>
 
</body>
</html> 

추천0

댓글목록

등록된 댓글이 없습니다.

Total 825건 14 페이지
  • RSS
팁앤테크 목록
번호 제목 글쓴이 조회 추천 날짜
500 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 14464 0 12-10
499 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 14440 0 10-16
498 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 14429 0 04-26
497 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 14413 0 02-08
496 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 14403 0 10-24
495 론이쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 14395 0 06-07
494 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 14394 0 06-11
493 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 14385 0 06-20
492 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 14368 0 11-08
491 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 14362 0 11-28
490 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 14361 0 10-23
489 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 14349 0 05-11
488 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 14336 0 05-26
487 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 14335 0 04-18
486 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 14328 0 06-28
485 포인트쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 14327 0 02-04
484 오타짱쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 14324 0 03-22
483 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 14316 0 10-17
열람중 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 14315 0 01-09
481 연후아빠쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 14314 0 03-26
480 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 14314 0 04-18
479 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 14311 0 09-19
478 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 14308 0 12-27
477 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 14301 0 05-18
476 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 14282 0 11-17

검색

회원로그인

회원가입

사이트 정보

株式会社YHPLUS / 대표 : ZERO
〒140-0011 東京都品川区東大井2-5-9-203
050-5539-7787
오픈카카오톡 (YHPLUS) :
https://open.kakao.com/o/slfDj15d

접속자집계

오늘
4,828
어제
4,531
최대
7,259
전체
1,230,857
Copyright (c) 株式会社YHPLUS. All rights reserved.