<?php
if(!defined('MOD'))  die("access die");
$actions = trim( $_GET['actions']) ? trim( $_GET['actions']) : 'manage';
$action = trim( $_GET['action']) ? trim( $_GET['action']) : 'manage';
require_once(R_P . "/include/class.resizeimage.php");
switch( $actions)
{
	case 'manage':
		$channelid = intval($_GET['channelid']);
		$pic_width = intval($channel[$channelid]['pic_width']);
		$pic_height = intval($channel[$channelid]['pic_height']);
		$pic_width || $pic_width = 140;
		$pic_height || $pic_height = 90;
		$imageName = $_GET["imageName"];
		if ( strpos( $imageName, W_P) !== false){
			$imageName = "../".str_replace(W_P, '', $imageName);
		}else if ( strpos( $imageName, '/') === 0){
			$imageName = "../".substr($imageName, strlen($_SET['webpath']));
		}else if ( strpos( $imageName, '../') === 0){
			
		}else{
			$imageName = "../".$imageName;
		}
		$data = array('status'=>200, 'img'=>'', 'width'=>0, 'height'=>0);
		if(is_file($imageName)){
			$info = @getimagesize($imageName);
			if($info !== false){
				$ext = array_pop(explode('/',$imageName));
				$editName = "../cache/temp/" . $ext;
				@copy( $imageName, $editName);
				$data = array(
					'status'=>200,
					'oldimg'=>$imageName,
					'img'=>$editName,
					'width'=> $info[0],
					'height'=> $info[1]
				);
			}
		}
		include qbase::template($path . 'image_editor');
	break;	

	case 'getImage':
		$imageName = $_GET["imageName"];
		if(empty($imageName) || !file_exists($imageName)) {  $data = "{imageFound:false}"; header("x-json: " .  $data); exit;  }
		ob_clean();
		header("Location:" . $imageName);
	break;

	case 'processImage':
		$imageName = $_POST["imageName"];
		$imageOldName = $_POST["imageOldName"];
		$action   = $_GET["action"];
		$editNameDir = "../cache/temp";
		if ( !is_dir( $editNameDir)) qbase::createdir( $editNameDir);
		if( !file_exists($imageName)) {
			$data = array('status'=>404); 
			echo qbase::xmlPrint(qbase::jsonencode($data));
			exit;  
		}
		switch($action)
		{
			case "viewActive":
				@copy($imageOldName, $imageName);
			break;

			case "save":
				$type = intval($_POST['type']);
				$obj = new resizeimage($imageName);
				$obj->init();
				$out = imagecreatetruecolor($obj->width, $obj->height);
				imagecopyresampled($out, $obj->im, 0, 0, 0, 0, $obj->width, $obj->height, $obj->width, $obj->height);
				if($type == 0){
					$obj->output($out, $imageOldName, 70);
					$obj->close();
					imagedestroy($out);
					@unlink($imageName);
					$data = array('status'=>200, 'type'=>0);
				}else{
					require_once(R_P . '/include/inc.editor.php');
					$ext = qbase::fileext($imageOldName);
					$filename = dirname($imageOldName) .'/'. GetRndFileName($ext);
					$obj->output($out, $filename, 70);
					$obj->close();
					imagedestroy($out);
					$data = array('status'=>200, 'type'=>1, 'img'=>$filename);
				}
				clearstatcache();
				echo qbase::xmlPrint(qbase::jsonencode($data));
				exit();
			break;

			case "resize":
				$out_w = intval($_POST["width"]);
				$out_h =  intval($_POST["height"]);
				if (!is_numeric($out_w) || $out_w < 1 || $out_w > 2000 || !is_numeric($out_h) || $out_h < 1 || $out_h > 2000) { exit; }
				$obj = new resizeimage($imageName);
				$obj->init();
				$obj->resize($out_w, $out_h, 100);
			break;

			case "rotate":
				$degrees = intval($_POST["degrees"]);
				if (($degrees != 90 && $degrees != 180 && $degrees != 270)) { exit; }
				$obj = new resizeimage($imageName);
				$obj->init();
				$obj->rotate($degrees, 100);
			break;

			case "crop": // additional required params: x, y, w, h
				$param = explode(',', $_POST['param']);
				$x = intval($param[0]);
				$y = intval($param[1]);
				$w = intval($param[2]);
				$h = intval($param[3]);
				if (!is_numeric($x) || !is_numeric($y) || !is_numeric($w) || !is_numeric($h)) { exit; }
				$obj = new resizeimage($imageName);
				$obj->init();
				$obj->crop($x, $y, $w, $h, 100);
			break;

			case 'border':
				$color = trim($_POST["color"]);
				$size  = intval($_POST["size"]);
				if($size > 0){
					$obj = new resizeimage($imageName);
					$obj->init();
					$obj->border($size, $color, 100);
				}
			break;

		}
		clearstatcache();
		list($w, $h) = getimagesize($imageName);
		$data = array('status'=>200, 'data'=>array(
				'imageFound'=>true,
				'imageName'=>$imageName,
				'width'=>$w,
				'height'=>$h
		));
		echo qbase::xmlPrint(qbase::jsonencode($data));
		exit(); 
	break;
}
?>