function thumbnail($filename, $source_path, $target_path, $thumb_width, $thumb_height, $is_create, $is_crop = false, $crop_mode = 'center', $is_sharpen = false, $um_value = '80/0.5/3') { global $g5; if (!$thumb_width && !$thumb_height) { return; } $source_file = "{$source_path}/{$filename}"; if (!is_file($source_file)) { // 원본 파일이 없다면 return; } $size = @getimagesize($source_file); if ($size[2] < 1 || $size[2] > 3) { // gif, jpg, png 에 대해서만 적용 return; } if (!is_dir($target_path)) { @mkdir($target_path, G5_DIR_PERMISSION); @chmod($target_path, G5_DIR_PERMISSION); } // 디렉토리가 존재하지 않거나 쓰기 권한이 없으면 썸네일 생성하지 않음 if (!(is_dir($target_path) && is_writable($target_path))) { return ''; } // Animated GIF는 썸네일 생성하지 않음 if ($size[2] == 1) { if (is_animated_gif($source_file)) { return basename($source_file); } } $ext = array(1 => 'gif', 2 => 'jpg', 3 => 'png'); $thumb_filename = preg_replace("/\\.[^\\.]+\$/i", "", $filename); // 확장자제거 $thumb_file = "{$target_path}/thumb-{$thumb_filename}_{$thumb_width}x{$thumb_height}." . $ext[$size[2]]; $thumb_time = @filemtime($thumb_file); $source_time = @filemtime($source_file); if (file_exists($thumb_file)) { if ($is_create == false && $source_time < $thumb_time) { return basename($thumb_file); } } // 원본파일의 GD 이미지 생성 $src = null; $degree = 0; if ($size[2] == 1) { $src = imagecreatefromgif($source_file); $src_transparency = imagecolortransparent($src); } else { if ($size[2] == 2) { $src = imagecreatefromjpeg($source_file); if (function_exists('exif_read_data')) { // exif 정보를 기준으로 회전각도 구함 $exif = @exif_read_data($source_file); if (!empty($exif['Orientation'])) { switch ($exif['Orientation']) { case 8: $degree = 90; break; case 3: $degree = 180; break; case 6: $degree = -90; break; } // 회전각도 있으면 이미지 회전 if ($degree) { $src = imagerotate($src, $degree, 0); // 세로사진의 경우 가로, 세로 값 바꿈 if ($degree == 90 || $degree == -90) { $tmp = $size; $size[0] = $tmp[1]; $size[1] = $tmp[0]; } } } } } else { if ($size[2] == 3) { $src = imagecreatefrompng($source_file); imagealphablending($src, true); } else { return; } } } if (!$src) { return; } $is_large = true; // width, height 설정 if ($thumb_width) { if (!$thumb_height) { $thumb_height = round($thumb_width * $size[1] / $size[0]); } else { if ($size[0] < $thumb_width || $size[1] < $thumb_height) { $is_large = false; } } } else { if ($thumb_height) { $thumb_width = round($thumb_height * $size[0] / $size[1]); } } $dst_x = 0; $dst_y = 0; $src_x = 0; $src_y = 0; $dst_w = $thumb_width; $dst_h = $thumb_height; $src_w = $size[0]; $src_h = $size[1]; $ratio = $dst_h / $dst_w; if ($is_large) { // 크롭처리 if ($is_crop) { switch ($crop_mode) { case 'center': if ($size[1] / $size[0] >= $ratio) { $src_h = round($src_w * $ratio); $src_y = round(($size[1] - $src_h) / 2); } else { $src_w = round($size[1] / $ratio); $src_x = round(($size[0] - $src_w) / 2); } break; default: if ($size[1] / $size[0] >= $ratio) { $src_h = round($src_w * $ratio); } else { $src_w = round($size[1] / $ratio); } break; } } $dst = imagecreatetruecolor($dst_w, $dst_h); if ($size[2] == 3) { imagealphablending($dst, false); imagesavealpha($dst, true); } else { if ($size[2] == 1) { $palletsize = imagecolorstotal($src); if ($src_transparency >= 0 && $src_transparency < $palletsize) { $transparent_color = imagecolorsforindex($src, $src_transparency); $current_transparent = imagecolorallocate($dst, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); imagefill($dst, 0, 0, $current_transparent); imagecolortransparent($dst, $current_transparent); } } } } else { $dst = imagecreatetruecolor($dst_w, $dst_h); $bgcolor = imagecolorallocate($dst, 255, 255, 255); // 배경색 if ($src_w < $dst_w) { if ($src_h >= $dst_h) { $dst_x = round(($dst_w - $src_w) / 2); $src_h = $dst_h; } else { $dst_x = round(($dst_w - $src_w) / 2); $dst_y = round(($dst_h - $src_h) / 2); $dst_w = $src_w; $dst_h = $src_h; } } else { if ($src_h < $dst_h) { $dst_y = round(($dst_h - $src_h) / 2); $dst_h = $src_h; $src_w = $dst_w; } } if ($size[2] == 3) { $bgcolor = imagecolorallocatealpha($dst, 0, 0, 0, 127); imagefill($dst, 0, 0, $bgcolor); imagealphablending($dst, false); imagesavealpha($dst, true); } else { if ($size[2] == 1) { $palletsize = imagecolorstotal($src); if ($src_transparency >= 0 && $src_transparency < $palletsize) { $transparent_color = imagecolorsforindex($src, $src_transparency); $current_transparent = imagecolorallocate($dst, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); imagefill($dst, 0, 0, $current_transparent); imagecolortransparent($dst, $current_transparent); } else { imagefill($dst, 0, 0, $bgcolor); } } else { imagefill($dst, 0, 0, $bgcolor); } } } imagecopyresampled($dst, $src, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); // sharpen 적용 if ($is_sharpen && $is_large) { $val = explode('/', $um_value); UnsharpMask($dst, $val[0], $val[1], $val[2]); } if ($size[2] == 1) { imagegif($dst, $thumb_file); } else { if ($size[2] == 3) { if (!defined('G5_THUMB_PNG_COMPRESS')) { $png_compress = 5; } else { $png_compress = G5_THUMB_PNG_COMPRESS; } imagepng($dst, $thumb_file, $png_compress); } else { if (!defined('G5_THUMB_JPG_QUALITY')) { $jpg_quality = 90; } else { $jpg_quality = G5_THUMB_JPG_QUALITY; } imagejpeg($dst, $thumb_file, $jpg_quality); } } chmod($thumb_file, G5_FILE_PERMISSION); // 추후 삭제를 위하여 파일모드 변경 imagedestroy($src); imagedestroy($dst); return basename($thumb_file); }
public function upload() { if ($GLOBALS['user_info']['id'] == 0) { $data['status'] = 0; //未登录 $data['msg'] = "请先登录"; ajax_return($data); } //上传处理 //创建avatar临时目录 if (!is_dir(APP_ROOT_PATH . "public/avatar")) { @mkdir(APP_ROOT_PATH . "public/avatar"); @chmod(APP_ROOT_PATH . "public/avatar", 0777); } if (!is_dir(APP_ROOT_PATH . "public/avatar/temp")) { @mkdir(APP_ROOT_PATH . "public/avatar/temp"); @chmod(APP_ROOT_PATH . "public/avatar/temp", 0777); } $upd_id = $id = intval($_REQUEST['uid']); if (is_animated_gif($_FILES['avatar_file']['tmp_name'])) { $rs = save_image_upload($_FILES, "avatar_file", "avatar/temp", $whs = array()); $im = get_spec_gif_anmation($rs['avatar_file']['path'], 48, 48); $file_name = APP_ROOT_PATH . "public/avatar/temp/" . md5(NOW_TIME . $upd_id) . "_small.jpg"; file_put_contents($file_name, $im); $img_result['avatar_file']['thumb']['small']['path'] = $file_name; $im = get_spec_gif_anmation($rs['avatar_file']['path'], 120, 120); $file_name = APP_ROOT_PATH . "public/avatar/temp/" . md5(NOW_TIME . $upd_id) . "_middle.jpg"; file_put_contents($file_name, $im); $img_result['avatar_file']['thumb']['middle']['path'] = $file_name; $im = get_spec_gif_anmation($rs['avatar_file']['path'], 200, 200); $file_name = APP_ROOT_PATH . "public/avatar/temp/" . md5(NOW_TIME . $upd_id) . "_big.jpg"; file_put_contents($file_name, $im); $img_result['avatar_file']['thumb']['big']['path'] = $file_name; } else { $img_result = save_image_upload($_FILES, "avatar_file", "avatar/temp", $whs = array('small' => array(48, 48, 1, 0), 'middle' => array(120, 120, 1, 0), 'big' => array(200, 200, 1, 0))); } //开始移动图片到相应位置 $uid = sprintf("%09d", $id); $dir1 = substr($uid, 0, 3); $dir2 = substr($uid, 3, 2); $dir3 = substr($uid, 5, 2); $path = $dir1 . '/' . $dir2 . '/' . $dir3; //创建相应的目录 if (!is_dir(APP_ROOT_PATH . "public/avatar/" . $dir1)) { @mkdir(APP_ROOT_PATH . "public/avatar/" . $dir1); @chmod(APP_ROOT_PATH . "public/avatar/" . $dir1, 0777); } if (!is_dir(APP_ROOT_PATH . "public/avatar/" . $dir1 . '/' . $dir2)) { @mkdir(APP_ROOT_PATH . "public/avatar/" . $dir1 . '/' . $dir2); @chmod(APP_ROOT_PATH . "public/avatar/" . $dir1 . '/' . $dir2, 0777); } if (!is_dir(APP_ROOT_PATH . "public/avatar/" . $dir1 . '/' . $dir2 . '/' . $dir3)) { @mkdir(APP_ROOT_PATH . "public/avatar/" . $dir1 . '/' . $dir2 . '/' . $dir3); @chmod(APP_ROOT_PATH . "public/avatar/" . $dir1 . '/' . $dir2 . '/' . $dir3, 0777); } $id = str_pad($id, 2, "0", STR_PAD_LEFT); $id = substr($id, -2); $avatar_file_big = APP_ROOT_PATH . "public/avatar/" . $path . "/" . $id . "virtual_avatar_big.jpg"; $avatar_file_middle = APP_ROOT_PATH . "public/avatar/" . $path . "/" . $id . "virtual_avatar_middle.jpg"; $avatar_file_small = APP_ROOT_PATH . "public/avatar/" . $path . "/" . $id . "virtual_avatar_small.jpg"; @file_put_contents($avatar_file_big, file_get_contents($img_result['avatar_file']['thumb']['big']['path'])); @file_put_contents($avatar_file_middle, file_get_contents($img_result['avatar_file']['thumb']['middle']['path'])); @file_put_contents($avatar_file_small, file_get_contents($img_result['avatar_file']['thumb']['small']['path'])); @unlink($img_result['avatar_file']['thumb']['big']['path']); @unlink($img_result['avatar_file']['thumb']['middle']['path']); @unlink($img_result['avatar_file']['thumb']['small']['path']); @unlink($img_result['avatar_file']['path']); //上传成功更新用户头像的动态缓存 update_avatar($upd_id); $data['status'] = 1; $data['small_url'] = get_user_avatar($upd_id, "small"); $data['middle_url'] = get_user_avatar($upd_id, "middle"); $data['big_url'] = get_user_avatar($upd_id, "big"); ajax_return($data); }
function apms_thumbnail($url, $thumb_width, $thumb_height, $is_create = false, $is_crop = true, $crop_mode = 'center', $is_sharpen = false, $um_value = '80/0.5/3') { if (!$url) { return; } $thumb = array(); // 이미지 path 구함 $p = @parse_url($url); if (strpos($p['path'], '/' . G5_DATA_DIR . '/') != 0) { $data_path = preg_replace('/^\\/.*\\/' . G5_DATA_DIR . '/', '/' . G5_DATA_DIR, $p['path']); } else { $data_path = $p['path']; } $srcfile = G5_PATH . $data_path; if (is_file($srcfile)) { $size = @getimagesize($srcfile); if (empty($size)) { return; } // jpg 이면 exif 체크 if ($size[2] == 2 && function_exists('exif_read_data')) { $degree = 0; $exif = @exif_read_data($srcfile); if (!empty($exif['Orientation'])) { switch ($exif['Orientation']) { case 8: $degree = 90; break; case 3: $degree = 180; break; case 6: $degree = -90; break; } // 세로사진의 경우 가로, 세로 값 바꿈 if ($degree == 90 || $degree == -90) { $tmp = $size; $size[0] = $tmp[1]; $size[1] = $tmp[0]; } } } // 원본 width가 thumb_width보다 작다면 if ($size[0] <= $thumb_width) { $thumb['src'] = $url; $thumb['height'] = $size[1]; return $thumb; } // Animated GIF 체크 $is_animated = false; if ($size[2] == 1) { $is_animated = is_animated_gif($srcfile); } // 이미지 높이 $img_height = round($thumb_width * $size[1] / $size[0]); $filename = basename($srcfile); $filepath = dirname($srcfile); // 썸네일 생성 if (!$is_animated) { $thumb_file = thumbnail($filename, $filepath, $filepath, $thumb_width, $thumb_height, $is_create, $is_crop, $crop_mode, $is_sharpen, $um_value); } else { $thumb_file = $filename; } if (!$thumb_file) { $thumb['src'] = $url; $thumb['height'] = $size[1]; return $thumb; } $url = G5_URL . str_replace($filename, $thumb_file, $data_path); } $thumb['src'] = $url; $thumb['height'] = $img_height; return $thumb; }
public function water($source, $water, $alpha = 80, $position = "0") { //检查文件是否存在 if (!file_exists($source) || !file_exists($water)) { return false; } //图片信息 $sInfo = es_imagecls::getImageInfo($source); $wInfo = es_imagecls::getImageInfo($water); //如果图片小于水印图片,不生成图片 if ($sInfo["0"] < $wInfo["0"] || $sInfo['1'] < $wInfo['1']) { return false; } if (is_animated_gif($source)) { require_once APP_ROOT_PATH . "system/utils/gif_encoder.php"; require_once APP_ROOT_PATH . "system/utils/gif_reader.php"; $gif = new GIFReader(); $gif->load($source); foreach ($gif->IMGS['frames'] as $k => $img) { $im = imagecreatefromstring($gif->getgif($k)); //为im加水印 $sImage = $im; $wCreateFun = "imagecreatefrom" . $wInfo['type']; if (!function_exists($wCreateFun)) { $wCreateFun = 'imagecreatefromjpeg'; } $wImage = $wCreateFun($water); //设定图像的混色模式 imagealphablending($wImage, true); switch (intval($position)) { case 0: break; //左上 //左上 case 1: $posY = 0; $posX = 0; //生成混合图像 imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0], $wInfo[1], $alpha); break; //右上 //右上 case 2: $posY = 0; $posX = $sInfo[0] - $wInfo[0]; //生成混合图像 imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0], $wInfo[1], $alpha); break; //左下 //左下 case 3: $posY = $sInfo[1] - $wInfo[1]; $posX = 0; //生成混合图像 imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0], $wInfo[1], $alpha); break; //右下 //右下 case 4: $posY = $sInfo[1] - $wInfo[1]; $posX = $sInfo[0] - $wInfo[0]; //生成混合图像 imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0], $wInfo[1], $alpha); break; //居中 //居中 case 5: $posY = $sInfo[1] / 2 - $wInfo[1] / 2; $posX = $sInfo[0] / 2 - $wInfo[0] / 2; //生成混合图像 imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0], $wInfo[1], $alpha); break; } //end im加水印 ob_start(); imagegif($sImage); $content = ob_get_contents(); ob_end_clean(); $frames[] = $content; $framed[] = $img['frameDelay']; } $gif_maker = new GIFEncoder($frames, $framed, 0, 2, 0, 0, 0, "bin"); $image_rs = $gif_maker->GetAnimation(); //如果没有给出保存文件名,默认为原图像名 @unlink($source); //保存图像 file_put_contents($source, $image_rs); return true; } //建立图像 $sCreateFun = "imagecreatefrom" . $sInfo['type']; if (!function_exists($sCreateFun)) { $sCreateFun = 'imagecreatefromjpeg'; } $sImage = $sCreateFun($source); $wCreateFun = "imagecreatefrom" . $wInfo['type']; if (!function_exists($wCreateFun)) { $wCreateFun = 'imagecreatefromjpeg'; } $wImage = $wCreateFun($water); //设定图像的混色模式 imagealphablending($wImage, true); switch (intval($position)) { case 0: break; //左上 //左上 case 1: $posY = 0; $posX = 0; //生成混合图像 imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0], $wInfo[1], $alpha); break; //右上 //右上 case 2: $posY = 0; $posX = $sInfo[0] - $wInfo[0]; //生成混合图像 imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0], $wInfo[1], $alpha); break; //左下 //左下 case 3: $posY = $sInfo[1] - $wInfo[1]; $posX = 0; //生成混合图像 imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0], $wInfo[1], $alpha); break; //右下 //右下 case 4: $posY = $sInfo[1] - $wInfo[1]; $posX = $sInfo[0] - $wInfo[0]; //生成混合图像 imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0], $wInfo[1], $alpha); break; //居中 //居中 case 5: $posY = $sInfo[1] / 2 - $wInfo[1] / 2; $posX = $sInfo[0] / 2 - $wInfo[0] / 2; //生成混合图像 imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0], $wInfo[1], $alpha); break; } //如果没有给出保存文件名,默认为原图像名 @unlink($source); //保存图像 imagejpeg($sImage, $source, 100); imagedestroy($sImage); }
public function get_thumbnail($contents, $thumb_width = 0) { global $board, $config, $eyoom_board, $exif; if (!$thumb_width) { $thumb_width = $board['bo_image_width']; } // $contents 중 img 태그 추출 $matches = get_editor_image($contents, true); if (empty($matches)) { return $contents; } for ($i = 0; $i < count($matches[1]); $i++) { $img = $matches[1][$i]; preg_match("/src=[\\'\"]?([^>\\'\"]+[^>\\'\"]+)/i", $img, $m); $src = $m[1]; preg_match("/style=[\"\\']?([^\"\\'>]+)/i", $img, $m); $style = $m[1]; preg_match("/width:\\s*(\\d+)px/", $style, $m); $width = $m[1]; preg_match("/height:\\s*(\\d+)px/", $style, $m); $height = $m[1]; preg_match("/alt=[\"\\']?([^\"\\']*)[\"\\']?/", $img, $m); $alt = get_text($m[1]); // 이미지 path 구함 $p = parse_url($src); if (strpos($p['path'], '/' . G5_DATA_DIR . '/') != 0) { $data_path = preg_replace('/^\\/.*\\/' . G5_DATA_DIR . '/', '/' . G5_DATA_DIR, $p['path']); } else { $data_path = $p['path']; } $srcfile = G5_PATH . $data_path; if (is_file($srcfile)) { // EXIF 정보 if ($eyoom_board['bo_use_exif']) { $exif_info = $exif->get_exif_info($srcfile); } $size = @getimagesize($srcfile); if (empty($size)) { continue; } // jpg 이면 exif 체크 if ($size[2] == 2 && function_exists('exif_read_data')) { $degree = 0; $_exif = @exif_read_data($srcfile); if (!empty($_exif['Orientation'])) { switch ($_exif['Orientation']) { case 8: $degree = 90; break; case 3: $degree = 180; break; case 6: $degree = -90; break; } // 세로사진의 경우 가로, 세로 값 바꿈 if ($degree == 90 || $degree == -90) { $tmp = $size; $size[0] = $tmp[1]; $size[1] = $tmp[0]; } } } // 원본 width가 thumb_width보다 작다면 if ($size[0] <= $thumb_width) { continue; } // Animated GIF 체크 $is_animated = false; if ($size[2] == 1) { $is_animated = is_animated_gif($srcfile); } // 썸네일 높이 $thumb_height = round($thumb_width * $size[1] / $size[0]); $filename = basename($srcfile); $filepath = dirname($srcfile); // 썸네일 생성 if (!$is_animated) { $thumb_file = thumbnail($filename, $filepath, $filepath, $thumb_width, $thumb_height, false); } else { $thumb_file = $filename; } if ($thumb_file) { if ($width) { $thumb_tag = '<img src="' . G5_URL . str_replace($filename, $thumb_file, $data_path) . '" alt="' . $alt . '" width="' . $width . '" height="' . $height . '"/>'; } else { $thumb_tag = '<img src="' . G5_URL . str_replace($filename, $thumb_file, $data_path) . '" alt="' . $alt . '"/>'; } // $img_tag에 editor 경로가 있으면 원본보기 링크 추가 $img_tag = $matches[0][$i]; if (strpos($img_tag, G5_DATA_DIR . '/' . G5_EDITOR_DIR) && preg_match("/\\.({$config['cf_image_extension']})\$/i", $filename)) { $imgurl = str_replace(G5_URL, "", $src); $thumb_tag = '<a href="' . G5_BBS_URL . '/view_image.php?fn=' . urlencode($imgurl) . '" target="_blank" class="view_image">' . $thumb_tag . '</a>'; } } else { if ($width) { $thumb_tag = '<img src="' . G5_URL . $data_path . '" alt="' . $alt . '" width="' . $width . '" height="' . $height . '"/>'; } else { $thumb_tag = '<img src="' . G5_URL . $data_path . '" alt="' . $alt . '"/>'; } $img_tag = $matches[0][$i]; } // EXIF 정보 if ($exif_info && $eyoom_board['bo_use_exif']) { $thumb_tag .= $exif_info; } $contents = str_replace($img_tag, $thumb_tag, $contents); } } return $contents; }
// 세로사진의 경우 가로, 세로 값 바꿈 if ($degree == 90 || $degree == -90) { $tmp = $size; $size[0] = $tmp[1]; $size[1] = $tmp[0]; } } } // 원본 width가 리사이즈 보다 작다면 if ($size[0] <= $board['as_resize']) { continue; } // Animated GIF 체크 $is_animated = false; if ($size[2] == 1) { $is_animated = is_animated_gif($srcfile); } if ($is_animated) { continue; } $org = basename($srcfile); $filepath = dirname($srcfile); $thumb = thumbnail($org, $filepath, $filepath, $board['as_resize'], 0, false); if ($thumb && $thumb != $org) { $orgfile = $filepath . '/' . $org; $thumbfile = $filepath . '/' . $thumb; @chmod($orgfile, G5_FILE_PERMISSION); @unlink($orgfile); @copy($thumbfile, $orgfile); @chmod($orgfile, G5_FILE_PERMISSION); @unlink($thumbfile);
function thumbnail($type = '', $filename = '', $thumb_width = 0, $thumb_height = 0, $is_create = false, $is_crop = true, $crop_mode = 'center', $is_sharpen = false, $um_value = '80/0.5/3', $create_animate_thumb = false) { $source_file = 'uploads/'; if ($type) { $source_file .= $type . '/'; } $source_file .= $filename; if (is_file($source_file) === false) { // 원본 파일이 없다면 return; } if (empty($thumb_width) && empty($thumb_height)) { return $source_file; } $size = @getimagesize($source_file); if ($size[2] < 1 or $size[2] > 3) { // gif, jpg, png 에 대해서만 적용 return; } $uploadDir = 'uploads/cache/'; if (is_dir($uploadDir) === false) { @mkdir($uploadDir, 0755); @chmod($uploadDir, 0755); $file = $uploadDir . 'index.php'; $f = @fopen($file, 'w'); @fwrite($f, ''); @fclose($f); @chmod($file, 0644); } if ($type) { $uploadDir .= $type . '/'; if (is_dir($uploadDir) === false) { @mkdir($uploadDir, 0755); @chmod($uploadDir, 0755); $file = $uploadDir . 'index.php'; $f = @fopen($file, 'w'); @fwrite($f, ''); @fclose($f); @chmod($file, 0644); } } $exp = explode('/', $filename); $filepos = count($exp) - 1; for ($k = 0; $k < $filepos; $k++) { $uploadDir .= $exp[$k] . '/'; if (is_dir($uploadDir) === false) { @mkdir($uploadDir, 0755); @chmod($uploadDir, 0755); $file = $uploadDir . 'index.php'; $f = @fopen($file, 'w'); @fwrite($f, ''); @fclose($f); @chmod($file, 0644); } } $realfilename = $exp[$filepos]; $target_path = $uploadDir; // 디렉토리가 존재하지 않거나 쓰기 권한이 없으면 썸네일 생성하지 않음 if (!(is_dir($target_path) && is_writable($target_path))) { return ''; } // Animated GIF는 썸네일 생성하지 않음 if ($size[2] === 1) { if (is_animated_gif($source_file) && $create_animate_thumb === false) { return $source_file; } } $ext = array(1 => 'gif', 2 => 'jpg', 3 => 'png'); $thumb_filename = preg_replace("/\\.[^\\.]+\$/i", '', $realfilename); // 확장자제거 $thumb_file = $target_path . 'thumb-' . $thumb_filename . '_' . $thumb_width . 'x' . $thumb_height . '.' . $ext[$size[2]]; $thumb_time = @filemtime($thumb_file); $source_time = @filemtime($source_file); if (file_exists($thumb_file)) { if ($is_create === false && $source_time < $thumb_time) { return $thumb_file; } } // 원본파일의 GD 이미지 생성 $src = null; $degree = 0; if ($size[2] === 1) { $src = imagecreatefromgif($source_file); $src_transparency = imagecolortransparent($src); } elseif ($size[2] === 2) { $src = imagecreatefromjpeg($source_file); if (function_exists('exif_read_data')) { // exif 정보를 기준으로 회전각도 구함 $exif = @exif_read_data($source_file); if (!empty($exif['Orientation'])) { switch ($exif['Orientation']) { case 8: $degree = 90; break; case 3: $degree = 180; break; case 6: $degree = -90; break; } // 회전각도 있으면 이미지 회전 if ($degree) { $src = imagerotate($src, $degree, 0); // 세로사진의 경우 가로, 세로 값 바꿈 if ($degree === 90 || $degree === -90) { $tmp = $size; $size[0] = $tmp[1]; $size[1] = $tmp[0]; } } } } } elseif ($size[2] === 3) { $src = imagecreatefrompng($source_file); imagealphablending($src, true); } else { return; } if (empty($src)) { return; } $is_large = true; $keep_origin = false; // width, height 설정 if ($thumb_width) { if (empty($thumb_height)) { $thumb_height = round($thumb_width * $size[1] / $size[0]); if ($thumb_width > $size[0]) { $keep_origin = true; } } else { if ($size[0] < $thumb_width || $size[1] < $thumb_height) { $is_large = false; } } } else { if ($thumb_height) { $thumb_width = round($thumb_height * $size[0] / $size[1]); } } $dst_x = 0; $dst_y = 0; $src_x = 0; $src_y = 0; $src_w = $size[0]; $src_h = $size[1]; $dst_w = $keep_origin ? $src_w : $thumb_width; $dst_h = $keep_origin ? $src_h : $thumb_height; $ratio = $dst_h / $dst_w; if ($is_large) { // 크롭처리 if ($is_crop) { switch ($crop_mode) { case 'center': if ($size[1] / $size[0] >= $ratio) { $src_h = round($src_w * $ratio); $src_y = round(($size[1] - $src_h) / 2); } else { $src_w = round($size[1] / $ratio); $src_x = round(($size[0] - $src_w) / 2); } break; default: if ($size[1] / $size[0] >= $ratio) { $src_h = round($src_w * $ratio); } else { $src_w = round($size[1] / $ratio); } break; } } $dst = imagecreatetruecolor($dst_w, $dst_h); if ($size[2] === 3) { imagealphablending($dst, false); imagesavealpha($dst, true); } elseif ($size[2] === 1) { $palletsize = imagecolorstotal($src); if ($src_transparency >= 0 && $src_transparency < $palletsize) { $transparent_color = imagecolorsforindex($src, $src_transparency); $current_transparent = imagecolorallocate($dst, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); imagefill($dst, 0, 0, $current_transparent); imagecolortransparent($dst, $current_transparent); } } } else { $dst = imagecreatetruecolor($dst_w, $dst_h); $bgcolor = imagecolorallocate($dst, 255, 255, 255); // 배경색 if ($src_w < $dst_w) { if ($src_h >= $dst_h) { $dst_x = round(($dst_w - $src_w) / 2); $src_h = $dst_h; } else { $dst_x = round(($dst_w - $src_w) / 2); $dst_y = round(($dst_h - $src_h) / 2); $dst_w = $src_w; $dst_h = $src_h; } } else { if ($src_h < $dst_h) { $dst_y = round(($dst_h - $src_h) / 2); $dst_h = $src_h; $src_w = $dst_w; } } if ($size[2] === 3) { $bgcolor = imagecolorallocatealpha($dst, 0, 0, 0, 127); imagefill($dst, 0, 0, $bgcolor); imagealphablending($dst, false); imagesavealpha($dst, true); } elseif ($size[2] === 1) { $palletsize = imagecolorstotal($src); if ($src_transparency >= 0 && $src_transparency < $palletsize) { $transparent_color = imagecolorsforindex($src, $src_transparency); $current_transparent = imagecolorallocate($dst, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); imagefill($dst, 0, 0, $current_transparent); imagecolortransparent($dst, $current_transparent); } else { imagefill($dst, 0, 0, $bgcolor); } } else { imagefill($dst, 0, 0, $bgcolor); } } imagecopyresampled($dst, $src, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); // sharpen 적용 if ($is_sharpen && $is_large) { $val = explode('/', $um_value); UnsharpMask($dst, $val[0], $val[1], $val[2]); } if ($size[2] === 1) { imagegif($dst, $thumb_file); } elseif ($size[2] === 3) { $png_compress = 5; imagepng($dst, $thumb_file, $png_compress); } else { $jpg_quality = 90; imagejpeg($dst, $thumb_file, $jpg_quality); } chmod($thumb_file, 0644); // 추후 삭제를 위하여 파일모드 변경 imagedestroy($src); imagedestroy($dst); return $thumb_file; }