[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건 26 페이지
  • RSS
팁앤테크 목록
번호 제목 글쓴이 조회 추천 날짜
200 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18208 0 01-05
199 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18241 0 09-21
198 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18306 0 10-07
197 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18328 0 02-22
196 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18339 0 10-28
195 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18362 0 04-04
194 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18389 0 01-20
193 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18395 0 05-30
192 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18401 0 09-29
191 朴城忍쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18422 0 02-27
190 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18436 0 06-01
189 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18441 0 04-25
188 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18471 0 01-12
187 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18477 0 01-24
186 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18496 0 03-14
185 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18546 0 06-12
184 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18595 0 10-06
183 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18605 0 03-02
182 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18621 0 01-05
181 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18644 0 10-08
180 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18714 0 11-01
179 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18724 0 04-17
178 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18744 0 06-23
177 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18790 0 11-29
176 제로쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18798 0 10-22

검색

회원로그인

회원가입

사이트 정보

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

접속자집계

오늘
7,380
어제
8,371
최대
8,371
전체
1,632,936
Copyright (c) 株式会社YHPLUS. All rights reserved.