<?php

// Demo URLs:
// ----------
// http://playground.jwscripts.com/compass/?pos=north
// http://playground.jwscripts.com/compass/?pos=east
// http://playground.jwscripts.com/compass/?pos=south
// http://playground.jwscripts.com/compass/?pos=west


// Directions
$positions = array(
    
'north' => array(07520),
    
'east' => array(27012570),
    
'south' => array(18080125),
    
'west' => array(903075)
);

// Validate input
if (!isset($_GET['pos']) || !in_array($_GET['pos'], array_keys($positions))) {
    die(
'Please provide a <code>pos</code> parameter containing either <code>' .
        
'(' implode('|'array_keys($positions)) . ')</code>');
}

// Fetch image data
list($degrees$x$y) = $positions[$_GET['pos']];

// Create resources
$compass imagecreatefromjpeg('compass.jpg');
$hand imagecreatefrompng('point.png');

// Rotate image
$rotated imagerotate($hand$degrees0);

// Restore transparency
$black imagecolorallocate($rotated000);
imagecolortransparent($rotated$black);

// Merge images
imagecopymerge($compass$rotated$x$y004651100);

// Output
header("Content-Type: image/jpeg");

imagejpeg($compass);
imagedestroy($compass);

?>