<?
/*
		$X->IMG->resize($plik, array(
			array('w'=>800, 'h'=>800, 'dir'=>USR.'/zdjecia', 'name'=>'', 'base_res'=>1),
			array('w'=>200, 'h'=>200, 'dir'=>USR.'/zdjecia', 'name'=>'_', 'crop'=>1, 'watermark'=>array('watermark.png','left','bottom',30,10)),
		));
*/

//	function resize($src, $sizes) {
		global $X;

		$ext=$X->FILE->get_ext($src);
		if(!file_exists($src) || !is_file($src) || empty($ext)) return false;
		if(count($sizes)==0) return false;
		$original_file=@array_pop(explode('/',$src));
		if(!@in_array($ext,array('jpg','jpeg','gif','png'))) $ext='jpg';


		if($ext=='jpg' || $ext=='jpeg') {
										$source=@imagecreatefromjpeg($src);
			if($source==false) 	$source=@imagecreatefrompng($src);
			if($source==false) 	$source=@imagecreatefromgif($src);
		}

		if($ext=='gif') {
										$source=@imagecreatefromgif($src);
			if($source==false) 	$source=@imagecreatefrompng($src);
			if($source==false) 	$source=@imagecreatefromjpeg($src);
		}

		if($ext=='png') {
										$source=@imagecreatefrompng($src);
			if($source==false) 	$source=@imagecreatefromjpeg($src);
			if($source==false) 	$source=@imagecreatefromgif($src);
		}
		if($source==false) return false;


		$width=imagesx($source);
		$height=imagesy($source);
		$orientation=($width>=$height ? 'h':'v');
		$proportion=$width/$height;


		foreach($sizes as $resize) {
		/////////
			$to_crop_w = $to_crop_h = 0;
			$new_image_width  = $new_width  = $width;
			$new_image_height = $new_height = $height;
			if(!is_numeric($resize['quality'])) $resize['quality']=85;
			$destination_proportion=$resize['w']/$resize['h'];


			if($resize['crop']==1) {
				if($proportion<$destination_proportion) { //width resize, crop height
					$new_width=$resize['w'];
					$new_height=$new_width/$width;
					$new_height=round($new_height*$height);
					$to_crop_h=round(($new_height-$resize['h'])/2);
				} else { //height resize, crop width
					$new_height=$resize['h'];
					$new_width=$new_height/$height;
					$new_width=round($new_width*$width);
					$to_crop_w=round(($new_width-$resize['w'])/2);
				}
				$new_image_width=$resize['w'];
				$new_image_height=$resize['h'];
			} else {
				if($width>$resize['w'] || $height>$resize['h']) {
					if($width>$resize['w']) {
						$new_width=$resize['w'];
						$new_height=$new_width/$width;
						$new_height=$new_height*$height;
					}

					if($new_height>$resize['h']) {
						$new_height=$resize['h'];
						$new_width=$new_height/$height;
						$new_width=$new_width*$width;
					}
				}

//				(int)$new_width=round($new_width);
//				(int)$new_height=round($new_height);
//				if($new_width+1<$resize['w'] && ($new_width+1/$new_height)<$proportion) $new_width++;
////				if($new_height+1<$resize['h'] && ($new_width/$new_height+1)<$proportion) $new_height++;

//				$new_image_width=$new_width;
//				$new_image_height=$new_height;

				$new_width=$new_image_width=$resize['w'];
				$new_height=$new_image_height=$resize['h'];
			}

			(int)$to_crop_w=($to_crop_w<0 ? 0 : $to_crop_w);
			(int)$to_crop_h=($to_crop_h<0 ? 0 : $to_crop_h);

			$thumb=imagecreatetruecolor($new_image_width, $new_image_height);
			$new_file_name=(!empty($resize['dir']) ? $resize['dir'].'/'.$X->FILE->add_suffix($original_file, $resize['name']) : $X->FILE->add_suffix($src, $resize['name']));
			if(!empty($resize['dir'])) @mkdir($resize['dir'], 0755);

			if(count($resize['watermark'])>0) {
				$add_watermark=true;
				$watermark['src']=imagecreatefrompng($resize['watermark'][0]);
				$watermark['w']=imagesx($watermark['src']);
				$watermark['h']=imagesy($watermark['src']);

				switch($resize['watermark'][1]) {
					default:
					case 'center':	$watermark['left'] = ($new_image_width/2) - ($watermark['w']/2); 									break;
					case 'left':	$watermark['left'] = 0 + (int)$resize['watermark'][3];												break;
					case 'right':	$watermark['left'] = $new_image_width - $watermark['w'] - (int)$resize['watermark'][3]; 	break;
				}
				switch($resize['watermark'][2]) {
					default:
					case 'center':	$watermark['top'] = ($new_image_height/2) - ($watermark['h']/2); 									break;
					case 'top':		$watermark['top'] = 0 + (int)$resize['watermark'][4];													break;
					case 'bottom':	$watermark['top'] = $new_image_height - $watermark['h'] - (int)$resize['watermark'][4];	break;
				}
			}


			switch($ext) {
				case 'jpg':
				case 'jpeg':
					imagecopyresampled($thumb, $source, -$to_crop_w, -$to_crop_h, 0, 0, $new_width, $new_height, $width, $height);
					if($add_watermark==true) imagecopyresampled($thumb, $watermark['src'], $watermark['left'], $watermark['top'], 0, 0, $watermark['w'], $watermark['h'], $watermark['w'], $watermark['h']);
					imagejpeg($thumb, $new_file_name, $resize['quality']);
				break;

				case 'gif':
					$gif_info = $X->IMG->GIF_info($src);
					if($gif_info['version']=='89a' && $gif_info['color_flag']==1) {
						$transparent=imagecolorallocate($thumb, $gif_info['transparent_r'], $gif_info['transparent_g'], $gif_info['transparent_b']);
						imagecolortransparent($thumb, $transparent);
					}
					imagecopyresampled($thumb, $source, -$to_crop_w, -$to_crop_h, 0, 0, $new_width, $new_height, $width, $height);
					if($add_watermark==true) imagecopyresampled($thumb, $watermark['src'], $watermark['left'], $watermark['top'], 0, 0, $watermark['w'], $watermark['h'], $watermark['w'], $watermark['h']);
					imagegif($thumb, $new_file_name);
				break;

				case 'png':
					imagealphablending($thumb, false);
					imagesavealpha($thumb,true);
					$transparent = imagecolorallocatealpha($thumb, 255, 255, 255, 127);
					imagefilledrectangle($thumb, 0, 0, $new_width, $new_height, $transparent);
					imagecopyresampled($thumb, $source, -$to_crop_w, -$to_crop_h, 0, 0, $new_width, $new_height, $width, $height);
					if($add_watermark==true) imagecopyresampled($thumb, $watermark['src'], $watermark['left'], $watermark['top'], 0, 0, $watermark['w'], $watermark['h'], $watermark['w'], $watermark['h']);
					imagepng($thumb, $new_file_name);
				break;

				default:
					return false;
				break;
			}
			@chmod($new_file_name, 0644);

			if($resize['base_res']==1) {
				$source=$thumb;
				$width=imagesx($source);
				$height=imagesy($source);
			}

		/////////
		}

		unset($thumb);
		unset($source);
		unset($watermark);

		return true;
//	}
?>