<?php
set_time_limit(0);
ini_set('memory_limit', '256M');
function openImage($path)
{
global $openImages, $maxImages;
if(isset($openImages[$path]))
{
return $openImages[$path];
}
else
{
if(count($openImages) == $maxImages)
{
$value = reset($openImages);
imagedestroy($value);
$key = key($openImages);
unset($openImages[$key]);
}
$im = @imagecreatefromjpeg($path);
if($im)
$openImages[$path] = $im;
return $im;
}
}
function closeImages()
{
global $openImages;
while(count($openImages) > 0)
{
$value = reset($openImages);
$key = key($openImages);
unset($openImages[$key]);
imagedestroy($value);
}
}
$r_index = 0;
function openNextImage($files)
{
global $r_index;
while(!$im)
{
$file = $files[$r_index];
$im = openImage($file);
$r_index++;
if($r_index == count($files))
$r_index = 0;
if($im)
break;
}
return $im;
}
function randColor($files)
{
global $r_index;
$im = openNextImage($files);
$ok = false;
$n = 0;
while(!$ok)
{
$x = rand(0, 70);
$y = rand(0, 70);
$rgb = imagecolorat($im, $x, $y);
$hsv = rgb2hsv($rgb);
if($hsv[1] >= 0.2 && $hsv[2] <= 0.9)
$ok = true;
else
{
$n++;
if($n > 100)
{
$im = openNextImage($files);
$n = 0;
}
}
}
return $rgb;
}
function rgb2hsv($rgb)
{
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$r /= 255;
$g /= 255;
$b /= 255;
$minRGB = min($r, min($g, $b));
$maxRGB = max($r, max($g, $b));
if($minRGB == $maxRGB)
{
return array(0, 0, $minRGB);
}
$d = ($r == $minRGB) ? $g - $b : (($b == $minRGB) ? $r - $g : $b - $r);
$h = ($r == $minRGB) ? 3 : (($b == $minRGB) ? 1 : 5);
$h = 60 * ($h - $d/($maxRGB-$minRGB));
$s = ($maxRGB - $minRGB)/$maxRGB;
$v = $maxRGB;
return array($h, $s, $v);
}
function cmp($x, $y)
{
return $x[0] < $y[0] ? -1 : 1;
}
function sortPixels($im, $w, $h)
{
for($x = 0; $x < $w; $x++)
{
$hues = array();
for($y = 0; $y < $h; $y++)
{
$rgb = imagecolorat($im, $x, $y);
$hsv = rgb2hsv($rgb);
$hues[$y] = array($hsv[0], $rgb);
}
usort($hues, 'cmp');
for($y = 0; $y < $h; $y++)
{
imagesetpixel($im, $x, $y, $hues[$y][1]);
}
}
for($y = 0; $y < $h; $y++)
{
$values = array();
for($x = 0; $x < $w; $x++)
{
$rgb = imagecolorat($im, $x, $y);
$hsv = rgb2hsv($rgb);
$values[$x] = array($hsv[2], $rgb);
}
usort($values, 'cmp');
for($x = 0; $x < $w; $x++)
{
imagesetpixel($im, $x, $y, $values[$x][1]);
}
}
}
$subs = explode(',', strtolower($_GET['sub']));
sort($subs);
$w = 80;
$h = 60;
$dw = 80;
$dh = 60;
$num = $w * $h;
$vspacing = 32;
$hspacing = 72;
$columns = $_GET['c'];
$rows = $_GET['r'];
$page = imagecreatetruecolor(($dw+$hspacing)*$columns+$hspacing, ($dh+$vspacing)*$rows+$vspacing*2);
$trans = imagecolorallocate($page, 4, 4, 4);
$white = imagecolorallocate($page, 255, 255, 255);
imagefill($page, 0, 0, $trans);
$row = 0;
$col = 0;
$maxImages = 50;
$open = array();
foreach($subs as $sub)
{
$dir = opendir('images/'.$sub);
$colors = array();
$files = array();
while($file = readdir($dir))
{
$path = 'images/'.$sub.'/'.$file;
if(is_file($path))
$files[] = $path;
}
if(count($files) == 0)
continue;
$path = 'out/'.$sub.'.png';
if(file_exists($path))
$out = imagecreatefrompng($path);
else
{
$out = imagecreatetruecolor($w, $h);
$x = 0;
$y = 0;
for($i = 0; $i < $num; $i++)
{
$rgb = randColor($files);
imagesetpixel($out, $x, $y, $rgb);
$x++;
if($x == $w)
{
$x = 0;
$y++;
}
}
closeImages();
sortPixels($out, $w, $h);
imagepng($out, $path);
}
$px = $hspacing + $col * ($dw + $hspacing);
$py = 32 + $row * ($dh + $vspacing);
imagecopyresampled($page, $out, $px, $py, 0, 0, $dw, $dh, $w, $h);
imagettftext($page, 8, 0, $px, $py + $dh + $vspacing/2, $white, "/ARLRDBD.TTF", '/r/'.$sub);
$col++;
if($col == $columns)
{
$col = 0;
$row++;
}
}
header('Content-type: image/png');
imagepng($page);
?>