<?
//check value
if($_POST["mag_name"]=="")
{
echo"undefinedata";
exit();
}
$mag_name   = $_POST["mag_name"];
$mag_public = $_POST["mag_public"];
$mag_key    = md5(time());

$name_time = time();

if($_FILES['mag_thumb']['name'] != ""){
//หาเอกเทนชั่นของภาพ
$doc_filename = stripslashes($_FILES['mag_thumb']['name']);
$doc_extension = getExtension($doc_filename);
//upload doc
$newdocname="../datacenter/upload/frn". $name_time.".".$doc_extension;
copy($_FILES['mag_thumb']['tmp_name'], $newdocname);
$mag_thumb = "frn".$name_time.".".$doc_extension;
$images = $_FILES['mag_thumb']['tmp_name'];
$width=500; //*** Fix Width & Heigh (Autu caculate) ***//
				$new_images = $mag_thumb;
				$size=GetimageSize($images);
				$height=round($width*$size[1]/$size[0]);
				if($fileextent=="jpg"||$fileextent=="jpeg"){
				$images_orig = ImageCreateFromJPEG($images);
				$photoX = ImagesX($images_orig);
				$photoY = ImagesY($images_orig);
				$images_fin = ImageCreateTrueColor($width, $height);
				ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
				ImageJPEG($images_fin,"../datacenter/upload/".$new_images);
				}elseif($fileextent=="png"){
				$images_orig = ImageCreateFromPNG($images);
				$photoX = ImagesX($images_orig);
				$photoY = ImagesY($images_orig);
				$images_fin = ImageCreateTrueColor($width, $height);
				ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
				ImagePNG($images_fin,"../datacenter/upload/".$new_images);
				}elseif($fileextent=="gif"){
				$images_orig = ImageCreateFromGIF($images);
				$photoX = ImagesX($images_orig);
				$photoY = ImagesY($images_orig);
				$images_fin = ImageCreateTrueColor($width, $height);
				ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
				ImageGIF($images_fin,"../datacenter/upload/".$new_images);
				}
				//ImageDestroy($images_orig);
				//ImageDestroy($images_fin);
}
else
{
Jsalert("ท่านไม่ได้อัพโหลดไฟล์ไอคอน");
jsback();
}
$magsql = "INSERT INTO ann_gall (mag_name, mag_public, mag_thumb, mag_status, mag_key)
VALUES ('$mag_name', '$mag_public', '$mag_thumb', 'Y', '$mag_key') ";
$magquery = mysql_query("$magsql");

   for($j=1;$j<=(int)($_POST["hdnLine"]);$j++)
		{
			if($_FILES["fileUpload".$j]["name"] != "")
			{
				$fileextent = getExtension($_FILES["fileUpload".$j]["name"]);
				$pagename = "courseimgsend".$name_time."_".$j.".".$fileextent;


				$sql = "select * from ann_gall where mag_key ='".$mag_key."'";
				$qri = mysql_query($sql);
		     	$arrmag = mysql_fetch_row($qri);
				$mag_id = $arrmag[0];
				$sqlim = "insert into ann_gall_page (page_int, page_mag_id, page_images) values ('$j','$mag_id','$pagename')";
				mysql_query($sqlim);
				
				$images = $_FILES["fileUpload".$j]["tmp_name"];
				$new_images = $pagename;
				copy($images,"../datacenter/upload/".$new_images);
				$width=500; //*** Fix Width & Heigh (Autu caculate) ***//
				$size=GetimageSize($images);
				$height=round($width*$size[1]/$size[0]);

				if($fileextent=="jpg"||$fileextent=="jpeg"){
				$images_orig = ImageCreateFromJPEG($images);
				$photoX = ImagesX($images_orig);
				$photoY = ImagesY($images_orig);
				$images_fin = ImageCreateTrueColor($width, $height);
				ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
				ImageJPEG($images_fin,"../datacenter/upload/".$new_images);
				}elseif($fileextent=="png"){
				$images_orig = ImageCreateFromPNG($images);
				$photoX = ImagesX($images_orig);
				$photoY = ImagesY($images_orig);
				$images_fin = ImageCreateTrueColor($width, $height);
				ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
				ImagePNG($images_fin,"../datacenter/upload/".$new_images);
				}elseif($fileextent=="gif"){
				$images_orig = ImageCreateFromGIF($images);
				$photoX = ImagesX($images_orig);
				$photoY = ImagesY($images_orig);
				$images_fin = ImageCreateTrueColor($width, $height);
				ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
				ImageGIF($images_fin,"../datacenter/upload/".$new_images);
				}
				ImageDestroy($images_orig);
				ImageDestroy($images_fin);


			}
		}
jsalert("สร้างแกลลอลี่ภาพสำเร็จ");
GoToURL("index.php?option=galllist&action=editgalltail&itemid=$mag_id");
?>