<?php
function duotone(&$img, $hex){
	//strip #
	$hex = ltrim($hex, '#');
	//shift RGB values
	$dec	= hexdec($hex);
	$r = $dec >> 16 & 0xFF;
	$g = $dec >> 8 & 0xFF;
	$b = $dec & 0xFF;

	//dimensions:
	$w = imagesx($img);
	$h = imagesy($img);

	//new resource
	$new 	= imagecreatetruecolor($w, $h);
	//center color
	$c = ($r+$g+$b)/3;
	//container
	$t = array();

	//create pallet
	$i =0;
	while($i < 256){
		$nr = ($i <= $c) ? (($i / $c) * $r) : (255 - ((255 - $i) / (255 - $c) * (255 - $r)));
		$ng = ($i <= $c) ? (($i / $c) * $g) : (255 - ((255 - $i) / (255 - $c) * (255 - $g)));
		$nb = ($i <= $c) ? (($i / $c) * $b) : (255 - ((255 - $i) / (255 - $c) * (255 - $b)));
		$t[$i] = imagecolorallocate($new, $nr, $ng, $nb);
		$i++;
	}
    //pixel dance, loop all pixels and change them
	$x=0;
	while($x < $w){
		$y = 0;
		while($y < $h){
				$rgb = imagecolorat($img, $x, $y);

				//$a = ($rgb >> 24) << 1; WRONG!
				$a = $rgb >> 24 & 0x7F;
				$r = $rgb >> 16 & 0xFF;
				$g = $rgb >> 8 & 0xFF;
				$b = $rgb & 0xFF;
				$bw = ($r+$g+$b)/3;
				imagesetpixel($new, $x, $y, $t[$bw]);
				$y++;
		}
		$x++;
	}
	//copy new
	$img = $new;
	//free memory
	//imagedestroy will not work because of reference.
	//unset is faster anyway too!
	unset($new);
}
/************ DEBUG ******************/
ini_set('display_errors', true);
ini_set('error_reporting', 8191);
/*********** EXAMPLE(S) ******************/
$jpg = imagecreatefromjpeg('0.jpg');

duotone($jpg, '#704214'); //sepia
//duotone($jpg, '#7F7F7F'); // grey
//duotone($jpg, '#336699'); // blue
//duotone($jpg, '#9900CC'); // purple
//duotone($jpg, '#7FFF00'); // lime

if(!headers_sent()){
	header('Cache-control: private');
	header('Content-type: image/jpeg');
	header('Content-Disposition: filename="'.basename(__FILE__).'.jpg"');
	imagejpeg($jpg, NULL, 100);
	exit;
}
?>