[함수] XenoImage class makeCropThumbnail
<?php
// +--------------------------------------------------------+
// | PHP version 5.x |
// +--------------------------------------------------------+
// | License : new BSD |
// +--------------------------------------------------------+
// | Copyright : Song Hyo-Jin <shj at xenosi.de> |
// +--------------------------------------------------------+
//
// $Id: XenoImage.class.php, 2012. 1. 22. crucify Exp
class XenoImage
{
public $img, $width, $height;
public function __construct($filename = false) {
if($filename) $this->loadImage($filename);
}
public function setDir($filename) {
if(is_dir($filename)) return;
if(substr($filename, -1) == '/') {
$dir = $filename;
} else {
$dir = dirname($filename);
}
if(!file_exists($dir)) mkdir($dir, 0755, true);
}
public function loadImage($filename) {
if(!is_resource($filename)) {
$mime = getimagesize($filename);
switch($mime['mime']) {
case 'image/jpeg':
$img = imagecreatefromjpeg($filename);
break;
case 'image/png':
$img = imagecreatefrompng($filename);
break;
case 'image/gif':
$img = imagecreatefromgif($filename);
break;
default:
throw new Exception('이미지가 아닙니다.'.$mime['mime']);
}
} else $img = $filename;
$this->width = imagesx($img);
$this->height = imagesy($img);
if(!imageistruecolor($img)) {
$img2 = imagecreatetruecolor($this->width, $this->height);
imagecopy($img2, $img, 0, 0, 0, 0, $this->width, $this->height);
imagedestroy($img);
$img = $img2;
}
$this->img = $img;
}
public function __destruct() {
if($this->img != null) imagedestroy($this->img);
}
public function makeThumbnail($width, $height, $outfile = false) {
list($w, $h, $resizeflag) = self::calcScale($this->width, $this->height, $width, $height, true);
if(!$resizeflag) return $this->saveImage(null, $outfile);
return $this->saveThumbnail(0, 0, 0, 0, $w, $h, $this->width, $this->height, $outfile);
}
public function makeCropThumbnail($width, $height, $outfile = false) {
list($w, $h, $px, $py, $sw, $sh, $resizeflag) = self::calcScaleCrop($this->width, $this->height, $width, $height);
if(!$resizeflag) return $this->saveImage(null, $outfile);
return $this->saveThumbnail(0, 0, $px, $py, $w, $h, $sw, $sh, $outfile);
}
private function saveThumbnail($x1, $y1, $x2, $y2, $w1, $h1, $w2, $h2, $outfile) {
$img = imagecreatetruecolor($w1, $h1);
imagecopyresampled($img, $this->img, $x1, $y1, $x2, $y2, $w1, $h1, $w2, $h2);
return $this->saveImage($img, $outfile);
}
private function saveImage($img, $outfile = false) {
$im = ($img == null) ? $this->img : $img;
if($outfile == false) return $im;
$this->setDir($outfile);
switch(strtolower(preg_replace('~^.*\.~', '', $outfile))) {
case 'jpg':
case 'jpeg':
imagejpeg($im, $outfile, 90);
break;
case 'png':
imagepng($im, $outfile);
break;
default:
imagegif($im, $outfile);
break;
}
if($img != null) {
imagedestroy($img);
$img = null;
}
return true;
}
public static function calcScale($w, $h, $cw, $ch, $calczero = false) {
if($cw == 0) {
if(!$calczero) {
if($h <= $ch) return array(0, $h, false);
return array(0, $ch, true);
}
$cw = floor($w * $ch / $h);
return array($cw, $ch, true);
}
if($ch == 0) {
if(!$calczero) {
if($w < $cw) return array($w, 0, false);
return array($cw, 0, true);
}
$ch = floor($h * $cw / $w);
return array($cw, $ch, true);
}
list($nw, $nh) = array($w, $h);
if($w >= $cw || $h >= $ch) {
$rw = ($w > 0) ? $cw / $w : 0;
$rh = ($h > 0) ? $ch / $h : 0;
$r = ($rw > $rh) ? $rh : $rw;
$nw = floor($w * $r);
$nh = floor($h * $r);
$resizeflag = true;
} else $resizeflag = false;
return array($nw, $nh, $resizeflag);
}
public static function calcScaleCrop($w, $h, $cw, $ch) {
if($cw == 0 || $ch == 0) {
return array($cw, $ch, 0, 0, $w, $h, false);
}
list($nw, $nh, $px, $py, $sw, $sh) = array($w, $h, 0, 0, $w, $h);
if($w >= $cw || $h >= $ch) {
$rw = $cw / $w;
$rh = $ch / $h;
$r = ($rw > $rh) ? $rw : $rh;
$nw = $w * $r;
$nh = $h * $r;
$ppx = $nw - $cw;
$ppy = $nh - $ch;
$px = ($ppx * $w / $nw) >> 1;
$py = ($ppy * $h / $nh) >> 1;
$sw = floor($cw / $r);
$sh = floor($ch / $r);
$resizeflag = true;
} else $resizeflag = false;
return array($cw, $ch, $px, $py, $sw, $sh, $resizeflag);
}
}
$img = new XenoImage($_FILES['image']['tmp_name']);
$img->makeCropThumbnail(200, 150, '/home/user/files/'.floor($seq / 1000).'/'.$seq.'croppped.jpg');
$img->makeThumbnail(200, 0, '/home/user/files/'.floor($seq / 1000).'/'.$seq.'resized.jpg');
우승길드 됐길래 전에 올렸던거 옮김.
송효진님꺼
'개발-PHP' 카테고리의 다른 글
[함수] php 금액을 한글로 바꾸는 소스 (0) | 2016.03.30 |
---|---|
[함수] [PHP] 년별 월별 일별 시간 범위 구하기 (0) | 2016.03.30 |
[함수] 왕 초보가 올리는 파일 업로드 방법 (0) | 2016.03.30 |
[함수] array_iconv - 1~n차원 배열의 key, value 문자 인코딩 변환 함수 (0) | 2016.03.30 |
[함수] 제로보드 댓글 스팸 동시 전체삭제 (0) | 2016.03.30 |