array getimagesize ( string $filename [, array &$imageinfo ] )
(PHP 4, PHP 5)

이 함수는 이미지의 크기나 Type에 대한 정보를 반환하는 함수로 7개의 엘레먼트를 배열로 제공합니다. 이 함수는 자주 사용되기도 하지만 알아두면 아주 유용합니다.

<?php
 $size = getimagesize("test.jpg");

 print_r($size);

 // 출력
 Array
 (
    [0] => 68
    [1] => 100
    [2] => 2
    [3] => width="68" height="100"
    [bits] => 8
    [channels] => 3
    [mime] => image/jpeg
 )
 ?>

배열에 대한 정보는 다음 표를 참고합니다.

엘레먼트 설 명
[0] Width 값
[1] Height 값
[2] Image Type Flag, 타입을 정수로 반환
(아래 표 참조)
[3] Width, Height값;
Ex.) # "width='68' height='10'"
[bits] 비트
[channels] channels
[mime] 파일 mime-type;
Ex.) # "image/jpeg"

"test.jpg" 파일 정보를 보면, 8비트인 jpg이미지로 가로 크기 68, 세로 크기가 100임을 알 수 있습니다. 여기서 엘레먼트 [2]는 다음 표의 Type을 참고로 대조에도 사용할 수 있습니다.

반환값 설 명
1 GIF
2 JPG
3 SWF
4 PSD
5 BMP
6 TIFF(orden de bytes intel)
7 TIFF(orden de bytes motorola)
8 JPC
9 JP2
10 JPX
11 JB2
12 SWC
13 IFF
14 WBMP
15 XBM

<?php
 $size = getimagesize("rss.gif");

 // 타입 1은 gif파일이므로 다음 조건문은 참입니다.
 if($size[2] == 1){
  echo "gif 파일입니다.";
 }


 $size = getimagesize("test.bmp");

 // 타입 5는 bmp파일이므로 다음 조건문은 참입니다.
 if($size[2] == 5){
  echo "bmp 파일입니다.";
 }


 $size = getimagesize("myphoto.psd");

 // 타입 4는 psd파일이므로 다음 조건문은 거짓입니다.
 if($size[2] == 5){
  echo "bmp 파일입니다.";
 }
 ?>

<?php
 $imgurl = "test.bmp";
 list($width, $height, $type, $attr) = getimagesize($imgurl);

 // $attr는 엘레먼트 [3]입니다.
 echo "<img src='${imgurl}' ${attr} alt='getimagesize() example' />";

 // 결과: <img src='test.bmp' width="182" height="220"
           alt='getimagesize() example' />
 ?>

다음은 URL를 이용한 예제입니다.

<?php
 $size = getimagesize("http://kr2.php.net/images/php.gif");
 print_r($size);

 /*
 결과: 
 Array
 (
    [0] => 120
    [1] => 67
    [2] => 1
    [3] => width="120" height="67"
    [bits] => 7
    [channels] => 3
    [mime] => image/gif
 )
 */


 $size = getimagesize("http://main2.nateimg.co.kr/img/cms/
                                  content_pool/2011/05/L_02(3).jpg"); 

 print_r($size);

 /*
 결과:
 Array
 (
    [0] => 105
    [1] => 70
    [2] => 2
    [3] => width="105" height="70"
    [bits] => 8
    [channels] => 3
    [mime] => image/jpeg
 )
 */
 ?> 

다음 fopen함수를 이용한 이미지 출력입니다.

<?php
 $file = "test.bmp";
 $size = getimagesize($file);
 $fp = fopen($file, "rb");
 if ($size && $fp) {
    header("Content-type: ".$size['mime']);
    fpassthru($fp);
    exit;
 }
 ?>

0 댓글