<?php
require_once "../../includes/common.inc.php";
$tmp = MODEPATH.'/uploads';
$urlpic = $urlpic;

define("TMP_PATH", $tmp);
define("TMP_URL", $tmp);
define("FILENAME_CHECK",'/(jpg|jpeg|gif|png|bmp)$/i');
//extract($_GET);

settype($x,'int');
settype($y,'int');
settype($w,'int');
settype($h,'int');
settype($iw,'int');
settype($ih,'int');
settype($pic,'string');

$pic = @trim($pic);
if(empty($pic)){
	printf("error:%s\n\n\n","1");
	//printf("错误提示：%s","请选择需要剪切的图片！");
	exit;
}
elseif(!preg_match(FILENAME_CHECK,$pic))
{
	printf("error:%s\n\n\n","2");
	//printf("错误提示：%s","请正确选择需要的图片！");
	exit;
}

$imagecontent = @file_get_contents($pic);

if(!$imagecontent){
	printf("error:%s\n\n\n","3");
	//printf("错误提示：%s","图片读取失败");
	exit;
}

if(strlen($imagecontent)<1024){
	printf("error:%s\n\n\n","4");
	//printf("错误提示：%s","图片文件太小，不能处理");
	exit;
}

$source = @imagecreatefromstring($imagecontent);
if($source!==false){
	$width = @imagesx($source);
	$height = @imagesx($source);
}
else
{
	printf("error:%s\n\n\n","5");
	//printf("错误提示：%s","图片格式不正确，无法处理");
	exit;
}

if(!$width || !$height || $width<10 || $height<10){
	printf("error:%s\n\n\n","6");
	//printf("错误提示：%s","图片尺寸太小，无法处理");
	exit;
}

$percent = $width/$iw;

$thumb = @imagecreatetruecolor($w, $h);
$thumbfile = $urlpic;
if($thumbfile) @unlink(TMP_PATH.'/'.$thumbfile);
setcookie('thumbfile',$thumbfile);
if(!@imagecopyresized($thumb, $source, 0, 0, $x * $percent, $y * $percent, $w, $h, $w * $percent, $h * $percent))
{
	printf("error:%s\n\n\n","7");
	//printf("错误提示：%s","图片剪切失败");
	exit;
}
if(@imagejpeg($thumb,TMP_PATH.'/'.$thumbfile,80))
{
	@imagedestroy($thumb);
	//printf("url:%s",TMP_URL.'/'.$thumbfile);
	printf("error:%s\n\n\n","9");
}
else
{
	printf("error:%s\n\n\n","8");
	//printf("错误提示：%s","图片写入失败");
	exit;
}
?>