메뉴 건너뛰기

프로그램언어

조회 수 262 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
<?
class Image {
    
    var $file;
    var $image_width;
    var $image_height;
    var $width;
    var $height;
    var $ext;
    var $types = array('','gif','jpeg','png','swf');
    var $quality = 80;
    var $top = 0;
    var $left = 0;
    var $crop = false;
    var $type;
    
    function Image($name='') {
        $this->file = $name;
        $info = getimagesize($name);
        $this->image_width = $info[0];
        $this->image_height = $info[1];
        $this->type = $this->types[$info[2]];
        $info = pathinfo($name);
        $this->dir = $info['dirname'];
        $this->name = str_replace('.'.$info['extension'], '', $info['basename']);
        $this->ext = $info['extension'];
    }
    
    function dir($dir='') {
        if(!$dir) return $this->dir;
        $this->dir = $dir;
    }
    
    function name($name='') {
        if(!$name) return $this->name;
        $this->name = $name;
    }
    
    function width($width='') {
        $this->width = $width;
    }
    
    function height($height='') {
        $this->height = $height;
    }
    
    function resize($percentage=50) {
        if($this->crop) {
            $this->crop = false;
            $this->width = round($this->width*($percentage/100));
            $this->height = round($this->height*($percentage/100));
            $this->image_width = round($this->width/($percentage/100));
            $this->image_height = round($this->height/($percentage/100));
        } else {
            $this->width = round($this->image_width*($percentage/100));
            $this->height = round($this->image_height*($percentage/100));
        }
        
    }
    
    function crop($top=0, $left=0) {
        $this->crop = true;
        $this->top = $top;
        $this->left = $left;
    }
    
    function quality($quality=80) {
        $this->quality = $quality;
    }
    
    function show() {
        $this->save(true);
    }
    
    function save($show=false) {
 
        if($show) @header('Content-Type: image/'.$this->type);
        
        if(!$this->width && !$this->height) {
            $this->width = $this->image_width;
            $this->height = $this->image_height;
        } elseif (is_numeric($this->width) && empty($this->height)) {
            $this->height = round($this->width/($this->image_width/$this->image_height));
        } elseif (is_numeric($this->height) && empty($this->width)) {
            $this->width = round($this->height/($this->image_height/$this->image_width));
        } else {
            if($this->width<=$this->height) {
                $height = round($this->width/($this->image_width/$this->image_height));
                if($height!=$this->height) {
                    $percentage = ($this->image_height*100)/$height;
                    $this->image_height = round($this->height*($percentage/100));
                }
            } else {
                $width = round($this->height/($this->image_height/$this->image_width));
                if($width!=$this->width) {
                    $percentage = ($this->image_width*100)/$width;
                    $this->image_width = round($this->width*($percentage/100));
                }
            }
        }
        
        if($this->crop) {
            $this->image_width = $this->width;
            $this->image_height = $this->height;
        }
 
        if($this->type=='jpeg') $image = imagecreatefromjpeg($this->file);
        if($this->type=='png') $image = imagecreatefrompng($this->file);
        if($this->type=='gif') $image = imagecreatefromgif($this->file);
        
        $new_image = imagecreatetruecolor($this->width, $this->height);
        imagecopyresampled($new_image, $image, 0, 0, $this->top, $this->left, $this->width, $this->height, $this->image_width, $this->image_height);
        
        $name = $show ? null: $this->dir.DIRECTORY_SEPARATOR.$this->name.'.'.$this->ext;
    
        if($this->type=='jpeg') imagejpeg($new_image, $name, $this->quality);
        if($this->type=='png') imagepng($new_image, $name);
        if($this->type=='gif') imagegif($new_image, $name);
 
        imagedestroy($image); 
        imagedestroy($new_image);
        
    }
    
}
  
 
/*  사용방법
$re_image = new Image(이미지 파일명);
$re_image -> width(200);
$re_image -> height(300);
$re_image -> save();
*/
?>

 


List of Articles
번호 제목 날짜 조회 수
220 JSON and JavaScript usage 2014.03.26 19066
219 자릿수만큼 앞에 0 붙이기 2017.03.07 19040
218 링크를 걸때 http 처리방법 2016.12.23 19021
217 디렉토리 안의 파일의 내용들을 읽는 예 2016.12.23 19016
216 정규 표현식 검색과 치환 (preg_replace) 2016.12.23 19012
215 mysql_result — 결과 데이터를 반환 2016.12.23 18969
214 문자열 뒤집기 (strrev) 2016.12.23 18952
213 도메인 앞에 자동으로 WWW를 붙이는 방법 2017.03.07 18921
212 문자열 찾기 (strstr) 2016.12.23 18911
211 두 날짜 사이의 차이 구하기 2017.03.07 18880
210 문자열 추출하기 (substr) 2016.12.23 18869
209 로그인페이지에서 온 경우/로그인한 페이지로 이동 2016.12.23 18851
208 문자열 치환 (str_replace) 2016.12.23 18815
207 mysql_affected_rows — 최근 MySQL 작업으로 변경된 행 개수를 얻음 2016.12.23 18813
206 mysql_insert_id 2016.12.23 18753
205 네이버 지도 API 연동 PHP 소스 2017.03.06 18650
204 mysql_real_escape_string 이진 데이터를 입력할 경우 이 함수를 사용해야 함 2016.12.23 18645
203 PHP 날짜/시간 정리 2017.03.07 18638
202 오류 메시지 출력(alert) 및 페이지 이동(refresh) 관련 2017.03.06 18574
201 잡다한 php 2017.03.06 18505
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 11 ... 17 Next
/ 17

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved