<?php

  ini_set('include_path',"/usr/web/sites/tuve.tv/include/");
  include("site.inc");

  $uploaddir = '/usr/web/sites/ubixonline.com/docroot/images/profile/';
  $file2 = $_GET['oid'] . ".png";

  switch($_FILES['Filedata']['type']) {
    case 'image/jpeg':
    case 'image/pjpeg':
      $orig = imagecreatefromjpeg($_FILES['Filedata']['tmp_name']);
      break;
    case 'image/png':
      $orig = imagecreatefrompng($_FILES['Filedata']['tmp_name']);
      break;
    case 'image/gif':
      $orig = imagecreatefromgif($_FILES['Filedata']['tmp_name']);
      break;
    default:
      $iType = exif_imagetype($_FILES['Filedata']['tmp_name']);
      switch ($iType) {
        case 1:
          $orig = imagecreatefromgif($_FILES['Filedata']['tmp_name']);
          break;
        case 2:
          $orig = imagecreatefromjpeg($_FILES['Filedata']['tmp_name']);
          break;
        case 3:
          $orig = imagecreatefrompng($_FILES['Filedata']['tmp_name']);
          break;
        default:
          system("echo \"Type: $iType\" >> /tmp/boob");
        }
      }

  if ($orig) {
    $orig_x = imagesx($orig);
    $orig_y = imagesy($orig);

    if ($orig_x > $orig_y) {
      if (($orig_x <= 300) && ($orig_y <= 225)) {
        $thumb_x_l = $orig_x;
        $thumb_y_l = $orig_y;
        }
      else {
        $thumb_x_l = 300;
        $thumb_y_l = round(($orig_y * $thumb_x_l) / $orig_x);
        }

      $thumb_x_m = 200;
      $thumb_y_m = round(($orig_y * $thumb_x_m) / $orig_x);

      $thumb_x_s = 100;
      $thumb_y_s = round(($orig_y * $thumb_x_s) / $orig_x);
      }
    else {
      if (($orig_x <= 300) && ($orig_y <= 225)) {
        $thumb_x_l = $orig_x;
        $thumb_y_l = $orig_y;
        }
      else {
        $thumb_y_l = 300;
        $thumb_x_l = round(($orig_x * $thumb_y_l) / $orig_y);
        }

      $thumb_y_m = 200;
      $thumb_x_m = round(($orig_x * $thumb_y_m) / $orig_y);

      $thumb_y_s = 100;
      $thumb_x_s = round(($orig_x * $thumb_y_s) / $orig_y);
      }

   
    $thumb = imagecreatetruecolor($thumb_x_l,$thumb_y_l);
    imagecopyresampled($thumb, $orig, 0, 0, 0, 0, $thumb_x_l, $thumb_y_l, $orig_x, $orig_y);
    imagepng($thumb,$uploaddir . $file2);

    $thumb = imagecreatetruecolor($thumb_x_m,$thumb_y_m);
    imagecopyresampled($thumb, $orig, 0, 0, 0, 0, $thumb_x_m, $thumb_y_m, $orig_x, $orig_y);
    imagepng($thumb,$uploaddir . $file2 . "-med.png");

    $thumb = imagecreatetruecolor($thumb_x_s,$thumb_y_s);
    imagecopyresampled($thumb, $orig, 0, 0, 0, 0, $thumb_x_s, $thumb_y_s, $orig_x, $orig_y);
    imagepng($thumb,$uploaddir . $file2 . "-small.png");

    $oid = $_GET['oid'];
    $query = "UPDATE users SET main_image = 'http://www.ubixonline.com/images/profile/$file2' WHERE uid = $oid";
    mysql_query($query);
    }
  else {
    return(false);
    }
?>
