<?php

   function InStr($String,$Find,$CaseSensitive = false) {
    $i=0;
    while (strlen($String)>=$i) {
     unset($substring);
     if ($CaseSensitive) {
      $Find=strtolower($Find);
      $String=strtolower($String);
     }
     $substring=substr($String,$i,strlen($Find));
     if ($substring==$Find) return true;
     $i++;
    }
    return false;
   } 


$i = $_SERVER["REQUEST_URI"];

if ( $_SERVER[QUERY_STRING] ) {
$i =  $_SERVER[QUERY_STRING];
} else {

}


//if ( InStr($_SERVER["HTTP_REFERER"],"gbatemp.m4d.sm",0) ) {
//
//} else {
//if ($_SERVER["HTTP_REFERER"]) {
//$fpi = fopen("image.log.txt", "a");
//fwrite($fpi, "\nSite = " . $_SERVER["HTTP_REFERER"] . "\n    file = " . $_SERVER["REQUEST_URI"] . "\n   //ip = " . $_SERVER["REMOTE_ADDR"]);
//fclose($fpi);
//}
//}


//if ( $_GET[id] ) {
//$i = $_GET[id];
//} else {
//$i = $_SERVER["REQUEST_URI"];
//}

$type = $_GET[type];
$i = str_replace(".php", "",$i);
$i = str_replace(".png", "",$i);
$i = str_replace(".jpg", "",$i);
$i = str_replace(".gif", "",$i);
$i = str_replace("/nfo/", "",$i);
$i = str_replace("/static/", "",$i);
$i = str_replace("/", "",$i);

$fout=$i;

$hash = "DynamicImage-" . $i . "-" . $type . "-" . $_GET[ratio] . "-" . $percent;

  if (ereg($hash, str_replace("'","",stripslashes($_SERVER['HTTP_IF_NONE_MATCH']))) )
  {
   header("ETag: \"{$hash}\"");
header('HTTP/1.1 304 Not Modified');
exit();
  }
   header("ETag: \"{$hash}\"");


$ii = $i;

if ( InStr($i,"gt",0) ) {
$logo = imagecreatefromgif("./gbatemp.gif");
$i = str_replace("gt", "",$i);
}

if ( InStr($i,"go",0) ) {
$logo = imagecreatefromgif("./gonline.gif");
$i = str_replace("go", "",$i);
}

$ffile = "/var/www/vhosts/gbatemp.net/httpdocs/img/gba/$i.jpg";


$image = imagecreatefromjpeg($ffile);


$width = ImageSX($image);
$height = ImageSY($image);

$percent = $width / 400;



$newwidth = $width / $percent;
$newheight = $height / $percent;
$thumb = imagecreatetruecolor($newwidth, $newheight);
//$thumb = imagecreate($newwidth, $newheight);

imagecopyresized($thumb, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);



imagecopymerge($thumb,$logo,$newwidth - ImageSX($logo), 0,0,0,ImageSX($logo),ImageSY($logo),100);



//if ( InStr($ii,"gt",0) ) {
//$logo2 = imagecreatefromgif("./gbatemp2.gif");
//imagecopymerge($thumb,$logo2,$newwidth - ImageSX($logo2) - 10, $newheight - ImageSY($logo2) - 10 ,0,0,ImageSX($logo2),ImageSY($logo2),60);
//}

header("Content-Type: image/png");
header("Expires: Fri, 30 Oct 2005 14:19:41 GMT");
header("Cache-Control: max-age=3600000, must-revalidate");
imagepng($thumb);
imagepng($thumb,"static/".$fout.".png");
?>
