<?php
// Давний код. Лень новый писать..
class image {
 

var $image;
var $image_type;
 

function load($filename) {

$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if ( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
} else if ( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} else if ( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($filename);
}
	
}

function save($filename, $image_type=IMAGETYPE_JPEG, $compression=90, $permissions=null) {

if ( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} else if ( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} else if ( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if ($permissions != null) {
chmod($filename,$permissions);
}
	
}

function getWidth() {

return imagesx($this->image);
	
}


function getHeight() {
	
return imagesy($this->image);
	
}
 

function resize($width,$height) {

imagesavealpha($this->image, true);
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
	
}      

// Обрезание фотографии.
function crop($crop = 'square',$percent = false) {
	$w_i = $this->getWidth();
	$h_i = $this->getHeight();
	if($crop == 'square') {
		$min = $w_i;
		if($w_i > $h_i) $min = $h_i;
		$w_o = $h_o = $min;
	} else {
		list($x_o,$y_o,$w_o,$h_o) = $crop;
		if($percent) {
			$w_o *= $w_i / 100;
			$h_o *= $h_i / 100;
			$x_o *= $w_i / 100;
			$y_o *= $h_i / 100;
		}
		if($w_o < 0) $w_o += $w_i;
		$w_o -= $x_o;
		if($h_o < 0) $h_o += $h_i;
		$h_o -= $y_o;
	}
	$new_image = imagecreatetruecolor($w_o, $h_o);
	imagecopy($new_image, $this->image, 0, 0, $x_o, $y_o, $w_o, $h_o);
	$this->image = $new_image;
} 


     
function preview($width, $height, $preview) { 

$x_ratio = $preview / $width; 
$y_ratio = $preview / $height; 
if (($x_ratio * $height) < $preview) { 
$tn_height = ceil($x_ratio * $height); 
$tn_width = $preview; } else { 
$tn_width = ceil($y_ratio * $width); 
$tn_height = $preview;} 
$this->resize($tn_width,$tn_height); 

}

function preview2($width, $height, $preview) { 

$x_ratio = $preview / $width; 
$y_ratio = $preview / $height; 
if (($x_ratio * $height) > $preview) { 
$tn_height = ceil($x_ratio * $height); 
$tn_width = $preview; } else { 
$tn_width = ceil($y_ratio * $width); 
$tn_height = $preview;} 
$this->resize($tn_width,$tn_height); 

}


function copy_add($i, $user = NULL)
{

		$x = imagesx($i);
		$y = imagesy($i);
		
		$white = imagecolorallocate ($i, 255, 255, 255);
		
		$text = $user;
		// Средний коэффициент ника, длинной 32 символа = 6.25! P.S. Погрешность = дох*я наверное. 
		$out_x = ($x >= (strlen($text) * 6.25) ? 3 : $x );
		$font_size = ($x < ((strlen($text) + strlen($_SERVER['HTTP_HOST'])) * 6.25) ? 6 : 8);
		$out_y = ($y - 4);
		
		imagettftext (
		$i,									// Image.
		$font_size,							// Размер шрифта.
		0,									// Угол.
		$out_x,								// Отступ слева.
		$out_y,								// Отступ сверху.
		$white,								// Color. Записан функцией.
		ROOT."template/arial.ttf",			// Сам шрифт.
		$_SERVER['HTTP_HOST']."/".$text		// Имя копирайта.
		);
		//Как то так :D
		return $i;
}
     
}

?>