ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • php에서 이미지 사이즈 줄이기
    프로그래밍/PHP 2021. 6. 12. 19:01
    반응형

     

     

     

     

     

     

     

     

    이미지 용량 리사이즈를 하기 위해서는 아래와 같이 imagecreatefrompng 등의 함수를 사용해서 

    특정 경로에 있는 이미지 파일을 이미지 객체화를 시켜야한다. 

     

    function getImageResource($path)
    {
            // 업로드된 이미지파일 정보를 가져옵니다
            $file = getimagesize($path);
            if ($file['mime'] == 'image/png')
                    $image = imagecreatefrompng($path);
            else if ($file['mime'] == 'image/gif')
                    $image = imagecreatefromgif($path);
            else
                    $image = imagecreatefromjpeg($path);
    
            return $image;
    
    }
    

     

    여기서 문제는 이 함수를 사용하면서 원래의 회전방향 정보(Exif) 가 날아가면서

    원래 세로 방향으로 되었던 이미지들이 가로 방향으로 나타나는 부작용이 발생하였다. 

    그리고 gif 파일일 경우 imagecreatefromgif로 이미지 객체를 만들었는데도

    압축된 이미지가 더이상 앱에서 움직이지 않는 현상이 발견되서 gif 파일은 압축 대상에서 제외하였다. 

     

    exif란 교환 이미지 파일 형식(Exif; Exchangable Image File Format)의 약자로

    디지털 카메라에서 시용되는 이미지 파일 포맷이다. 

    이 데이터는 사진에 대한 정보를 포함하는 메타데이터를 저장하고 있으며

    아래 지원 포맷에 이미지나 소리에 대한 정보를 추가로 저장하는 것이 가능하다. 

    PNG는 원래 EXIF에 대한 지원이 되지 않았으나, 2017년 7월 발표된 PNG 1.2 (ver 1.5) 이후로 지원이 추가되었다. 

     

    지원 포맷: JPEG, TIFF 6.0, RIFF, WAW, PNG 1.2 (2017년 7월 발표된 버전) 

    미지원 포맷: JPEG 2000, PNG, GIF

    function rotateImage($imageObject, $orientation)
    {
            switch ($orientation) {
                    case 2:
                            imageflip($imageObject, IMG_FLIP_HORIZONTAL);
                            break;
                    case 3:
                            $imageObject = imagerotate($imageObject, 180, 0);
                            break;
                    case 4:
                            imageflip($imageObject, IMG_FLIP_VERTICAL);
                            break;
                    case 5:
                            $imageObject = imagerotate($imageObject, -90, 0);
                            imageflip($imageObject, IMG_FLIP_HORIZONTAL);
                            break;
                    case 6:
                            $imageObject = imagerotate($imageObject, -90, 0);
                            break;
                    case 7:
                            $imageObject = imagerotate($imageObject, 90, 0);
                            imageflip($imageObject, IMG_FLIP_HORIZONTAL);
                            break;
                    case 8:
                            $imageObject = imagerotate($imageObject, 90, 0);
                            break;
            }
            return $imageObject;
    }
    

     

     

     

    참고: 

    erulabo.com/40

    www.php.net/manual/en/function.imagecreatefromjpeg.php

    stackoverflow.com/questions/45963286/php-imagecreatefromjpeg-while-keeping-orientation

    feel5ny.github.io/2018/08/06/JS_13/

     

    728x90
    반응형
Designed by Tistory.