Location: PHPKode > projects > SiteX > admin/photo_add.php
<?PHP
ob_start();
session_start();

ini_set("memory_limit","100M");

include("../includes/globals.php.inc");
include("../includes/functions.php");
include("../lang/English.php");
if(strtolower($sxSetup['Language']) != 'english') include("../lang/".$sxSetup['Language'].".php");
include("../themes/$THEME_FOLDER/config.php.inc");

protectWriteMode();

if((!userEditor())AND(!userAdmin()))  loginError($sxLang['LoginRequired']);
if((userEditor())AND(!$sxSetup['EditorRightsPhotoAdd']))  loginError($sxLang['LoginRequired']);

$date_month 	= substr($date, 5, 2);
$date_day 		= substr($date, 8, 2);
$date_year 		= substr($date, 0, 4);

$timestamp = mktime(0, 0, 0, $date_month, $date_day, $date_year);
$curDateTime = date("YmdHis");

if($Submit)
{
	$sxPhotoName = basename($_FILES['PhotoUpload']['name']);
	
	// Check to make sure file is an acceptable image type
	if (eregi ("(.)+\\.(jp(e){0,1}g$|gif$|png$|bmp$)", strtolower($sxPhotoName)))
	{
		// Name photo by incremental id
		$sxFileNameParts = explode('.',$sxPhotoName);
		
		$query = "SELECT * FROM $DB_Photos ORDER BY id DESC LIMIT 0,1";
		$result = mysql_query($query, $Link) or queryError("12", mysql_error());
		if(mysql_num_rows($result))
		{
			$ROW = mysql_fetch_object($result);
			$filename = ($ROW->id + 1) . "_".$curDateTime."." . $sxFileNameParts[1];
		}
		else
			$filename = "1_".$curDateTime."." . $sxFileNameParts[1];
		
		// Copy photo to the raw location
		if (@move_uploaded_file($_FILES['PhotoUpload']['tmp_name'], "../".$sxContentDir['PhotoFullsize'].$filename))
		{
			// Size max width or height - 1 to trim mishap edges
			$size_thumb = $sxSetup['GalleryMaxDimensionThumb'] - 1;
			$size_scaled = $sxSetup['GalleryMaxDimensionPhoto'] - 1;
			
			// Set JPG compression (for JPG images only)
			$compression_thumb = $sxSetup['GalleryCompressionThumb'];
			$compression_scaled = $sxSetup['GalleryCompressionPhoto'];
			
			// Read raw image
			if((imageCompatibility() == "truecolor")OR(imageCompatibility() == "normal"))
			{
				$sxImageInfo = getimagesize("../".$sxContentDir['PhotoFullsize'].$filename);
				switch($sxImageInfo[2])
				{
					case 1: $image = imagecreatefromgif("../".$sxContentDir['PhotoFullsize'].$filename); 	break;
					case 2: $image = imagecreatefromjpeg("../".$sxContentDir['PhotoFullsize'].$filename); 	break;
					case 3: $image = imagecreatefrompng("../".$sxContentDir['PhotoFullsize'].$filename); 	break;
					case 6: $image = imagecreatefromwbmp("../".$sxContentDir['PhotoFullsize'].$filename); 	break;
				}
				$width_original = $sxImageInfo[0];
				$height_original = $sxImageInfo[1];
			}
			
			// Get filesize
			$filesize_original = filesize("../".$sxContentDir['PhotoFullsize'].$filename);
			
			// Image not watermarked (yet)
			$sxImageWatermarked = 0;
			
			// Watermark raw image
			if($sxSetup['GalleryWatermarking'] == 1)
			{
				$sxImageWatermarked = 1;
				$sxRawImage = sxWatermarkImage($image, $width_original, $height_original);
				
				// Make image
				switch($sxImageInfo[2])
				{
					case 1: imagegif($sxRawImage,  "../".$sxContentDir['PhotoFullsize'].$filename); 		break;
					case 2: imagejpeg($sxRawImage, "../".$sxContentDir['PhotoFullsize'].$filename, 90); 	break;
					case 3: imagepng($sxRawImage,  "../".$sxContentDir['PhotoFullsize'].$filename); 		break;
					case 6: imagewbmp($sxRawImage, "../".$sxContentDir['PhotoFullsize'].$filename); 		break;
				}
				imagedestroy($sxRawImage);
			}	// End watermarking
			
			// Check if dimension change needed for scaled image change and make scaled image
			if(($width_original > $size_scaled)OR($height_original > $size_scaled))
			{
				$dimensions = resizeDimensions($width_original, $height_original, $size_scaled);
				$sxScaledWidth = $dimensions[0]+1;
				$sxScaledHeight = $dimensions[1]+1;
			}
			else
			{
				$sxScaledWidth = $width_original;
				$sxScaledHeight = $height_original;
			}
			
			// Form scaled image	
			if(imageCompatibility() == "truecolor")
			{
				$sxScaledImage = imagecreatetruecolor($sxScaledWidth, $sxScaledHeight);
				imagecopyresampled($sxScaledImage, $image, 0, 0, 0, 0, $sxScaledWidth, $sxScaledHeight, $width_original, $height_original);
			}
			elseif(imageCompatibility() == "normal")
			{
				$sxScaledImage = imagecreate($sxScaledWidth, $sxScaledHeight);
				imagecopyresized($sxScaledImage, $image, 0, 0, 0, 0, $sxScaledWidth, $sxScaledHeight, $width_original, $height_original);
			}
			
			// Watermark scaled image
			if($sxSetup['GalleryWatermarking'] == 1)
				$sxScaledImage = sxWatermarkImage($sxScaledImage, $sxScaledWidth, $sxScaledHeight);
			
			// Create scaled image	
			switch($sxImageInfo[2])
			{
				case 1: imagegif($sxScaledImage,  "../".$sxContentDir['PhotoScaled'].$filename); 						break;
				case 2: imagejpeg($sxScaledImage, "../".$sxContentDir['PhotoScaled'].$filename, $compression_scaled); 	break;
				case 3: imagepng($sxScaledImage,  "../".$sxContentDir['PhotoScaled'].$filename); 						break;
				case 6: imagewbmp($sxScaledImage, "../".$sxContentDir['PhotoScaled'].$filename); 						break;
			}
			imagedestroy($sxScaledImage);

			$filesize_scaled = filesize("../".$sxContentDir['PhotoScaled'].$filename);
			
			// Resize image for thumbnail
			$dimensions = resizeDimensions($width_original, $height_original, $size_thumb);
			if((imageCompatibility() == "truecolor")OR(imageCompatibility() == "normal"))
			{
				if(imageCompatibility() == "truecolor")
				{
					$image_new_thumb = imagecreatetruecolor($dimensions[0]+1, $dimensions[1]+1);
					imagecopyresampled($image_new_thumb, $image, 0, 0, 0, 0, $dimensions[0]+1, $dimensions[1]+1, $width_original, $height_original);
				}
				elseif(imageCompatibility() == "normal")
				{
					$image_new_thumb = imagecreate($dimensions[0]+1, $dimensions[1]+1);
					imagecopyresized($image_new_thumb, $image, 0, 0, 0, 0, $dimensions[0]+1, $dimensions[1]+1, $width_original, $height_original);
				}
				
				switch($sxImageInfo[2])
				{
					case 1: imagegif($image_new_thumb, "../".$sxContentDir['PhotoThumb'].$filename); 						break;
					case 2: imagejpeg($image_new_thumb, "../".$sxContentDir['PhotoThumb'].$filename, $compression_thumb); 	break;
					case 3: imagepng($image_new_thumb, "../".$sxContentDir['PhotoThumb'].$filename); 						break;
					case 6: imagewbmp($image_new_thumb, "../".$sxContentDir['PhotoThumb'].$filename); 						break;
				}
				imagedestroy($image_new_thumb);
			}
			elseif(imageCompatibility() == "imagemagick")
			{
				$image_new_thumb = imagick_readimage("../".$sxContentDir['PhotoFullsize'].$filename);
				imagick_sample($image_new_thumb, $dimensions[0]+1, $dimensions[1]+1);
				imagick_writeimage($image_new_thumb, "../".$sxContentDir['PhotoThumb'].$filename);
			}
			
			// Set final size/dimension data for db
			if($sxSetup['GalleryStoreFullsize'])
			{
				$width_final = $width_original;
				$height_final = $height_original;
				$filesize_final = $filesize_original;
				$fullsize = 1;
			}
			else
			{
				@unlink("../".$sxContentDir['PhotoFullsize'].$filename);
				$width_final = $sxScaledWidth;
				$height_final = $sxScaledHeight;
				$filesize_final = $filesize_scaled;
				$fullsize = 0;
			}
			
			$message = $sxLang['MessagePhotoAdded1'] . " '" . stripslashes($name) . "' " . $sxLang['MessagePhotoAdded2'];
						
			if($hide_random != 1)
				$hide_random = 0;

			$query = "INSERT INTO $DB_Photos (name, filename, photographer, filesize, height, width, description, timestamp, fullsize, userid, watermark, hide_random) VALUES ('$name', '$filename', '$photographer', '$filesize_final', '$height_final', '$width_final', '$description', '$timestamp', '$fullsize', '$THIS_USER', '$sxImageWatermarked', '$hide_random')";
			$result = mysql_query($query, $Link) or queryError("152", mysql_error());
			
			$query = "SELECT * FROM $DB_Photos WHERE filename='$filename'";
			$result = mysql_query($query, $Link) or queryError("151", mysql_error());
			$ROW = mysql_fetch_object($result);
			
			// Add album info to db
			for($i = 0; $i < count($albums); $i++)
			{	
				$result = mysql_query("INSERT INTO $DB_Photos_Assoc (photoid, albumid) VALUES ('$ROW->id', '$albums[$i]')", $Link);
				
				$query2 = "SELECT * FROM $DB_Photos_Assoc WHERE albumid='$albums[$i]' AND position!='0' ORDER BY position DESC LIMIT 0,1";
				$result2 = mysql_query($query2, $Link) or queryError("150", mysql_error());
				
				if(mysql_num_rows($result2))
				{
					$ROW2 = mysql_fetch_object($result2);
					$new_position = $ROW2->position + 1;
				}
				else
					$new_position = 1;
				
				$result = mysql_query("UPDATE $DB_Photos_Assoc SET position='$new_position' WHERE photoid='$ROW->id' AND albumid='$albums[$i]'", $Link) or queryError("154", mysql_error());
			}
			
			// Add keyword info to db
			$keywords = explode(", ", $keywords);
			foreach($keywords as $k)
				$keywords[$k] = trim($keywords[$k]);
			
			for($i = 0; $i < count($keywords); $i++)
			{	
				if($keywords[$i] != '')
					$result = mysql_query("INSERT INTO $DB_Photos_Keywords (photoid, keyword) VALUES ('$ROW->id', '$keywords[$i]')", $Link) or queryError("150", mysql_error());
			}
		}
		else
			$message = $sxLang['MessageFileCopyError'];
		@unlink($File);
		
	}
	else
		$message = $sxLang['MessagePhotoResourceError'];
}

$sxTitleAddendum = $sxLang['AdminHeading'];
include("../themes/$THEME_FOLDER/header.php");
?>
  <div class="bodyWrapper">
   <h1 class="sxAdminHeading"><?PHP echo $sxLang['PhotoHeadingAdd']; ?></h1>
   <div class="sxAdminBreadcrumbs"><?PHP if(userAdmin()) { ?><a href="index.php"><?PHP echo $sxLang['AdminBreadcrumb']; ?><?PHP }?><?PHP if(userEditor()) { ?><a href="editor_index.php"><?PHP echo $sxLang['EditorBreadcrumb']; ?><?PHP }?></a> > <a href="photos.php"><?PHP echo $sxLang['PhotoHeadingManager']; ?></a> > <?PHP echo $sxLang['PhotoHeadingAdd']; ?></div>   
   <?PHP echoMessage($message); ?>
   <form action="<?PHP print $PHP_SELF; ?>" method="post" name="sxPhotoAdd" enctype="multipart/form-data">
	<table border="0" cellspacing="3" cellpadding="5">
	  <tr> 
		<td class="sxTdItem"><?PHP echo $sxLang['GeneralItemName']; ?></td>
		<td><input name="name" style="width:200px;" type="text" id="name" /></td>
	  </tr>
	  <tr> 
		<td class="sxTdItem"><?PHP echo $sxLang['GeneralItemFile']; ?></td>
		<td><input type="file" style="width:200px;" name="PhotoUpload" /></td>
	  </tr>
	  <tr> 
		<td class="sxTdItem"><?PHP echo $sxLang['PhotoItemPhotographer']; ?></td>
		<td><input name="photographer" style="width:200px;" type="text" id="photographer" /></td>
	  </tr>
	  <tr> 
		<td class="sxTdItem"><?PHP echo $sxLang['PhotoItemHideRandom']; ?></td>
		<td><input name="hide_random" type="checkbox" id="hide_random" value="1" /></td>
	  </tr>
	  <tr>
		<td class="sxTdItem"><?PHP echo $sxLang['GeneralItemDate']; ?></td>
		<td><div id="calendarForForm"><input type="text" name="date" id="date" readonly="readonly" value="<?PHP echo date("Y-m-d"); ?>" />
			
			<script type="text/javascript" src="../includes/datepicker/dhtmlSuite-config.js"></script>
			<script type="text/javascript" src="../includes/datepicker/dhtmlSuite-common.js"></script>
			<script type="text/javascript" src="../includes/datepicker/dhtmlSuite-calendar.js"></script>
			<script type="text/javascript" src="../includes/datepicker/dhtmlSuite-dragDropSimple.js"></script>
			
			<script type="text/javascript">
			var calendarObjForForm = new DHTMLSuite.calendar({minuteDropDownInterval:1,numberOfRowsInHourDropDown:5,callbackFunctionOnDayClick:'getDateFromCalendar',isDragable:true,displayTimeBar:false}); 

			function pickDate(buttonObj,inputObject)
			{
				calendarObjForForm.setCalendarPositionByHTMLElement(inputObject,0,inputObject.offsetHeight+2);	// Position the calendar right below the form input
				calendarObjForForm.setInitialDateFromInput(inputObject,'yyyy-mm-dd');	// Specify that the calendar should set it's initial date from the value of the input field.
				calendarObjForForm.addHtmlElementReference('myDate',inputObject);	// Adding a reference to this element so that I can pick it up in the getDateFromCalendar below(myInput is a unique key)
				if(calendarObjForForm.isVisible()){
					calendarObjForForm.hide();
				}else{
					calendarObjForForm.resetViewDisplayedMonth();	// This line resets the view back to the inital display, i.e. it displays the inital month and not the month it displayed the last time it was open.
					calendarObjForForm.display();
				}		
			}	
			function getDateFromCalendar(inputArray)
			{
				var references = calendarObjForForm.getHtmlElementReferences(); // Get back reference to form field.
				references.myDate.value = inputArray.year + '-' + inputArray.month + '-' + inputArray.day;
				calendarObjForForm.hide();	
				
			}	
			</script>
			<img alt="<?PHP echo $sxLang['GeneralTooltipPickDate']; ?>" title="<?PHP echo $sxLang['GeneralTooltipPickDate']; ?>" style="cursor:Pointer;cursor:hand;" src="../images/calendar.gif" onclick="pickDate(this,document.sxPhotoAdd.date);" align="absmiddle" /><br /><span class="sxSmallText">(<?PHP echo $sxLang['DateFormatYYYYMMDD']; ?>)</span></div>
		</td>
	  </tr>
	  <tr> 
		<td class="sxTdItem"><?PHP echo $sxLang['GeneralItemDescription']; ?></td>
		<td><textarea name="description" style="width:300px; height:100px;" id="description"></textarea></td>
	  </tr>
	  <tr> 
		<td class="sxTdItem"><?PHP echo $sxLang['PhotoItemAlbums']; ?></td>
		<td><select style="height:150px; width:300px;" name="albums[]" size="6" multiple>
			<?PHP
			$query = "SELECT * FROM $DB_Photos_Albums ORDER BY id DESC";
			$result = mysql_query($query, $Link) or queryError("150", mysql_error());
			while($ROW = mysql_fetch_object($result))
			{
				echo '<option value="' . $ROW->id . '">' . $ROW->name . '</option>';
			}
			?></select><br />
			<span class="sxSmallText">(<?PHP echo $sxLang['PhotoHoldSelections']; ?>)</span></td>
	  </tr>
	  <tr> 
		<td class="sxTdItem"><?PHP echo $sxLang['PhotoItemKeywords']; ?></td>
		<td valign="top"><textarea name="keywords" style="width:300px; height:100px;" id="keywords"></textarea></td>
	  </tr>
	  <tr> 
		<td colspan="2" valign="top" align="right"><input type="submit" name="Submit" value="<?PHP echo $sxLang['PhotoButtonAdd']; ?>" class="sxButton" /></td>
	  </tr>
	</table></form>
  </div>
<?PHP
include("../themes/$THEME_FOLDER/footer.php");
ob_end_flush();
?>
Return current item: SiteX