179 lines
5.7 KiB
PHP
179 lines
5.7 KiB
PHP
|
|
<?
|
||
|
|
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 = 70;
|
||
|
|
var $top = 0;
|
||
|
|
var $left = 0;
|
||
|
|
var $crop = false;
|
||
|
|
var $type;
|
||
|
|
var $rotation_angle;
|
||
|
|
var $dir;
|
||
|
|
var $name;
|
||
|
|
|
||
|
|
function __construct($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=70) {
|
||
|
|
$this->quality = $quality;
|
||
|
|
}
|
||
|
|
|
||
|
|
function rotate($angle) {
|
||
|
|
$this->rotation_angle = $angle;
|
||
|
|
}
|
||
|
|
|
||
|
|
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);
|
||
|
|
|
||
|
|
// Apply rotation if specified
|
||
|
|
if(isset($this->rotation_angle) && $this->rotation_angle != 0) {
|
||
|
|
$new_image = imagerotate($new_image, $this->rotation_angle, 0);
|
||
|
|
}
|
||
|
|
|
||
|
|
$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);
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
// // 파일 압축 메소드 수정
|
||
|
|
function compress_image($source, $destination, $quality) {
|
||
|
|
$info = getimagesize($source);
|
||
|
|
|
||
|
|
if ($info['mime'] == 'image/jpeg') {
|
||
|
|
$image = imagecreatefromjpeg($source);
|
||
|
|
} elseif ($info['mime'] == 'image/gif') {
|
||
|
|
$image = imagecreatefromgif($source);
|
||
|
|
} elseif ($info['mime'] == 'image/png') {
|
||
|
|
$image = imagecreatefrompng($source);
|
||
|
|
} elseif ($info['mime'] == 'image/x-ms-bmp') {
|
||
|
|
$image = imagecreatefrombmp($source);
|
||
|
|
} else {
|
||
|
|
return false; // 지원하지 않는 파일 형식
|
||
|
|
}
|
||
|
|
|
||
|
|
// 압축된 파일 저장
|
||
|
|
imagejpeg($image, $destination, $quality);
|
||
|
|
|
||
|
|
// 크기 조정 (최대 800px)
|
||
|
|
list($width, $height) = getimagesize($destination);
|
||
|
|
if ($width > 800 || $height > 800) {
|
||
|
|
$rate = $width / $height;
|
||
|
|
|
||
|
|
if ($width > $height) {
|
||
|
|
$new_width = 800;
|
||
|
|
$new_height = 800 / $rate;
|
||
|
|
} else {
|
||
|
|
$new_width = 800 * $rate;
|
||
|
|
$new_height = 800;
|
||
|
|
}
|
||
|
|
|
||
|
|
// `Image` 클래스를 사용해 크기 조정
|
||
|
|
$imageObj = new Image($destination);
|
||
|
|
$imageObj->width($new_width);
|
||
|
|
$imageObj->height($new_height);
|
||
|
|
$imageObj->save(false); // 파일을 다시 저장
|
||
|
|
}
|
||
|
|
|
||
|
|
return $destination; // 최종 파일 경로 반환
|
||
|
|
}
|
||
|
|
?>
|