<?
header("Content-Type: image/png");

$MAX_CACHE_ENTRIES = 16;
$download_method = "fopen"; //wget or fopen
$protect_resources = FALSE;

$masksrc="mask.png";

$filename = time();
$imageURL = $_GET["src"];
$cacheEntry = "cdcase/" . md5($imageURL . $currentSkin . $currentType) . ".png";
$src_url = parse_url($_SERVER['HTTP_REFERER']);

if (($src_url[host] != $_SERVER['SERVER_NAME'] && $protect_resources == TRUE)) {
  $image = imagecreatefrompng("nodisplay.png");
  imagepng($image);
  imagedestroy($image);
  exit();	
}

if (file_exists($cacheEntry)) {
  $image = imagecreatefrompng($cacheEntry);
  imagepng($image);
  imagedestroy($image);
  exit();
}

function ls($path, $filter) {
  $thisDir = dir($path);
  while ($file = $thisDir->read()) {
    if (preg_match($filter, $file)) {
      $resultArray[] = $file;
    }
  }
  return $resultArray;
}

function imageflip($imgsrc) {
  $width = imagesx($imgsrc);
  $height = imagesy($imgsrc);

  $imgdest = imagecreatetruecolor($width, $height);

  for ($y=0; $y < $height; $y++) {
    imagecopy($imgdest, $imgsrc, 0, $height-$y-1, 0, $y, $width, 1);
  }

  return $imgdest;
}

$jewel = imagecreatetruecolor(262, 466);

if (substr($imageURL, -3) == "jpg") {
  $header = "jpeg";
} else {
  $header = substr($imageURL, -3);
}

$cacheEntries = ls("cdcase", "/.+\\.png/");
if (count($cacheEntries) >= $MAX_CACHE_ENTRIES) {
  $oldest = 0xFFFFFFFF;
  $unlinkEntry = "";
  foreach ($cacheEntries as $entry) {
    $entry = "cdcase/" . $entry;
    $t = filemtime($entry);
    if ($t < $oldest) {
      $oldest = $t;
      $unlinkEntry = $entry;
    }
  }
  unlink($unlinkEntry);
}
	
if ($download_method == "fopen") {
	$fileOut = fopen($filename . "." . $header, "w+b");
	$fileIn = fopen($imageURL, "rb");
	$contents = ""; 
	do { 
  	$data = fread($fileIn, 32768); 
  	if (strlen($data) == 0) { 
    	break; 
  	} 
  	fputs($fileOut, $data);
	} while(true); 
	fclose($fileIn);
	fclose($fileOut);
} else {
	shell_exec("wget -O " . $filename . "." . $header . " " . $imageURL);
}

if ($header=="jpeg") { 
	$cover = imagecreatefromjpeg($filename . "." . $header); 
} else if ($header=="gif") { 
	$cover = imagecreatefromgif($filename . "." . $header); 
} else { 
	$cover = imagecreatefrompng($filename . "." . $header); 
}

$stitch = imagecreatefrompng("stitch.png");
$glints = imagecreatefrompng("reflections.png");
$case = imagecreatefrompng("cd_case.png");
$mask = imagecreatefrompng($masksrc);

imagecopyresampled($jewel, $cover, 19, 3, 0, 0, 240, 227, imagesx($cover), imagesy($cover));
imagecopy($jewel, $stitch, 19, 3, 0, 0, imagesx($stitch), imagesy($stitch));
imagecopy($jewel, $glints, 0, 0, 0, 0, imagesx($glints), imagesy($glints));
imagecopy($jewel, $case, 0, 0, 0, 0, imagesx($case), imagesy($case));
imagecopy($jewel, imageflip($jewel), 0, 233, 0, 233, 262, 233);
imagecopy($jewel, $mask, 0, 233, 0, 0, imagesx($mask), imagesy($mask));

imagedestroy($stitch);
imagedestroy($glints);
imagedestroy($case);
imagedestroy($mask);

$final = imagecreatetruecolor(262, 320);
imagecopy($final, $jewel, 0, 0, 0, 0, 262, 320);
imagepng($final);
imagepng($final, $cacheEntry);

imagedestroy($jewel);
imagedestroy($final);

unlink($filename . "." . $header);
?>