<?php
	require("../__common__/__define_common.php");
	require("__define.php");
	require("../__common__/include-common-all.php");
	require("../account/__logincheck.php");

	$img_file = my_GET('img').'';
	if (defined("photo_LIMITED_IMAGES_FOLDER") and photo_LIMITED_IMAGES_FOLDER <> '') {
		if ($_SESSION['システム管理者'] <> "YES" or photo_LIMITED_ADMIN_NOLIMIT <> 'YES') {
			$len = strlen(photo_LIMITED_IMAGES_FOLDER);
			if (substr($img_file, 0, $len) <> photo_LIMITED_IMAGES_FOLDER) {
				exit;
			}
		}
	}
	if ($img_file == '') {
		exit;
	}
	$img_SJIS = myfile_ENCODE($img_file);
	if(!($attr = @getimagesize($img_SJIS))) {
		exit;
	}
	$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 ($_GET['type'] == 'thumb') {
			$dir = photo_RESIZE_DIR_THUMB;
			$size = photo_RESIZE_SIZE_THUMB;
		} else {
			$dir = photo_RESIZE_DIR_SMALL;
			$size = photo_RESIZE_SIZE_SMALL;
		}
		$resize_file = resize_filename($img_file, $dir);
		$resize_SJIS = myfile_ENCODE(str_replace('/', DIRECTORY_SEPARATOR, $resize_file));
		if ($checksize > $size) {
			if (!@file_exists($resize_SJIS)) {
				$img_SJIS = myfile_ENCODE(str_replace('/', DIRECTORY_SEPARATOR, $img_file));
				$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;
		}
	}
	$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);
	}
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;
}
?>
