<?php
define("CANVASEDGE", 2000);
define("SRCEDGE", 480);
define("MARGIN", 20);
define("FRAME_PATH", "frames/");
define("MAT_PATH", "../media/wysiwyg/option_preview/");

$im = imagecreatetruecolor(CANVASEDGE, CANVASEDGE);
$white = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, CANVASEDGE, CANVASEDGE, $white);

$imageName=$_GET['imageName'];
$frame_name=$_GET['frame'];
$longEdge=$_GET['longedge'];
//$mat=$_GET['mat'];
$padding=$_GET['padding'];

$frame_path=FRAME_PATH.$frame_name."/";
$frame_config=file_get_contents(($frame_path."config.txt"));
$config=explode (";",$frame_config);
$frame_width=floatval($config[0]);
//$padding=floatval($config[1]);


$src_path="images/".$imageName;


$src_image=imagecreatefromstring(file_get_contents($src_path));
$src_width=imagesx($src_image);  //get original width
$src_height=imagesy($src_image);  //get original height
$src_ratio=round($src_width/$src_height,3);

if($src_ratio>=1){
    $inch=(CANVASEDGE-MARGIN*2)/($longEdge+2*$frame_width+2*$padding);
    $padding=round($padding*$inch);
    $frame_width=round($frame_width*$inch);
    resizeWidth($src_image,($longEdge*$inch));
    $src_width=imagesx($src_image); 
    $src_height=imagesy($src_image); 

} else
{
    $inch=(CANVASEDGE-MARGIN*2)/($longEdge+2*$frame_width+2*$padding);
    $padding=round($padding*$inch);
    $frame_width=round($frame_width*$inch);
    resizeHeight($src_image,($longEdge*$inch));
    $src_width=imagesx($src_image);  //get original width
    $src_height=imagesy($src_image);  
}

$x= round((CANVASEDGE/2)-($src_width/2));
$y= round((CANVASEDGE/2)-($src_height/2));



$corner1=imagecreatefromstring(file_get_contents(($frame_path."corner1.jpg")));
$corner2=imagecreatefromstring(file_get_contents(($frame_path."corner2.jpg")));
$corner3=imagecreatefromstring(file_get_contents(($frame_path."corner3.jpg")));
$corner4=imagecreatefromstring(file_get_contents(($frame_path."corner4.jpg")));
$top_bar=imagecreatefromstring(file_get_contents(($frame_path."top.jpg")));
$left_bar=imagecreatefromstring(file_get_contents(($frame_path."left.jpg")));
resizeWidth($corner1,$frame_width);
resizeWidth($corner2,$frame_width);
resizeWidth($corner3,$frame_width);
resizeWidth($corner4,$frame_width);
resizeWidth($left_bar,$frame_width);
resizeHeight($top_bar,$frame_width);

//mat
if($mat!="")
{
    $mat_path=MAT_PATH.$mat.".jpg";
    $src_mat=imagecreatefromjpeg($mat_path);
    imagesettile($im, $src_mat);
    imagefilledrectangle($im, ($x-$padding), ($y-$padding), ($x+$src_width+$padding), ($y+$src_height+$padding), IMG_COLOR_TILED);
    imagedestroy($src_mat);
}


//Image
imagecopyresampled($im, $src_image, $x, $y, 0, 0, $src_width, $src_height, $src_width, $src_height);
//Four corners
imagecopyresampled($im,$corner1,($x-$padding-$frame_width),($y-$padding-$frame_width),0,0,$frame_width,$frame_width,$frame_width,$frame_width);
imagecopyresampled($im,$corner2,($x+$src_width+$padding),($y-$padding-$frame_width),0,0,$frame_width,$frame_width,$frame_width,$frame_width);
imagecopyresampled($im,$corner3,($x+$src_width+$padding),($y+$src_height+$padding),0,0,$frame_width,$frame_width,$frame_width,$frame_width);
imagecopyresampled($im,$corner4,($x-$padding-$frame_width),($y+$src_height+$padding),0,0,$frame_width,$frame_width,$frame_width,$frame_width);


//Top and bottom
imagecopyresampled($im, $top_bar, ($x-$padding), ($y-$frame_width-$padding), 0, 0, ($src_width+2*$padding), $frame_width, ($src_width+2*$padding), $frame_width);
//imageflip ( $top_bar, IMG_FLIP_VERTICAL );
flipVertical($top_bar);
imagecopyresampled($im, $top_bar, ($x-$padding), ($y+$src_height+$padding), 0, 0, ($src_width+2*$padding), $frame_width, ($src_width+2*$padding), $frame_width);
//Left and Right
imagecopyresampled($im, $left_bar, ($x-$padding-$frame_width), ($y-$padding), 0, 0, $frame_width, ($src_height+2*$padding), $frame_width, ($src_height+2*$padding));
flipHorizontal($left_bar);
//imageflip ( $left_bar, IMG_FLIP_HORIZONTAL );
imagecopyresampled($im, $left_bar, ($x+$padding+$src_width), ($y-$padding), 0, 0, $frame_width, ($src_height+2*$padding), $frame_width, ($src_height+2*$padding));



/*
//Top and bottom
imagecopyresampled($im, $top_bar, ($x-$padding), ($y-$frame_width-$padding), 0, 0, ($src_width+2*$padding), $frame_width, ($src_width+2*$padding), $frame_width);
imageflip ( $top_bar, IMG_FLIP_VERTICAL );
imagecopyresampled($im, $top_bar, ($x-$padding), ($y+$src_height+$padding), 0, 0, ($src_width+2*$padding), $frame_width, ($src_width+2*$padding), $frame_width);
//Left and Right
imagecopyresampled($im, $left_bar, ($x-$padding-$frame_width), ($y-$padding), 0, 0, $frame_width, ($src_height+2*$padding), $frame_width, ($src_height+2*$padding));
imageflip ( $left_bar, IMG_FLIP_HORIZONTAL );
imagecopyresampled($im, $left_bar, ($x+$padding+$src_width), ($y-$padding), 0, 0, $frame_width, ($src_height+2*$padding), $frame_width, ($src_height+2*$padding));
*/


//output
$output_width=$src_width+2*($padding+$frame_width);
$output_height=$src_height+2*($padding+$frame_width);
$final_image = imagecreatetruecolor($output_width, $output_height);
imagecopyresampled($final_image, $im, 0, 0, ($x-$padding-$frame_width), ($y-$padding-$frame_width), $output_width, $output_height, $output_width, $output_height);
header('Content-Type: image/jpeg');
imagejpeg($final_image);


imagedestroy($final_image);
imagedestroy($src_image);
imagedestroy($final_image);
imagedestroy($im);
imagedestroy($corner1);
imagedestroy($corner2);
imagedestroy($corner3);
imagedestroy($corner4);
imagedestroy($top_bar);
imagedestroy($left_bar);

function resizeWidth(&$image,$final_width)
{
    //$src_image=imagecreatefromstring(file_get_contents($src_path));
    $src_width=imagesx($image);
    $src_height=imagesy($image);
	$final_height = round($final_width * $src_height / $src_width);
	$new_image = imagecreatetruecolor($final_width, $final_height);
	//imagealphablending($new_image, false);	//Preserve Transparency 
	//imagesavealpha ($new_image, true); 		//Preserve Transparency  
	imagecopyresampled($new_image, $image, 0, 0, 0, 0, $final_width, $final_height, $src_width, $src_height);
	$image=$new_image;
    return true;
}

function resizeHeight(&$image,$final_height)
{
    //$src_image=imagecreatefromstring(file_get_contents($src_path));
    $src_width=imagesx($image);
    $src_height=imagesy($image);
	$final_width = round($final_height * $src_width / $src_height);
	$new_image = imagecreatetruecolor($final_width, $final_height);
	//imagealphablending($new_image, false);	//Preserve Transparency 
	//imagesavealpha ($new_image, true); 		//Preserve Transparency  
	imagecopyresampled($new_image, $image, 0, 0, 0, 0, $final_width, $final_height, $src_width, $src_height);
	$image=$new_image;
    return true;
}
function hex2rgb($hex) {
   $hex = str_replace("#", "", $hex);

   if(strlen($hex) == 3) {
      $r = hexdec(substr($hex,0,1).substr($hex,0,1));
      $g = hexdec(substr($hex,1,1).substr($hex,1,1));
      $b = hexdec(substr($hex,2,1).substr($hex,2,1));
   } else {
      $r = hexdec(substr($hex,0,2));
      $g = hexdec(substr($hex,2,2));
      $b = hexdec(substr($hex,4,2));
   }
   $rgb = array($r, $g, $b);
   //return implode(",", $rgb); // returns the rgb values separated by commas
   return $rgb; // returns an array with the rgb values
}
function flipVertical(&$img) {
 $size_x = imagesx($img);
 $size_y = imagesy($img);
 $temp = imagecreatetruecolor($size_x, $size_y);
 $x = imagecopyresampled($temp, $img, 0, 0, 0, ($size_y-1), $size_x, $size_y, $size_x, 0-$size_y);
 if ($x) {
$img = $temp;
 }
 else {
//die("Unable to flip image");
 }
}
function flipHorizontal(&$img) {
 $size_x = imagesx($img);
 $size_y = imagesy($img);
 $temp = imagecreatetruecolor($size_x, $size_y);
 $x = imagecopyresampled($temp, $img, 0, 0, ($size_x-1), 0, $size_x, $size_y, 0-$size_x, $size_y);
 if ($x) {
$img = $temp;
 }
}
?>