<?
include "header.php";
if(isset($_GET['page'])) { $page = $_GET['page']; } elseif(isset($_POST['page'])) { $page = $_POST['page']; } else { $page = 1; }
if(isset($_POST['task'])) { $task = $_POST['task']; } else { $task = "main"; }
if(isset($_GET['i_id'])) { $i_id = $_GET['i_id']; } elseif(isset($_POST['i_id'])) { $i_id = $_POST['i_id']; } else { exit(); }
if(mysql_num_rows(mysql_query("SELECT * FROM ims_image WHERE i_id='$i_id' AND u_id='$user_info[u_id]'")) != 1) { exit(); }
$image_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims_image WHERE i_id='$i_id' AND u_id='$user_info[u_id]'"));
if($image_info[ext] == "gif" | $image_info[ext] == "pdf") { exit(); }
if($admin_info[resize_feature] != "yes") { exit(); }



if($task == "cancel") {
header("Location: view_folder.php?f_id=$image_info[f_id]&page=$page");
exit();
}


if($task == "resize") {
$image_filename = "./u/$user_info[u_id]/$image_info[f_id]/$image_info[i_id].$image_info[ext]";
$temp_image_filename = "./temp/$image_info[i_id].$image_info[ext]";

if(!preg_match("/^([0-9]+)$/", $_POST['percent']) | $_POST['percent'] != (int)$_POST['percent']) {
echo $header;
echo "
<table cellpadding='0' cellspacing='0' width='520'>
<tr><td>
<b>An Error Has Occurred:</b><br>
The resize percentage must be a numerical integer, without the % sign.<br>
For example, \"200\" or \"50\" are acceptable, but \"200%\" and \"50.142\" are not.
<br>
<form action='resize_images.php?i_id=$i_id&page=$page' method='POST'>
<input type='submit' class='button' value='Try Again'>
</td></form></tr></table>
";
echo $footer;
exit();
}

$percent = $_POST['percent']/100;
$image_size = getimagesize($image_filename);
$new_x = (int) $image_size[0]*$percent;
$new_y = (int) $image_size[1]*$percent;

if($new_x < 1 | $new_y < 1) {
echo $header;
echo "
<table cellpadding='0' cellspacing='0' width='520'>
<tr><td>
<b>An Error Has Occurred:</b><br>
The image size is too small. Please enter a larger resize percentage.
<br>
<form action='resize_images.php?i_id=$i_id&page=$page' method='POST'>
<input type='submit' class='button' value='Try Again'>
</td></form></tr></table>
";
echo $footer;
exit();
}

if($new_x > 4000 | $new_y > 4000) {
echo $header;
echo "
<table cellpadding='0' cellspacing='0' width='520'>
<tr><td>
<b>An Error Has Occurred:</b><br>
The image size is too large. Please enter a smaller resize percentage.
<br>
<form action='resize_images.php?i_id=$i_id&page=$page' method='POST'>
<input type='submit' class='button' value='Try Again'>
</td></form></tr></table>
";
echo $footer;
exit();
}


$space_used = mysql_fetch_assoc(mysql_query("SELECT sum(size) AS size FROM ims_image WHERE u_id='$user_info[u_id]'"));
if($space_used[size] == NULL) { $space_used[size] = 0; }
$space_avail = $user_info[space_avail] * (1024 * 1024);
$space_left = (int)$space_avail - (int)$space_used[size];

copy($image_filename, $temp_image_filename);


if($image_info[ext] == "bmp") {
$original = imagecreatefromjpeg($temp_image_filename);
$final = imagecreatetruecolor($new_x, $new_y);
imagecopyresampled($final, $original, 0, 0, 0, 0, $new_x, $new_y, $image_size[0], $image_size[1]);
imagejpeg($final, $temp_image_filename);
ImageDestroy($original);
ImageDestroy($final);

} elseif($image_info[ext] == "jpeg" | $image_info[ext] == "jpg") {
$original = imagecreatefromjpeg($temp_image_filename);
$final = imagecreatetruecolor($new_x, $new_y);
imagecopyresampled($final, $original, 0, 0, 0, 0, $new_x, $new_y, $image_size[0], $image_size[1]);
imagejpeg($final, $temp_image_filename);
ImageDestroy($original);
ImageDestroy($final);

} elseif($image_info[ext] == "png") {
$original = imagecreatefrompng($temp_image_filename);
$final = imagecreatetruecolor($new_x, $new_y);
imagecopyresampled($final, $original, 0, 0, 0, 0, $new_x, $new_y, $image_size[0], $image_size[1]);
imagepng($final, $temp_image_filename);
ImageDestroy($original);
ImageDestroy($final);
}


$temp_image_size = filesize($temp_image_filename);
if(($temp_image_size - filesize($image_filename)) >= $space_left) {
unlink($temp_image_filename);
echo $header;
echo "
<table cellpadding='0' cellspacing='0' width='520'>
<tr><td>
<b>An Error Has Occurred:</b><br>
You do not have enough space in your account.
<br>
<form action='resize_images.php?i_id=$i_id&page=$page' method='POST'>
<input type='submit' class='button' value='Try Again'>
</td></form></tr></table>
";
echo $footer;
exit();
}

if(round($temp_image_size/1024, 0) > $user_info[max_filesize]) {
unlink($temp_image_filename);
echo $header;
echo "
<table cellpadding='0' cellspacing='0' width='520'>
<tr><td>
<b>An Error Has Occurred:</b><br>
Your file could not be resized to this scale because the new filesize 
exceeds the maximum as set by your administrator.
<br>
<form action='resize_images.php?i_id=$i_id&page=$page' method='POST'>
<input type='submit' class='button' value='Try Again'>
</td></form></tr></table>
";
echo $footer;
exit();
}

unlink($image_filename);
copy($temp_image_filename, $image_filename);
unlink($temp_image_filename);

$image_size = filesize($image_filename);
mysql_query("UPDATE ims_image SET size='$image_size' WHERE i_id='$image_info[i_id]'");

header("Location: view_folder.php?f_id=$image_info[f_id]&page=$page");
exit();
}




if($task == "main") {
$image_filename = "./u/$user_info[u_id]/$image_info[f_id]/$image_info[i_id].$image_info[ext]";
$image_size = getimagesize($image_filename);
$old_x = (int) $image_size[0];
$old_y = (int) $image_size[1];

$min_percent_x = 1/$old_x*100;
$min_percent_y = 1/$old_y*100;
if($min_percent_x < $min_percent_y) { $min_percent = (int) $min_percent_y; } else { $min_percent = (int) $min_percent_x; }
if($min_percent < 1) { $min_percent = 1; }

$max_percent_x = 4000/$old_x*100;
$max_percent_y = 4000/$old_y*100;
if($max_percent_x < $max_percent_y) { $max_percent = (int) $max_percent_x; } else { $max_percent = (int) $max_percent_y; }
if($max_percent > 999) { $max_percent = 999; }


echo $header;
echo "

<table cellpadding='0' cellspacing='0' width='520'>
<tr><td>
<b>Resize Image: \"$image_info[title]\"</b><br>
Please enter the percentage of the size increase or decrease for this image.
<br>For example, 200% equals double size, and 50% equals half size.
<br>Only press the button once - larger images take longer to resize.
<br>Making the image smaller/larger will also make the image filesize smaller/larger.
</td></tr></table>

<br>

<table cellpadding='0' cellspacing='0'>
<tr><td style='padding-top: 5px; padding-right: 9px; padding-left: 9px; padding-bottom: 7px; background: #FFFFFF; border: 1px solid #AAAAAA;'>
Current Dimensions: <b>$old_x x $old_y pixels</b><br>
Choose a percent between $min_percent% and $max_percent%
</td></tr></table>

<br>
<table cellspacing='0' cellpadding='0'>
<tr>
<form action='resize_images.php' method='POST'>
<td><input type='text' name='percent' maxlength='3' size='2' value='100'>&nbsp;</td>
<td style='padding-bottom: 2px;'><b><font style='font-size: 11pt;'>%</font></b></td>
<td>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input class='button' type='submit' value='Resize Image'>&nbsp;
</td>
<input type='hidden' name='i_id' value='$i_id'>
<input type='hidden' name='task' value='resize'>
<input type='hidden' name='page' value='$page'>
</form>
<form action='resize_images.php' method='POST'>
<td>
<input class='button' type='submit' value='Cancel'>
</td>
<input type='hidden' name='i_id' value='$i_id'>
<input type='hidden' name='task' value='cancel'>
<input type='hidden' name='page' value='$page'>
</form>
</tr>
</table>
";
echo $footer;
exit();
}


?>