<?php
	require("__include-common.php");
	require("../account/__logincheck.php");
	set_time_limit(0);		//実行時間制御なし　max_execution_time
	if (photo_IMAGEMAGIC <> 'YES') {
		exit;
	}
	if ($_SESSION['システム管理者'] <> 'YES' and photo_IMAGEMAGIC_ALLUSER <> 'YES') {
		exit;
	}
	if ($_POST['path'] <> '') {
		$path = form_str_filepath($_POST['path']);
	} elseif ($_GET['path'] <> '') {
		$path = my_GET('path');
	}
	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($path.'/', 0, $len+1) <> photo_LIMITED_IMAGES_FOLDER.'/') {
				header("Location: index.php");
			}
		}
	}
	if ($_GET['page'].'' <> '') {
		$page = intval($_GET['page']);
		if ($page < 1) {
			$page = 1;
		}
	} else {
		$page = 1;
	}
	$file = $_SESSION['photo'][$page-1]['file'];
	$src_file = $path.'/'.$file;
	$src_file_url = urlencode($src_file);
	if (($type = $_SESSION['photo'][$page-1]['type']) == 'video') {
		$getimg = "video-jpeg.php?file=".$src_file_url."&ss=".$_SESSION['video_thumb_ss'];
	} else {
		$getimg = "img-view.php?img=".$src_file_url;
	}
	if ($_POST['option'] <> '') {
		$option_post = $_POST['option'];
			$overwrite = $_POST['overwrite'];
		$_SESSION['photo_convert_option_post'] = $option_post;
		$opno = -2;
	} else {
		$no = my_GET('no').'';
		if ($no == 'all') {
			$opno = -1;
		} elseif ($no == 'allop') {
			if ($_SESSION['システム管理者'] <> 'YES') {
				exit;
			}
			$opno = -3;
		} elseif ($no.'' == '') {
			$opno = 0;
		} else {
			$opno = intval($no);
		}
		$overwrite = $_GET['ov'];
	}
	$cnv_dir = trim(photo_IMG_CONVERT_DIR,'/');
	$src_file = $path.'/'.$file;
	if ($type == 'video') {
		$ss = $_SESSION['video_thumb_ss'];
		if ($ss.'' == '') $ss = 0;
		$src_file = ltrim(photo_RESIZE_DIR_THUMB,'/').left($file, strrpos($file, '.')).'-'.$ss.'.jpg';
		$cnv_ext = '.jpg';
	} else {
		$src_file = $file;
		$cnv_ext = strrchr($file, '.');
	}
	if ($_COOKIE['photo_convert_view_size'] != '') {
		$imgsize = $_COOKIE['photo_convert_view_size'];
	} else {
		$imgsize = CONVERT_MID_SIZE;
	}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="author" content="ok.2nd">
<style>
html,body {
	width: 100%;
	height: 100%;
}
#title {
	margin: 5px 0 10px 0;
}
#tool_title {
	color: #b8860b;
}
img {
	-ms-interpolation-mode: bicubic;
}
div#info {
	margin: 10px 0;
}
div#photoarea {
	margin: 0;
	width: 100%;
	text-align: center;
}
.photobox {
	display: inline;
	float: left;
	margin: 0 5px 5px 0;
}
img.photo {
	height: <?= $imgsize ?>px;
}
#selected_opt {
	background-color: #ffe000;
}
.select_opt {
	margin-right: 5px;
}
#free_opt {
	clear: both;
}
#info {
	margin: 0 0 2px 0;
}
div#select_opt_area {
	margin: 5px 0;
	line-height: 1.3;
}
#select_opt_area ul {
	list-style-type: none;
	margin: 5px 0;
}
#select_opt_area li {
	display: inline;
	float: left;
	white-space: nowrap;
}
div#photo_image_size_change {
	margin: 5px 0;
}
#info p {
	margin: 2px 0;
}
.infotext {
	color: #0000ff;
	background-color: #fff0e8;
}
.caption {
	margin-top: 2px;
	color: #FF6347;
}
.caption_file {
	color: #408080;
}
#option_post_true {
	background-color: #ffe0c0;
}
<?php	if ($opno <> 0 and $_COOKIE['photo_convert_original_onoff'] == 'none') { ?>
#original {
	display: none;
}
<?php	} ?>
</style>
<script src="../scripts/jquery.js"></script>
<script src="../scripts/jquery.cookie.js"></script>
<script>
var opno = <?= $opno ?>;
function convertClick(page, no) {
	if (document.getElementById('overwrite').checked) {
		var overwrite = '&ov=w';
	} else {
		var overwrite = '';
	}
	location.href = '?path=<?= urlencode($path) ?>&page=' + page + '&no=' + no + overwrite;
	return false;
}
function originalOnOff() {
	if (document.getElementById('originalOff').checked) {
		if (opno != 0) {
			document.getElementById('original').style.display = 'none';
		}
		$.cookie('photo_convert_original_onoff','none',{ path:'<?= MY_SESSION_PATH ?>', expires:365 });
	} else {
		document.getElementById('original').style.display = 'inline';
		$.cookie('photo_convert_original_onoff','inline',{ path:'<?= MY_SESSION_PATH ?>', expires:365 });
	}
}
</script>
<title>画像処理</title>
<link rel="stylesheet" href="<?= _STYLE_SHEET_FOLDER ?>/tools_common.css?20120406">
</head>
<body>
<div id="title">
<span id="tool_title">画像処理</span>
<?php 	if ($_SESSION['システム管理者'] == 'YES') { ?>
<a href="convert-opt.php?" target="_blank" style="margin-left:20px;">画像処理オプション</a>
<?php	} ?>
</div>
<div id="select_opt_area">
<form method="POST" action="?<?= $_SERVER['QUERY_STRING'] ?>">
<div>
<ul>
<li><a class="select_opt"<?php if ($opno == 0) echo ' id="selected_opt"' ?> href="?path=<?= urlencode($path) ?>&page=<?= $page ?>">オリジナル</a></li>
<?php
	$rec_ary = array();
	$con = my_mysql_connect(_DB_SCHEMA);
	$sql = 'select * from m_convert_opt where c_delete = 0 order by c_displayOrder';
	$rs = my_mysql_query($sql);
	while ($rec = mysql_fetch_array($rs)) {
		$rec_ary[] = $rec;
	};
	$con = mysql_close($con);
	foreach ($rec_ary as $rec) {
?>
	<li><a class="select_opt"<?php if ($opno == $rec[id_opt]) echo ' id="selected_opt"' ?> href="#" xxx="?path=<?= urlencode($path) ?>&page=<?= $page ?>&no=<?= $rec[id_opt] ?>" onClick="convertClick('<?= $page ?>','<?= $rec[id_opt] ?>')"><?= $rec[c_name] ?></a></li>
<?php
	}
?><li><a class="select_opt"<?php if ($opno == -1) echo ' id="selected_opt"' ?> href="?path=<?= urlencode($path) ?>&page=<?= $page ?>&no=all">←処理済全て</a></li>
</ul>
</div>
<?php 	if ($_SESSION['システム管理者'] == 'YES') { ?>
	<div id="free_opt">
	<span<?php if ($option_post <> '') echo ' id="selected_opt"' ?>">任意オプション</span>：<input class="text" type="text" name="option" value="<?= $_SESSION['photo_convert_option_post'] ?>"<?php if ($option_post <> '') echo ' id="option_post_true"' ?>style="width:300px;">
	<input type="submit" value="実行">
<a class="select_opt"<?php if ($opno == -3) echo ' id="selected_opt"' ?> href="?path=<?= urlencode($path) ?>&page=<?= $page ?>&no=allop" style="margin-left:5px;">←含めて処理済全て</a>
	</div>
<?php	} ?>
	<div style="clear:both;">
	<label><input type="checkbox" id="originalOff" onClick="originalOnOff()"<?php if ($_COOKIE['photo_convert_original_onoff'] == 'none') echo ' checked' ?>>オリジナル非表示</label>
	<label><input type="checkbox" id="overwrite" name="overwrite" value="w">再処理＆処理結果画像上書き</label>
	</div>
</form>
</div>
<div id="photo_image_size_change">
<script>
var imgsize = <?= $imgsize ?>;
var bigid = false;
function resizeImage(up_down) {
	setSizeImage(imgsize + (up_down));
}
function setSizeImage(size) {
	if (size < <?= CONVERT_SMALL_SIZE ?>) {
		return;
	}
	imgsize = size;
	$.cookie('photo_convert_view_size',size,{ path:'<?= MY_SESSION_PATH ?>', expires:365 });
	$("img.photo").height(size);
}
</script>
<input type="button" value="最小" onClick="setSizeImage(<?= CONVERT_SMALL_SIZE ?>)">
<input type="button" value="小さく" onClick="resizeImage(-<?= CONVERT_UPDOWN_SIZE ?>)">
<input type="button" value="大きく" onClick="resizeImage(<?= CONVERT_UPDOWN_SIZE ?>)">
<input type="button" value="中" onClick="setSizeImage(<?= CONVERT_MID_SIZE ?>)">
<input type="button" value="大" onClick="setSizeImage(<?= CONVERT_BIG_SIZE ?>)">
<input type="button" value="最大" onClick="setSizeImage(<?= CONVERT_MAX_SIZE ?>)">
</div>
<div id="info">
<p>元画像：<span class="infotext"><?= $path ?>/<?= $file ?></span>
<?php if ($type == 'video') { ?>
( <span class="infotext"><?= $path ?>/<?= $src_file ?></span><?php if ($_SESSION['システム管理者'] == "YES") { ?>
<a href="../tools/file-manager.php?path=<?= urlencode($path.photo_RESIZE_DIR_THUMB) ?>" target="_blank" style="margin-left: 10px;">ファイルマネージャ[<?= photo_RESIZE_DIR_THUMB ?>]</a>
<?php } ?>)
<?php } ?>
<?php
//	if (strlen($path.$file) <> mb_strlen($path.$file)) {
//		echo '<br><br><span style="color:red;">※ ファイルパスに日本語が含まれていると、処理できません。</span><br><br>';
//	}
?>
</p>
<?php
	if ($opno > 0) {
		$ix = 0;
		foreach ($rec_ary as $rec) {
			if ($rec[id_opt] == $opno) break;
			$ix++;
		}
		$c_option = $rec_ary[$ix][c_option];
		$c_name = $rec_ary[$ix][c_name];
		$cnv_file = cnv_file($file, $c_option, $cnv_ext);
		$info_view = true;
	} elseif ($opno == -2) {
		$c_option = $option_post;
		$cnv_file = cnv_file($file, $c_option, $cnv_ext);
		img_info_display($path, $cnv_dir, $cnv_file, $c_option);
		$info_view = true;
	} else {
		$c_option = '';
		$cnv_file = '';
		$info_view = false;
	}
	img_info_display($path, $cnv_dir, $cnv_file, $c_option, $info_view);
?>
</div>
<center><table><tr><td>
<div id="photoarea">
<?php
	img_display($getimg, $file, 'オリジナル', 'original');
	if ($opno > 0 or $opno == -2) {
		if ($getimg = get_cnv_img($path, $file, $src_file, $cnv_dir, $cnv_file, $c_option, $opno, $overwrite)) {
			img_display($getimg, $cnv_file, $c_name);
		}
	} elseif ($opno == -1) {
		foreach ($rec_ary as $rec) {
			$cnv_file = cnv_file($file, $rec[c_option], $cnv_ext);
			if ($getimg = get_cnv_img($path, $file, $src_file, $cnv_dir, $cnv_file, $rec[c_option], $rec[id_opt], $overwrite, true)) {
				img_display($getimg, $cnv_file, $rec[c_name]);
			}
		}
	} elseif ($opno == -3) {
		$cnv_path = $path.'/'.$cnv_dir.'/';
		$cnv_wildcard = ($cnv_path.left($file, strrpos($file, '.')).'-*');
		foreach (glob(myfile_ENCODE($cnv_wildcard)) as $cnv_path_JIS) {
			if (is_file($cnv_path_JIS)) {
				$cnv_path = myfile_DECODE($cnv_path_JIS);
				$cnv_file = basename($cnv_path);
				$getimg = "img-view.php?img=".urlencode($cnv_path);
				img_display($getimg, $cnv_file, '');
			}
		}
	}
?>
</div>
</td></tr></table></center>
</body>
</html>
<?php
function img_info_display($path, $cnv_dir, $cnv_file, $option, $info_view) {
?>
	<p>処理結果画像：
<?php	if ($info_view) { ?>
		<span class="infotext"><?= $path ?>/<?= $cnv_dir ?>/<?= $cnv_file ?></span>
<?php	} ?>
<?php	if ($_SESSION['システム管理者'] == "YES") { ?>
		<a href="../tools/file-manager.php?path=<?= urlencode($path.photo_IMG_CONVERT_DIR) ?>" target="_blank" style="margin-left: 20px;">ファイルマネージャ[<?= photo_IMG_CONVERT_DIR ?>]</a>
<?php	} ?>
	</p>
	<p>convertオプション：<span class="infotext"><?= $option ?></span></p>
<?php
}
function cnv_file($file, $option, $cnv_ext) {
	return left($file, strrpos($file, '.')).str_replace('--','-','-'.str_replace(' ','_',$option)).$cnv_ext;
}
function get_cnv_img($path, $file, $src_file, $cnv_dir, $cnv_file, $opt, $opno, $overwrite, $exist_only = false) {
	$cnv_path = $path.'/'.$cnv_dir.'/'.$cnv_file;
	if ($exist_only) {
		if (myfile_file_exists($cnv_path)) {
			return "img-view.php?img=".urlencode($cnv_path);
		} else {
			return false;
		}
	} else {
		if ($overwrite == 'w' || !myfile_file_exists($cnv_path)) {
			return 'img-imagemagic.php?path='.urlencode($path).'&src='.urlencode($src_file).'&cnv='.urlencode($cnv_file).'&dir='.urlencode($cnv_dir).'&opt='.urlencode(str_replace(' ','_',$opt)).'&opno='.$opno.'&ov='.$overwrite;
		} else {
			return "img-view.php?img=".urlencode($cnv_path);
		}
	}
}
function img_display($imgsrc, $file, $name, $id='') {
?>
<span class="photobox" id="<?= $id ?>">
	<p><img class="photo" src="<?= $imgsrc ?>" alt="<?= $file ?>" title="<?= $file ?>" /></p>
	<p class="caption"><?php if ($name <> '') { ?><?= $name ?> <?php } ?><span class="caption_file"><?= $file ?></span></p>
</span>
<?php
}
?>
