<?php
// functies om een image (foto) met caption (onderschrift) in een tabel in de content te plaatsen
// de classes "photo" en "fotoRight" || "fotoLeft" zit in CSS;
// syntax: ftoRight || ftoLeft (bestandnaam van nde image, alttekst, foto-onderschrift, 
//											link, doelframe, schalingsfactor in %)

// Rechts lijnend
function ftoRight($file, $alt, $caption, $link, $target, $scale, $copyright) {
	echo (ftoTable($file, $alt, $caption, $link, $target, $scale, $copyright, "Right"));
}

// Links lijnend
function ftoLeft($file, $alt, $caption, $link, $target, $scale, $copyright) {
  echo (ftoTable($file, $alt, $caption, $link, $target, $scale, $copyright, "Left"));
}

// subroutine van ftoRight() en ftoLeft() 
// returnt de echo-string - lege string indien fataal
function ftoTable($file, $alt, $caption, $link, $target, $scale, $copyright, $side) {

// twee locale waarden: tabelheader en footer
// tabel moet van bovenaf en van onderaf worden opgebouwd,
// want <img> tag valt binnen de <a href> tag
$hd = "";
$ft = "";


	// controle op valide bestand-pad
	if (!file_exists($file)) {
		// file bestaat niet zoals in parameter is meegegeven;
		// misschien in default images map (custom aanpasbaar)
		$file = "images/" . $file;
	}
		
	// nu moet het bestand te vinden zijn, anders pech
	if (file_exists($file)) {
		// file bestaat
		// nu info onttrekken aan de inmage, in dit geval width en height
		$imginfo = getimagesize($file);
      $iw=$imginfo[0];
		$ih=$imginfo[1];
		
		// controle of deze waarden valide zijn (niet 0)
		if (!empty($iw) and !empty($ih)) {
			// width en heigt zijn bekend; nu eventueel foto schalen
			if (is_int($scale) and ($scale > 0)) {
				$iw = (($iw * $scale) / 100);
				$ih = (($ih * $scale) / 100);
			}

			// header en footer variabelen vullen met vaste table elementen
			// newlines (\n) en tabs (\t) zijn niet noodzakelijk maar maakt de html-code beter leesbaar
			$hd = "\n\t\t<table witdh='" . $iw . "' class='foto" . $side . "'><tr><td width='" . $iw . "'>\n\t\t";
			$ft = "\n\t\t</td></tr></table>\n\n";

			// copyright meegegeven, dan footer verder vullen
			if (!empty($copyright)) {
				$ft = "\n\t\t</td></tr><tr><td width='" . $iw . "' class='copyright'>\n\t\t" . $copyright . $ft;
			}
			
			// is er een caption? want dan moet de footer verder gevuld worden
			// caption komt in tablecel onder de foto
			if (!empty($caption)) {
				// caption is meegegeven
				// kijken of de caption gelijk moet zijn aan de alt-tekst
				if (!empty($alt) and ($caption == "=")) {
					$caption = $alt;
				}
				
				// caption en table elementen toevoegen aan footer variabele
				$ft = "\n\t\t</td></tr><tr><td width='" . $iw . "'>\n\t\t" . $caption . $ft;
			}

			// is er een link? want dan moet dat nu eerst (nog vóór de <img> tag)
			if (!empty($link)) {
				// <a href> tag maken in header en footer
				$hd = $hd . "<a href='" . $link . "'";
				$ft = "\n\t\t</a>" . $ft;

				// target meegegegven?
				if (!empty($target)) {
					 $hd = $hd . " target='" . $target . "'>\n\t\t";
				} else {
					 $hd = $hd . ">\n\t\t";
				}
				
			} // link ok
				
			// nu de <img> tag; wordt hier aan header var geplakt
			// (kan ook in de footer var, maar dan string anders concentaneren)
			$hd = $hd ."<img class='photo' src='" . $file . "' width='" . $iw . "' height='" . $ih . "' border='0' alt='";
				
			// alttekst toevoegen?
			if (!empty($alt)) {
				$hd = $hd . $alt . "' />";
			} else {
				$hd = $hd . "' />";
			}
		
		} // width + height ok (!empty($iw) and !empty($ih))
	} // file ok (file_exists($file))
	
	return ($hd . $ft);
}
?>
