<?php

// image.php?mode=<scale_mode>&img=<image_name>&height=<canvas_height>&width=<canvas_width>&quality=<image_quality>&bw=<image_blackwhite>

// <canvas_height>						niet opgegeven: waarde is waarde originele afbeelding.
// <canvas_width>						niet opgegeven: waarde is waarde originele afbeelding.
// <image_name> 						naam (of eventueel path + naam) 
// <image_quality>   					kwaliteit jpg van 1-100;  niet opgegeven: waarde is default waarde hieronder.
// <image_blackwhite>   				true or false (default = false) afbeelding in zwartwit.
// <scale_mode> = "crop" of "fit"		fit: opgegeven img past geheel binnen canvas,	
//										crop: img wordt uitgevuld op canvas, uitstekende delen worden gecropt.


// defaults
$quality = 70;
$mode = "crop"; // mode is "crop" of "fit"
$bw = false;

// The file
$filename = htmlentities($_GET['img']);
//if(!is_file($filename)){ exit("no file"); }
//de controle hier boven ken je aan of uit zetten net wat je wilt :)

// Get original dimensions
if( !(list($width, $height, $type, $attr) = @getimagesize($filename)) ){ exit; }

// overwrite default stettings
if(!empty($_GET['quality']) && is_numeric($_GET['quality']) && $_GET['quality'] > 0 && $_GET['quality'] <= 100){ $quality = htmlentities($_GET['quality']); }
if(isset($_GET['mode'])){ $mode = htmlentities($_GET['mode']); }
if($_GET['bw'] === "true"){ $bw = true; }

// new canvas size
$thumb_height = $height;
$thumb_width = $width;
if(!empty($_GET['height']) && is_numeric($_GET['height']) && $_GET['height'] < $height && $_GET['height'] > 19){ $thumb_height = htmlentities($_GET['height']); }
if(!empty($_GET['width']) && is_numeric($_GET['width']) && $_GET['width'] < $width && $_GET['width'] > 19){ $thumb_width = htmlentities($_GET['width']); }


		
	//Creates yiq function
	function yiq($r,$g,$b)
	{
		return (($r*0.299)+($g*0.587)+($b*0.114));
	}
	

//////////////////////////////////////////////////
// ---------------- rescale  --------------------

if($mode == "crop"){

	// scale and crop to canvas
	if($height/$width < $thumb_height/$thumb_width){
		// original image is wider than canvas
		$new_height = $thumb_height;
		$new_width = $width * ($thumb_height/$height);
		$new_y = 0;
		$new_x = ($new_width - $thumb_width)/2;
	}else{
		// original image is higher than canvas
		$new_width = $thumb_width;
		$new_height = $height * ($thumb_width/$width);
		$new_x = 0;
		$new_y = ($new_height - $thumb_height)/2;
	}
	
}else{ 
	// mode is "fit" 
	
	// fit to canvas
	if($height/$width < $thumb_height/$thumb_width){
		// original image is wider than canvas
		$new_width = $thumb_width;
		$new_height = ($thumb_width/$width) * $height;
		$thumb_height = $new_height;
		$new_x = 0;
		$new_y = 0;
	}else{
		// original image is higher than canvas
		$new_height = $thumb_height;
		$new_width = ($thumb_height/$height) * $width;
		$thumb_width = $new_width;
		$new_y = 0;
		$new_x = 0;
	}
}

// ---------------- rescale  --------------------
//////////////////////////////////////////////////


if($type == 1){ // img is GIF
	// Content type
	header("Content-type: image/gif");
	
	// Resample
	$image_p = imagecreatetruecolor($thumb_width, $thumb_height);
	$image = imagecreatefromgif($filename);
	imagecopyresampled($image_p, $image, 0, 0, $new_x, $new_y, $new_width, $new_height, $width, $height);
	
	// Output
	imagegif($image_p, null, $quality);

}elseif($type == 2){ // img is JPG
	// Content type
	header('Content-type: image/jpeg');
	
	// Resample
	$image_p = imagecreatetruecolor($thumb_width, $thumb_height);
	$image = imagecreatefromjpeg($filename);
	imagecopyresampled($image_p, $image, 0, 0, $new_x, $new_y, $new_width, $new_height, $width, $height);
	
	// create black and white image
	if($bw && $thumb_width < 800 && $thumb_height < 800){
				
		// Creating the Canvas
		$bwimage= imagecreatetruecolor($thumb_width, $thumb_height);
		
		//Creates the 256 color palette
		for ($c=0;$c<256;$c++)
		{
			$palette[$c] = imagecolorallocate($bwimage,$c,$c,$c);
		}
		
		//Reads the origonal colors pixel by pixel
		for ($y=0;$y<$thumb_height;$y++)
		{
			for ($x=0;$x<$thumb_width;$x++)
			{
				$rgb = imagecolorat($image_p,$x,$y);
				$r = ($rgb >> 16) & 0xFF;
				$g = ($rgb >> 8) & 0xFF;
				$b = $rgb & 0xFF;
				
				//This is where we actually use yiq to modify our rbg values, and then convert them to our grayscale palette
				$gs = yiq($r,$g,$b);
				imagesetpixel($bwimage,$x,$y,$palette[$gs]);
			}
		}
	
		// black and white Output
		imagejpeg($bwimage, null, $quality);
		
		
	}else{
	
		// color Output
		imagejpeg($image_p, null, $quality);
		
	}
	
}

?>