File size: 949B
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=utf8');
header('Access-Control-Allow-Methods: GET');
$file = filter_var($_GET['file'], FILTER_SANITIZE_URL);
if (empty($file)) {
header('HTTP/2.0 400 Bad Request');
exit;
};
$info = getimagesize($file);
list($width, $height) = $info;
if ($width === null) {
header('HTTP/2.0 400 Bad Request');
exit;
};
if ($info['mime'] == 'image/jpeg') {
$image = imagecreatefromjpeg($file);
} elseif ($info['mime'] == 'image/png') {
$image = imagecreatefrompng($file);
} elseif ($info['mime'] == 'image/gif') {
$image = imagecreatefromgif($file);
}
$thumb = imagecreatetruecolor(1, 1);
imagecopyresampled($thumb, $image, 0, 0, 0, 0, 1, 1, imagesx($image), imagesy($image));
$color = strtoupper(dechex(imagecolorat($thumb, 0, 0)));
$data = [
'width' => $width,
'height' => $height,
'mime' => $info['mime'],
'hex' => $color
];
echo json_encode($data);