<?php
	function Resample(&$img, $owdt, $ohgt, $maxwdt, $maxhgt, $quality=1) {
	  if(!$maxwdt) $divwdt=0;
	   else $divwdt=Max(1,$owdt/$maxwdt);

	  if(!$maxhgt) $divhgt=0;
	   else $divhgt=Max(1,$ohgt/$maxhgt);

	  if($divwdt>=$divhgt) {
	   $newwdt=$maxwdt;
	   $newhgt=round($ohgt/$divwdt);
	  } else {
	   $newhgt=$maxhgt;
	   $newwdt=round($owdt/$divhgt);
	  }

	   $tn=imagecreatetruecolor($newwdt,$newhgt);
	   if($quality)
	       imagecopyresampled($tn,$img,0,0,0,0,$newwdt,$newhgt,$owdt,$ohgt);
	   else
	       imagecopyresized($tn,$img,0,0,0,0,$newwdt,$newhgt,$owdt,$ohgt);

	   imagedestroy($img);

	   $img = $tn;

	   return array($newwdt, $newhgt);
	}

	//Get any passed variables
	$imageFile = $_GET["image"];
	$frameFolder = $_GET["frameFolder"];
	$size = strtoupper($_GET["size"]);
	$framePixelsPerInch = $_GET["pixPerInch"];


	//Get the image
	$paintingFileName = $imageFile;
	$paintingDimensions = getimagesize($paintingFileName);

	$painting = imagecreatefromjpeg("./".$paintingFileName);

	//Determine the dimensions
	$paintingWidth=$paintingDimensions[0];
	$paintingHeight=$paintingDimensions[1];

	//Determine the pixels per inch
	$paintingLongestSide = $paintingWidth;
	if($paintingHeight>$paintingLongestSide)
	{
		$paintingLongestSide = $paintingHeight;
	}

	$paintingPixelsPerInch = 0;

	switch ($size) {
		case 'A':
			$paintingPixelsPerInch = $paintingLongestSide/10;
			break;
		case 'B':
			$paintingPixelsPerInch = $paintingLongestSide/16;
			break;
		case 'C':
			$paintingPixelsPerInch = $paintingLongestSide/20;
			break;
		case 'D':
			$paintingPixelsPerInch = $paintingLongestSide/24;
			break;
		case 'E':
			$paintingPixelsPerInch = $paintingLongestSide/36;
			break;
		case 'F':
			$paintingPixelsPerInch = $paintingLongestSide/40;
			break;
		case 'G':
			$paintingPixelsPerInch = $paintingLongestSide/48;
			break;
	}

	$frameResizeRatio = $paintingPixelsPerInch / $framePixelsPerInch;

	//Get the frame
	$frameFileTop = $frameFolder."/top.jpg";
	$frameFileTopLeft =  $frameFolder."/topLeft.jpg";
	
	list($width, $height) = getimagesize($frameFileTop);
	$topFrameWidth=$width;
	$topFrameHeight=$height;
	$sideFrameWidth=$height;
	$sideFrameHeight=$width;
	
	$oframeTop = imagecreatefromjpeg($frameFileTop);
	$oframeTopLeft = imagecreatefromjpeg($frameFileTopLeft);
	
	$oframeBottom = imagerotate($oframeTop, 180, 0);
	$oframeLeft = imagerotate($oframeTop, 90, -1);
	$oframeRight = imagerotate($oframeTop, 270, -1);
	
	$oframeTopRight = imagerotate($oframeTopLeft, 270, 0);
	$oframeBottomLeft = imagerotate($oframeTopLeft, 90, 0);
	$oframeBottomRight = imagerotate($oframeTopLeft, 180, 0);
	
	$frameTop = $oframeTop;
	$frameBottom = $oframeBottom;
	$frameLeft =$oframeLeft;
	$frameRight = $oframeRight;

	$frameTopLeft = $oframeTopLeft;
	$frameTopRight = $oframeTopRight;
	$frameBottomLeft = $oframeBottomLeft;
	$frameBottomRight = $oframeBottomRight;
	

	//Adjust the size of the frame graphics
	$newwidth = $topFrameWidth * $frameResizeRatio;
	$newheight = $topFrameHeight * $frameResizeRatio;
	$frameTop = imagecreatetruecolor($newwidth, $newheight);
	imagecopyresized($frameTop, $oframeTop, 0, 0, 0, 0, $newwidth, $newheight, $topFrameWidth, $topFrameHeight);
	
	$newwidth = $topFrameWidth * $frameResizeRatio;
	$newheight = $topFrameHeight * $frameResizeRatio;
	$frameBottom = imagecreatetruecolor($newwidth, $newheight);
	imagecopyresized($frameBottom, $oframeBottom, 0, 0, 0, 0, $newwidth, $newheight, $topFrameWidth, $topFrameHeight);
	
	$newwidth = $sideFrameWidth * $frameResizeRatio;
	$newheight = $sideFrameHeight * $frameResizeRatio;
	$frameLeft = imagecreatetruecolor($newwidth, $newheight);
	imagecopyresized($frameLeft, $oframeLeft, 0, 0, 0, 0, $newwidth, $newheight, $sideFrameWidth, $sideFrameHeight);
		
	$newwidth = $sideFrameWidth * $frameResizeRatio;
	$newheight = $sideFrameHeight * $frameResizeRatio;
	$frameRight = imagecreatetruecolor($newwidth, $newheight);
	imagecopyresized($frameRight, $oframeRight, 0, 0, 0, 0, $newwidth, $newheight, $sideFrameWidth, $sideFrameHeight);
	
	list($width, $height) = getimagesize($frameFileTopLeft);
	$newwidth = $width * $frameResizeRatio;
	$newheight = $height * $frameResizeRatio;
	$frameTopRight = imagecreatetruecolor($newwidth, $newheight);
	imagecopyresized($frameTopRight, $oframeTopRight, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

	list($width, $height) = getimagesize($frameFileTopLeft);
	$newwidth = $width * $frameResizeRatio;
	$newheight = $height * $frameResizeRatio;
	$frameTopLeft = imagecreatetruecolor($newwidth, $newheight);
	imagecopyresized($frameTopLeft, $oframeTopLeft, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

	list($width, $height) = getimagesize($frameFileTopLeft);
	$newwidth = $width * $frameResizeRatio;
	$newheight = $height * $frameResizeRatio;
	$frameBottomRight = imagecreatetruecolor($newwidth, $newheight);
	imagecopyresized($frameBottomRight, $oframeBottomRight, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

	list($width, $height) = getimagesize($frameFileTopLeft);
	$newwidth = $width * $frameResizeRatio;
	$newheight = $height * $frameResizeRatio;
	$frameBottomLeft = imagecreatetruecolor($newwidth, $newheight);
	imagecopyresized($frameBottomLeft, $oframeBottomLeft, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

	//Determine the dimensions
	$frameCornerDimensions = getimagesize($frameFileTopLeft);
	$frameTopDimensions = getimagesize($frameFileTop);

	$frameCornerWidth = $frameCornerDimensions[0];
	$frameCornerHeight = $frameCornerDimensions[1];
	$frameTopWidth = $frameTopDimensions[0];
	$frameTopHeight = $frameTopDimensions[1];
	$frameSideWidth = $sideFrameWidth;
	$frameSideHeight = $sideFrameHeight;

	$frameCornerWidth = $frameCornerWidth * $frameResizeRatio;
	$frameCornerHeight = $frameCornerHeight * $frameResizeRatio;
	$frameTopWidth = $frameTopWidth * $frameResizeRatio;
	$frameTopHeight = $frameTopHeight * $frameResizeRatio;
	$frameSideWidth = $frameSideWidth * $frameResizeRatio;
	$frameSideHeight = $frameSideHeight * $frameResizeRatio;

	$frameWidth = $frameSideWidth;
	if($frameCornerWidth > $frameWidth)
	{
		$frameWidth = $frameCornerWidth;
	}

	$frameHeight = $frameTopHeight;
	if($frameCornerHeight > $frameHeight);
	{
		$frameHeight = $frameCornerHeight;
	}

	//Create the canvas
	$canvasWidth = $frameWidth + $frameWidth + $paintingWidth;
	$canvasHeight = $frameHeight + $frameHeight + $paintingHeight;

	$canvas = imagecreatetruecolor($canvasWidth, $canvasHeight);
	//$canvas = imagecreate(0,0);

	$white = imagecolorallocate($canvas, 255,255,255);

	imageFill($canvas, 0, 0, $white);


	//Add the painting
	imagealphablending($canvas, false);
	imagesavealpha($canvas, true);
	imagecopy($canvas, $painting, $frameWidth, $frameHeight, 0, 0, $paintingWidth, $paintingHeight);

	//Add the frame
	//top
	$start = $frameCornerWidth;
	while($start<($frameCornerWidth+$paintingWidth))
	{
	imagecopy($canvas, $frameTop, $start , 0, 0, 0, $frameTopWidth, $frameTopHeight);
	$start += ($frameTopWidth-1);
	}

	//bottom
	$start = $frameCornerWidth;
	while($start<($frameCornerWidth+$paintingWidth))
	{
	imagecopy($canvas, $frameBottom, $start , $paintingHeight+$frameTopHeight, 0, 0, $frameTopWidth, $frameTopHeight);
	$start += ($frameTopWidth-1);
	}

	//left
	$start = $frameCornerHeight;
	while($start<($frameCornerHeight+$paintingHeight))
	{
	imagecopy($canvas, $frameLeft, 0, $start , 0, 0, $frameSideWidth, $frameSideHeight);
	$start += ($frameSideHeight-1);
	}

	//right
	$start = $frameCornerHeight;
	while($start<($frameCornerHeight+$paintingHeight))
	{
	imagecopy($canvas, $frameRight, $paintingWidth+$frameSideWidth, $start, 0, 0, $frameSideWidth, $frameSideHeight);
	$start += ($frameSideHeight-1);
	}

	imagecopy($canvas, $frameTopLeft, 0 , 0, 0, 0, $frameCornerWidth, $frameCornerHeight);
	imagecopy($canvas, $frameTopRight, $paintingWidth+$frameCornerWidth , 0, 0, 0, $frameCornerWidth, $frameCornerHeight);
	imagecopy($canvas, $frameBottomLeft, 0 , $paintingHeight+$frameCornerHeight, 0, 0, $frameCornerWidth, $frameCornerHeight);
	imagecopy($canvas, $frameBottomRight, $paintingWidth+$frameCornerWidth , $paintingHeight+$frameCornerHeight, 0, 0, $frameCornerWidth, $frameCornerHeight);

    // Does this directive cause the aspect ratio to go wrong?
	//Added this line to limit the image to a max size of 250px;
	Resample($canvas, $canvasWidth, $canvasHeight, 500, 500);

	//Generate the output
	header("Content-type: image/jpeg");

	imagejpeg($canvas, NULL, 50);

	imagedestroy($canvas);
?>
