<?php
function img_view($img_file, $type, $overwrite='', $size=0, $temp_use='', $temp_name='', $ret_opt='') {
	if ($img_file == '') {
		return;
	}
	$img_SJIS = myfile_ENCODE($img_file);
	if(!($attr = @getimagesize($img_SJIS))) {
		return;
	}
	$width = $attr[0];
	$height = $attr[1];
	if ($width > $height) {
		$checksize = $width;
	} else {
		$checksize = $height;
	}
	if (photo_JPEG_RESIZE == 'YES' and is_ext_filename_str($img_file, 'jpg,jpeg')) {
		if ($size == 0) {
			if ($type == 'thumb') {
				$size = photo_RESIZE_SIZE_THUMB;
			} else {
				$size = photo_RESIZE_SIZE_SMALL;
			}
		}
		$img_SJIS = myfile_ENCODE(str_replace('/', DIRECTORY_SEPARATOR, $img_file));
		if ($temp_use <> '') {
			//	$temp_file = tempnam(sys_get_temp_dir(), 'res');	// i_view32は拡張子がjpgでないとダメ
			if (defined("photo_GALLERY_99_TEMP_DIR") and photo_GALLERY_99_TEMP_DIR.'' <> '') {
				if (!@opendir(photo_GALLERY_99_TEMP_DIR)) {
					@mkdir(photo_GALLERY_99_TEMP_DIR);
				}
				if ($temp_name == '') {
					$min = intval($_SESSION['login_id']) * 10000;
					$jpg = mt_rand($min, $min+9999).'.jpg';
				} else {
					$jpg = $_SESSION['login_id'].'-'.$temp_name.'.jpg';
				}
				$temp_file = myfile_ENCODE(str_replace('/', DIRECTORY_SEPARATOR, photo_GALLERY_99_TEMP_DIR.'/'.$jpg));
				if ($temp_use == 'y') {		// $temp_use == 'c' の場合、既存のファイルを使う。
					$ret = resize_jpeg($img_SJIS, $size, $temp_file);
				}
				if (@file_exists($temp_file)) {
					$img_SJIS = $temp_file;
				} else {
					write_error_log(PHOTO_ERROR_LOG_FILE, 'exec(resize:tmpfile)', 'Err_file='.$img_file);
				}
			}
		} else {
			if ($type == 'thumb') {
				$dir = photo_RESIZE_DIR_THUMB;
			} else {
				$dir = photo_RESIZE_DIR_SMALL;
			}
			$resize_file = resize_filename($img_file, $dir);
			$resize_SJIS = myfile_ENCODE(str_replace('/', DIRECTORY_SEPARATOR, $resize_file));
			if ($checksize > $size) {
				if ($overwrite == 'w' or !@file_exists($resize_SJIS)) {
					$ret = resize_jpeg($img_SJIS, $size, $resize_SJIS);
					if ($ret <> 0) {
						write_error_log(PHOTO_ERROR_LOG_FILE, 'exec(resize)', 'Err_file='.$img_file);
					}
				}
			}
			if (@file_exists($resize_SJIS)) {
				$img_SJIS = $resize_SJIS;
			}
		}
	}
	if ($ret_opt == 'smallname') {
		if ($img_SJIS == $resize_SJIS) {
			return ($resize_file);
		} else {
			return ($img_file);
		}
	}
	$fp = @fopen($img_SJIS, "rb");
	if ($attr && $fp) {
		if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
			$img_file = myfile_ENCODE(substr($img_file,strrpos($img_file,'/')+1));
		} else {
			$img_file = urlencode(substr($img_file,strrpos($img_file,'/')+1));
		}
		header('Content-type: '.$attr['mime'].'; name='.$img_file);
		header('Content-Disposition: inline; filename='.$img_file);
		fpassthru($fp);
		@fclose($fp);
		if ($temp_file and $temp_name == '') {		// $temp_nameが指定されない場合のみ削除
			while (!unlink($temp_file)){		// 確実にファイルを消すため
				usleep(10000);	// 0.01秒
			};
		}
	}
}
function resize_filename($img_file, $dir) {		// UTF-8
	$resize_dir = up_folder_path($img_file).$dir;
	if (!@opendir(myfile_ENCODE($resize_dir))) {
		@mkdir(myfile_ENCODE($resize_dir));
	}
	return ($resize_dir.basename($img_file));
}
function resize_jpeg($src, $size, $tgt) {		// SJIS-win
	exec('i_view32 "'.$src.'" /resize=('.$size.','.$size.') /aspectratio /silent /convert="'.$tgt.'"', $o, $ret);
	return $ret;
}
?>
