<?php
empty($_POST['file']) ? $file = 'braten/image_01.jpg' : $file = $_POST['file'];
$image = imagecreatefromjpeg($file);
$w = imagesx($image);
$h = imagesy($image);
$avarage_pixel = imagecreatetruecolor(1, 1);
imagecopyresampled($avarage_pixel, $image, 0, 0, 0, 0, 1, 1, $w, $h);
$rgb = imagecolorat($avarage_pixel, 0, 0);
$color = imagecolorsforindex($avarage_pixel, $rgb);
$r = $color['red'];
$g = $color['green'];
$b = $color['blue'];
function rgb2hex($r, $g=-1, $b=-1)
{
if (is_array($r) && sizeof($r) == 3)
list($r, $g, $b) = $r;
$r = intval($r); $g = intval($g);
$b = intval($b);
$r = dechex($r<0?0:($r>255?255:$r));
$g = dechex($g<0?0:($g>255?255:$g));
$b = dechex($b<0?0:($b>255?255:$b));
$color = (strlen($r) < 2?'0':'').$r;
$color .= (strlen($g) < 2?'0':'').$g;
$color .= (strlen($b) < 2?'0':'').$b;
return '#'.$color;
}
?>
<html>
<head>
<title>RGB: <?php echo $color['red'] ?>, <?php echo $color['green'] ?>, <?php echo $color['blue'] ?></title>
</head>
<body style="background:#2a2a2a;">
<div id="wrap" style="margin: 100px auto 0 auto; height: 340px; width: 480px; background-color: rgb(<?php echo $color['red'] ?>, <?php echo $color['green'] ?>, <?php echo $color['blue'] ?>)">
<img src="<?php echo $file ?>" />
<form action="" method="post" style="margin: 10px;">
<select name="file">
<option value="braten/image_01.jpg">Bild 1</option>
<option value="braten/image_02.jpg">Bild 2</option>
<option value="braten/image_03.jpg">Bild 3</option>
<option value="braten/image_04.jpg">Bild 4</option>
<option value="braten/image_05.jpg">Bild 5</option>
</select>
<input type="submit" value="ansehen">
<span>HEX: <?php echo rgb2hex($r, $g, $b); ?></span> <span>RGB: <?php echo $color['red'] ?>, <?php echo $color['green'] ?>, <?php echo $color['blue'] ?></span>
</form>
</div>
</body>
</html>