<?php
ini_set('memory_limit', '1024M');

if( isset($_POST["PHPSESSID"]) )
	session_id($_POST["PHPSESSID"]);

session_start();

include('includes/check_login.php');

if( !isset($_SESSION['upload_news_id']) || ( $static_uint_news_id = (int)$_SESSION['upload_news_id'] ) <= 0 )
	exit('No session');

if( !file_exists('../images/news/' . $static_uint_news_id) && !mkdir('../images/news/' . $static_uint_news_id, 0775) )
	exit('Create news directory failed');

if( !isset($_FILES['Filedata']) || !is_uploaded_file($_FILES['Filedata']['tmp_name']) || $_FILES['Filedata']['error'] != 0 )
	exit(( !isset($_FILES['Filedata']) ? 'No file is selected' : $_FILES['Filedata']['error'] ));
else if( !( $static_str_img_file_name = trim($_FILES['Filedata']['tmp_name']) ) )
	exit('Invalid file name');

require_once('includes/common.php');
require_once('includes/admin.php');

$obj_class_admin = new class_admin();

if( !( $arr_image_info = getimagesize($static_str_img_file_name) ) )
{
	if( is_uploaded_file($static_str_img_file_name) )
		unlink($static_str_img_file_name);

	exit('Invalid file type');
}

switch( $arr_image_info[2] )
{
	case IMAGETYPE_GIF:
		$img_file = imagecreatefromgif($static_str_img_file_name);
		break;

	case IMAGETYPE_JPEG:
		$img_file = imagecreatefromjpeg($static_str_img_file_name);
		break;

	case IMAGETYPE_PNG:
		$img_file = imagecreatefrompng($static_str_img_file_name);
		break;

	case IMAGETYPE_WBMP:
		$img_file = imagecreatefromwbmp($static_str_img_file_name);
		break;

	case IMAGETYPE_XPM:
		$img_file = imagecreatefromxpm($static_str_img_file_name);
		break;

	case IMAGETYPE_XBM:
		$img_file = imagecreatefromxbm($static_str_img_file_name);
		break;

	default:
		if( is_uploaded_file($static_str_img_file_name) )
			unlink($static_str_img_file_name);
	
		exit('Invalid image type');
}

if( is_uploaded_file($static_str_img_file_name) )
	unlink($static_str_img_file_name);

if( !$img_file )
	exit('Create image failed');

$uint_now = time();

$float_img_file_ratio = (float)$arr_image_info[1] / (float)$arr_image_info[0];

if( $float_img_file_ratio <= 1.00 )
{
	if( $arr_image_info[0] <= NEWS_PIC_WIDTH )
	{
		$uint_target_width = $arr_image_info[0];
		$uint_target_height = $arr_image_info[1];
	}
	else
	{
		$uint_target_width = NEWS_PIC_WIDTH;
		$uint_target_height = round((float)NEWS_PIC_WIDTH * $float_img_file_ratio);
	}
}
else
{
	if( $arr_image_info[1] <= NEWS_PIC_WIDTH )
	{
		$uint_target_width = $arr_image_info[0];
		$uint_target_height = $arr_image_info[1];
	}
	else
	{
		$uint_target_width = round((float)NEWS_PIC_WIDTH / $float_img_file_ratio);
		$uint_target_height = NEWS_PIC_WIDTH;
	}
}

$img_watermark = imagecreatefrompng('../images/watermark.png');
$uint_watermark_width = imagesx($img_watermark);
$uint_watermark_height = imagesy($img_watermark);

$img_temp = imagecreatetruecolor($uint_target_width, $uint_target_height);
$color_white = imagecolorallocate($img_temp, 255, 255, 255);
imagefill($img_temp, 0, 0, $color_white);

imagecopyresampled($img_temp, $img_file, 0, 0, 0, 0, $uint_target_width, $uint_target_height, $arr_image_info[0], $arr_image_info[1]);

imagecopyresampled($img_temp, $img_watermark, 25, ($uint_target_height - $uint_watermark_height - 25), 0, 0, $uint_watermark_width, $uint_watermark_height, $uint_watermark_width, $uint_watermark_height);

$str_temp_file_name = '../images/news/' . $uint_now . '.jpg';

if( !imagejpeg($img_temp, $str_temp_file_name, 100) || !file_exists($str_temp_file_name) )
	exit('Create jpeg file failed');
else
	imagedestroy($img_temp);

$str_hash = md5_file($str_temp_file_name);

if( $obj_class_admin->get_news_picture_by_news_id_n_hash($static_uint_news_id, $str_hash) )
{
	unlink($str_temp_file_name);

	exit('Picture already exists');
}
else if( !( $uint_picture_id = $obj_class_admin->add_news_picture($static_uint_news_id, $str_hash) ) )
{
	unlink($str_temp_file_name);

	exit('Add picture data failed');
}
else
{
	rename($str_temp_file_name, '../images/news/' . $static_uint_news_id . '/' . $uint_picture_id . '.jpg');

	if( !$obj_class_admin->get_news_pictures_is_cover_by_news_id($static_uint_news_id) && $obj_class_admin->set_news_picture_is_cover($uint_picture_id) )
	{
		$float_target_ratio = (float)NEWS_COVER_HEIGHT / (float)NEWS_COVER_WIDTH;

		if( $float_img_file_ratio == $float_target_ratio )
		{
			$uint_source_width = $arr_image_info[0];
			$uint_source_height = $arr_image_info[1];

			$uint_source_x = 0;
			$uint_source_y = 0;
		}
		else if( $float_img_file_ratio < $float_target_ratio )
		{
			$uint_source_width = round($arr_image_info[1] / $float_target_ratio);
			$uint_source_height = $arr_image_info[1];

			$uint_source_x = floor(( $arr_image_info[0] - $uint_source_width ) / 2);
			$uint_source_y = 0;
		}
		else
		{
			$uint_source_width = $arr_image_info[0];
			$uint_source_height = round($arr_image_info[0] * $float_target_ratio);

			$uint_source_x = 0;
			$uint_source_y = floor(( $arr_image_info[1] - $uint_source_height ) / 2);
		}
	
		$img_temp_cover = imagecreatetruecolor(NEWS_COVER_WIDTH, NEWS_COVER_HEIGHT);
		$color_white = imagecolorallocate($img_temp_cover, 255, 255, 255);
		imagefill($img_temp_cover, 0, 0, $color_white);

		imagecopyresampled($img_temp_cover, $img_file, 0, 0, $uint_source_x, $uint_source_y, NEWS_COVER_WIDTH, NEWS_COVER_HEIGHT, $uint_source_width, $uint_source_height);
		imagedestroy($img_file);

		$str_target_file_name = '../images/news/' . $static_uint_news_id . '.jpg';

		if( file_exists($str_target_file_name) )
			unlink($str_target_file_name);

		imagejpeg($img_temp_cover, $str_target_file_name, 83);
		imagedestroy($img_temp_cover);
	}
	else
		imagedestroy($img_file);

	exit('1');
}
?>