function divide_image($imgPath, $x, $y) {
$path = pathinfo($imgPath);
$imgDir = $path['dirname'];
$imgExt = '.'.$path['extension'];
$imgName = $path['filename'];
if (!file_exists($imgDir.'/'.$imgName.'_C0'.$imgExt)) {
$CI =& get_instance();
$CI->load->library('image_lib');
$size = getimagesize($imgPath);
$cw = $size[0];
$ch = $size[1] / $y;
$pw = $cw / $x;
$ph = $ch;
$p = 1;
for ($i=0; $i<$y; $i++) {
$imgCrop = $imgDir.'/'.$imgName.'_C'.$i.$imgExt;
$config = array(
'source_image' => $imgPath,
'new_image' => $imgCrop,
'create_thumb' => true,
'thumb_marker' => false,
'maintain_ratio' => false,
'width' => $cw,
'height' => $ch,
'x_axis' => 0,
'y_axis' => $i * $ch
);
$CI->image_lib->initialize($config);
if ($CI->image_lib->crop()) {
for ($j=0; $j<$x; $j++) {
$config = array(
'source_image' => $imgCrop,
'new_image' => $imgDir.'/'.$imgName.'_P'.$p.$imgExt,
'create_thumb' => true,
'thumb_marker' => false,
'maintain_ratio' => false,
'width' => $pw,
'height' => $ph,
'x_axis' => $j * $pw,
'y_axis' => 0
);
$CI->image_lib->initialize($config);
$CI->image_lib->crop();
$p++;
}
}
}
}
$imgDir = str_replace(DATA_PATH, DATA_DIR, $imgDir);
$pcount = $x * $y;
$plist = array();
for ($i=1; $i<=$pcount; $i++) {
$plist[$i]->img = $imgDir.'/'.$imgName.'_P'.$i.$imgExt;
$plist[$i]->key = $i;
}
return $plist;
}
divide_image(DATA_PATH.'/image.jpg', 3, 6);
- 상수소개
DATA_PATH = $_SERVER['DOCUMENT_ROOT'];
DATA_DIR = '/';
- 예제
1) http://www.tested.co.kr 메인페이지 배경
2) http://www.tested.co.kr/exper/puzzle
|