<?php
# Script by seg0ro http://mobilarts.ru
# Not for sale!!!

define('_IN_JOHNCMS', 1);

function format($name){
  return array_pop(explode(".", $name));
}

$file = isset($_GET['image']) ? '../files/forum/'.htmlspecialchars($_GET['image']) : false;
$razm = 50;

if ($file){
  list($width, $height, $type) = getimagesize($file);
	switch ($type){
		case 1: $att_ext = 'gif' ;break;
		case 2: $att_ext = 'jpeg'; break;
		case 3: $att_ext = 'png'; break;
		case 4: $att_ext = 'jpg'; break;
	}
  
  if ($att_ext){
    $sizs = GetImageSize($file);
    if ($sizs){
      $width = $sizs[0];
      $height = $sizs[1];
      $x_ratio = $razm / $width;
      $y_ratio = $razm / $height;
      if (($width <= $razm) && ($height <= $razm)){
        $tn_width = $width;
        $tn_height = $height;
      }else if (($x_ratio * $height) < $razm){
        $tn_height = ceil($x_ratio * $height);
        $tn_width = $razm;
      }else{
        $tn_width = ceil($y_ratio * $width);
        $tn_height = $razm;
      }
			
			$function = 'imageCreateFrom'.$att_ext;
			$image = $function($file);
			$tmp = imageCreateTrueColor($tn_width, $tn_height);
			if($att_ext == 'png') {
				imageAlphaBlending($tmp, false);
				imageSaveAlpha($tmp, true);
			} else if($att_ext == 'gif'){
				imagecolortransparent($tmp, $color);
			}
			imageCopyResampled($tmp, $image, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
			ob_start();
			switch($att_ext){
				case 'jpg':
				case 'jpeg':
					@imagejpeg($tmp, null, 90);
				break;
				case 'gif':
					@imagegif($tmp, null);
				break;
				case 'png':
					@imagepng($tmp, null, 9);
				break;
				default:
					// *** No extension - No save.
				break;
			}
			imageDestroy($tmp);
			imageDestroy($image);
			header("Content-Type: image/".$att_ext);
			header('Content-Disposition: inline; filename=image.'.$att_ext);
			header('Content-Length: ' . ob_get_length());
			ob_end_flush();
    }
  }
}