%%image-mogrify-editor-png%%

%%preprocess%%
$this->getBaseUrl();

$tags= "";
$getvars= $GLOBALS['HTTP_GET_VARS'];
$postvars= $GLOBALS['HTTP_POST_VARS'];
if (!$postvars['submit']) {
	$width= preg_replace ("/[^0-9]/","",$getvars['width']);
	$height= preg_replace ("/[^0-9]/","",$getvars['height']);
	if ($getvars['exact']) $isExact= "!";
	
	// if getvars check out, build geometry tag for image size
	if ($width>0 && $width<1200 && $height>0 && $height<1200) {
		$tags.= "-geometry $width"."x$height"."$isExact ";
		}
	
	// if getvars check out, build quality tag for compression level
	if ($getvars['quality']>0 && $getvars['quality']<=100) {
		$quality= preg_replace ("/[^0-9]/","",$getvars['quality']);
		$tags.= "-quality $quality ";
		}
		
	if ($getvars['rotate']) {
		$rotatefactor= preg_replace ("/[^0-9]/","",$getvars['rotate']);
		$tags.= "-rotate $rotatefactor ";
		}
	if ($getvars['spread']) {
		$spreadfactor= preg_replace ("/[^0-9\.]/","",$getvars['spread']);
		$tags.= "-spread $spreadfactor ";
		}
	if ($getvars['gamma']>0 && $getvars['gamma']<3) {
		$gammafactor= preg_replace ("/[^0-9\.]/","",$getvars['gamma']);
		$tags.= "-gamma $gammafactor ";
		}
	if ($getvars['median']>0 && $getvars['median']<=6) {
		$medianfactor= preg_replace ("/[^0-9\.]/","",$getvars['median']);
		$tags.= "-median $medianfactor ";
		}
	if ($getvars['gaussian'] && $getvars['gaussiansigma']) {
		$gaussianfactor= preg_replace ("/[^0-9\.]/","",$getvars['gaussian']);
		$gaussiansigma= preg_replace ("/[^0-9\.]/","",$getvars['gaussiansigma']);
		$tags.= "-gaussian ".$gaussianfactor."x".$gaussiansigma." ";
		}
	if ($getvars['sharpen'] && $getvars['sharpensigma']) {
		$sharpenfactor= preg_replace ("/[^0-9\.]/","",$getvars['sharpen']);
		$sharpensigma= preg_replace ("/[^0-9\.]/","",$getvars['sharpensigma']);
		$tags.= "-sharpen ".$sharpenfactor."x".$sharpensigma." ";
		}
	if ($getvars['modulatebri'] && $getvars['modulatesat'] && $getvars['modulatehue']) {
		$modulatebri= preg_replace ("/[^0-9]/","",$getvars['modulatebri']);
		$modulatesat= preg_replace ("/[^0-9]/","",$getvars['modulatesat']);
		$modulatehue= preg_replace ("/[^0-9]/","",$getvars['modulatehue']);
		$tags.= "-modulate $modulatebri,$modulatesat,$modulatehue ";
		}
	if ($getvars['cropwidth'] && $getvars['cropheight']) {
		$cropwidth= preg_replace ("/[^0-9]/","",$getvars['cropwidth']);
		$cropheight= preg_replace ("/[^0-9]/","",$getvars['cropheight']);
		$cropstartx= preg_replace ("/[^0-9]/","",$getvars['cropstartx']);
		$cropstarty= preg_replace ("/[^0-9]/","",$getvars['cropstarty']);
		if ($cropstartx=="") $cropstartx=0;
		if ($cropstarty=="") $cropstarty=0;
		$tags.= "-crop ".$cropwidth."x".$cropheight."+$cropstartx+$cropstarty ";
		}
	if ($getvars['enhance']) {
		$tags.= "-enhance ";
		}
	if ($getvars['normalize']) {
		$tags.= "-normalize ";
		}
	if ($getvars['flop']) {
		$tags.= "-flop ";
		}
	if ($getvars['negate']) {
		$tags.= "-negate ";
		}
	if ($getvars['monochrome']) {
		$tags.= "-colorspace GRAY -colors 65536 ";
		}
		
	$safepath= escapeshellarg($this->path);	
	$command= "/usr/local/bin/convert $tags $safepath PNG:-";
	
	//send content header
	header("Content-type: image/gif");
	passthru($command);
	
	//print $command;
	exit;
	}
	
elseif ($postvars['submit']=="commit") {
	$width= preg_replace ("/[^0-9]/","",$postvars['width']);
	$height= preg_replace ("/[^0-9]/","",$postvars['height']);
	if ($postvars['exact']!=0) $isExact= "!";
	
	// if postvars check out, build geometry tag for image size
	if ($width>0 && $width<1200 && $height>0 && $height<1200) {
		$tags.= "-geometry $width"."x$height"."$isExact ";
		}
	
	// if postvars check out, build quality tag for compression level
	if ($postvars['quality']>0 && $postvars['quality']<=100) {
		$quality= preg_replace ("/[^0-9]/","",$postvars['quality']);
		$tags.= "-quality $quality ";
		}
		
	if ($postvars['rotate']) {
		$rotatefactor= preg_replace ("/[^0-9]/","",$postvars['rotate']);
		$tags.= "-rotate $rotatefactor ";
		}
	if ($postvars['spread']) {
		$spreadfactor= preg_replace ("/[^0-9\.]/","",$postvars['spread']);
		$tags.= "-spread $spreadfactor ";
		}
	if ($postvars['gamma']>0 && $postvars['gamma']<3) {
		$gammafactor= preg_replace ("/[^0-9\.]/","",$postvars['gamma']);
		$tags.= "-gamma $gammafactor ";
		}
	if ($postvars['median']>0 && $postvars['median']<=6) {
		$medianfactor= preg_replace ("/[^0-9\.]/","",$postvars['median']);
		$tags.= "-median $medianfactor ";
		}
	if ($postvars['gaussian'] && $postvars['gaussiansigma']) {
		$gaussianfactor= preg_replace ("/[^0-9\.]/","",$postvars['gaussian']);
		$gaussiansigma= preg_replace ("/[^0-9\.]/","",$postvars['gaussiansigma']);
		$tags.= "-gaussian ".$gaussianfactor."x".$gaussiansigma." ";
		}
	if ($postvars['sharpen'] && $postvars['sharpensigma']) {
		$sharpenfactor= preg_replace ("/[^0-9\.]/","",$postvars['sharpen']);
		$sharpensigma= preg_replace ("/[^0-9\.]/","",$postvars['sharpensigma']);
		$tags.= "-sharpen ".$sharpenfactor."x".$sharpensigma." ";
		}
	if ($postvars['modulatebri'] && $postvars['modulatesat'] && $postvars['modulatehue']) {
		$modulatebri= preg_replace ("/[^0-9]/","",$postvars['modulatebri']);
		$modulatesat= preg_replace ("/[^0-9]/","",$postvars['modulatesat']);
		$modulatehue= preg_replace ("/[^0-9]/","",$postvars['modulatehue']);
		$tags.= "-modulate $modulatebri,$modulatesat,$modulatehue ";
		}
	if ($postvars['cropwidth'] && $postvars['cropheight']) {
		$cropwidth= preg_replace ("/[^0-9]/","",$postvars['cropwidth']);
		$cropheight= preg_replace ("/[^0-9]/","",$postvars['cropheight']);
		$cropstartx= preg_replace ("/[^0-9]/","",$postvars['cropstartx']);
		$cropstarty= preg_replace ("/[^0-9]/","",$postvars['cropstarty']);
		if ($cropstartx=="") $cropstartx=0;
		if ($cropstarty=="") $cropstarty=0;
		$tags.= "-crop ".$cropwidth."x".$cropheight."+$cropstartx+$cropstarty ";
		}
	if ($postvars['enhance']) {
		$tags.= "-enhance ";
		}
	if ($postvars['normalize']) {
		$tags.= "-normalize ";
		}
	if ($postvars['flop']) {
		$tags.= "-flop ";
		}
	if ($postvars['negate']) {
		$tags.= "-negate ";
		}
	if ($postvars['monochrome']) {
		$tags.= "-colorspace GRAY -colors 65536 ";
		}
	
	$safepath= escapeshellarg($this->path);	
	$command= "/usr/local/bin/mogrify -format png $tags $safepath ";
	
	$result= exec($command);
	
	$imgsize= getimagesize($this->path);
	$this->width= $imgsize[0];
	$this->height= $imgsize[1];
	$this->updateObject();
		
	$redirect="Location: $_SERVER[SCRIPT_NAME]$folder->name/image-$this->name.html?method=view".$session->cgi; 
	if ($GLOBALS[debug]) {
		 print "Done with $command.<br><a href='$this->baseUrl?method=view$session->cgi'>Click here to continue.</a>";
		 }
	else {
		header($redirect); 
		exit;
		}
	}



%%header%%


%%css%%


%%template%%
<img src='/bicons/warning.png' alt='Warning' align=absmiddle> You seem to be in the wrong place.


%%listrow%%
 

%%nullobject%%
 <img src='/bicons/warning.png' alt='Warning' align=absmiddle> Sorry, that image is not available for editing.


%%footer%%


%%postprocess%%


%%end of context%%
