Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. <?php
  2. set_time_limit(0);
  3. ini_set('memory_limit', '256M');
  4. function openImage($path)
  5. {
  6. global $openImages, $maxImages;
  7. if(isset($openImages[$path]))
  8. {
  9. return $openImages[$path];
  10. }
  11. else
  12. {
  13. if(count($openImages) == $maxImages)
  14. {
  15. $value = reset($openImages);
  16. imagedestroy($value);
  17. $key = key($openImages);
  18. unset($openImages[$key]);
  19. }
  20. $im = @imagecreatefromjpeg($path);
  21. if($im)
  22. $openImages[$path] = $im;
  23. return $im;
  24. }
  25. }
  26. function closeImages()
  27. {
  28. global $openImages;
  29. while(count($openImages) > 0)
  30. {
  31. $value = reset($openImages);
  32. $key = key($openImages);
  33. unset($openImages[$key]);
  34. imagedestroy($value);
  35. }
  36. }
  37. $r_index = 0;
  38. function openNextImage($files)
  39. {
  40. global $r_index;
  41. while(!$im)
  42. {
  43. $file = $files[$r_index];
  44. $im = openImage($file);
  45. $r_index++;
  46. if($r_index == count($files))
  47. $r_index = 0;
  48. if($im)
  49. break;
  50. }
  51. return $im;
  52. }
  53. function randColor($files)
  54. {
  55. global $r_index;
  56. $im = openNextImage($files);
  57. $ok = false;
  58. $n = 0;
  59. while(!$ok)
  60. {
  61. $x = rand(0, 70);
  62. $y = rand(0, 70);
  63. $rgb = imagecolorat($im, $x, $y);
  64. $hsv = rgb2hsv($rgb);
  65. if($hsv[1] >= 0.2 && $hsv[2] <= 0.9)
  66. $ok = true;
  67. else
  68. {
  69. $n++;
  70. if($n > 100)
  71. {
  72. $im = openNextImage($files);
  73. $n = 0;
  74. }
  75. }
  76. }
  77. return $rgb;
  78. }
  79. function rgb2hsv($rgb)
  80. {
  81. $r = ($rgb >> 16) & 0xFF;
  82. $g = ($rgb >> 8) & 0xFF;
  83. $b = $rgb & 0xFF;
  84. $r /= 255;
  85. $g /= 255;
  86. $b /= 255;
  87. $minRGB = min($r, min($g, $b));
  88. $maxRGB = max($r, max($g, $b));
  89. if($minRGB == $maxRGB)
  90. {
  91. return array(0, 0, $minRGB);
  92. }
  93.  
  94. $d = ($r == $minRGB) ? $g - $b : (($b == $minRGB) ? $r - $g : $b - $r);
  95. $h = ($r == $minRGB) ? 3 : (($b == $minRGB) ? 1 : 5);
  96. $h = 60 * ($h - $d/($maxRGB-$minRGB));
  97. $s = ($maxRGB - $minRGB)/$maxRGB;
  98. $v = $maxRGB;
  99. return array($h, $s, $v);
  100. }
  101. function cmp($x, $y)
  102. {
  103. return $x[0] < $y[0] ? -1 : 1;
  104. }
  105. function sortPixels($im, $w, $h)
  106. {
  107. for($x = 0; $x < $w; $x++)
  108. {
  109. $hues = array();
  110. for($y = 0; $y < $h; $y++)
  111. {
  112. $rgb = imagecolorat($im, $x, $y);
  113. $hsv = rgb2hsv($rgb);
  114. $hues[$y] = array($hsv[0], $rgb);
  115. }
  116. usort($hues, 'cmp');
  117. for($y = 0; $y < $h; $y++)
  118. {
  119. imagesetpixel($im, $x, $y, $hues[$y][1]);
  120. }
  121. }
  122. for($y = 0; $y < $h; $y++)
  123. {
  124. $values = array();
  125. for($x = 0; $x < $w; $x++)
  126. {
  127. $rgb = imagecolorat($im, $x, $y);
  128. $hsv = rgb2hsv($rgb);
  129. $values[$x] = array($hsv[2], $rgb);
  130. }
  131. usort($values, 'cmp');
  132. for($x = 0; $x < $w; $x++)
  133. {
  134. imagesetpixel($im, $x, $y, $values[$x][1]);
  135. }
  136. }
  137.  
  138. }
  139.  
  140. $subs = explode(',', strtolower($_GET['sub']));
  141. sort($subs);
  142. $w = 80;
  143. $h = 60;
  144. $dw = 80;
  145. $dh = 60;
  146. $num = $w * $h;
  147. $vspacing = 32;
  148. $hspacing = 72;
  149. $columns = $_GET['c'];
  150. $rows = $_GET['r'];
  151. $page = imagecreatetruecolor(($dw+$hspacing)*$columns+$hspacing, ($dh+$vspacing)*$rows+$vspacing*2);
  152. $trans = imagecolorallocate($page, 4, 4, 4);
  153. $white = imagecolorallocate($page, 255, 255, 255);
  154. imagefill($page, 0, 0, $trans);
  155. $row = 0;
  156. $col = 0;
  157. $maxImages = 50;
  158. $open = array();
  159.  
  160. foreach($subs as $sub)
  161. {
  162. $dir = opendir('images/'.$sub);
  163. $colors = array();
  164. $files = array();
  165. while($file = readdir($dir))
  166. {
  167. $path = 'images/'.$sub.'/'.$file;
  168. if(is_file($path))
  169. $files[] = $path;
  170. }
  171. if(count($files) == 0)
  172. continue;
  173. $path = 'out/'.$sub.'.png';
  174. if(file_exists($path))
  175. $out = imagecreatefrompng($path);
  176. else
  177. {
  178. $out = imagecreatetruecolor($w, $h);
  179. $x = 0;
  180. $y = 0;
  181. for($i = 0; $i < $num; $i++)
  182. {
  183. $rgb = randColor($files);
  184.  
  185. imagesetpixel($out, $x, $y, $rgb);
  186. $x++;
  187. if($x == $w)
  188. {
  189. $x = 0;
  190. $y++;
  191. }
  192. }
  193. closeImages();
  194. sortPixels($out, $w, $h);
  195. imagepng($out, $path);
  196. }
  197. $px = $hspacing + $col * ($dw + $hspacing);
  198. $py = 32 + $row * ($dh + $vspacing);
  199. imagecopyresampled($page, $out, $px, $py, 0, 0, $dw, $dh, $w, $h);
  200. imagettftext($page, 8, 0, $px, $py + $dh + $vspacing/2, $white, "/ARLRDBD.TTF", '/r/'.$sub);
  201. $col++;
  202. if($col == $columns)
  203. {
  204. $col = 0;
  205. $row++;
  206. }
  207. }
  208. header('Content-type: image/png');
  209. imagepng($page);
  210. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement