Guest User

Untitled

a guest
Apr 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.10 KB | None | 0 0
  1. <?php
  2. /***************************
  3. Last.fm Collage Generator
  4. Developer: AntaresMHD (Twitter: @AntaresMHD)
  5.  
  6. CHANGELOG:
  7.  
  8. v.1.65b 27/06/2011
  9. Fixes:
  10. In order to comply with Last.fm's new standards, some parts of the script
  11. have been rewritten in order to make speedier the collage generation, using
  12. auto-generated 300x300 images directly from the server instead of the high
  13. resolution pictures resized on-the-fly.
  14.  
  15. v.1.60b 04/06/2011
  16. New:
  17. - Added another parameter (caption) to attach the artist and band name
  18.  
  19. v.1.55b 28/04/2011
  20. Fixes:
  21. - Avoids "hipster victories" by collecting only the albums with covers from the server.
  22. - Changes in code make the collage generation a tad faster.
  23.  
  24. v.1.50 22/04/2011
  25. New:
  26. - Frontend created by WaitWhat, new functions from previous version added to the GUI.
  27.  
  28. v.1.40b 14/03/2011
  29. Fixes:
  30. - Now generates every collage efficiently (using mod opers instead of shitty switches)
  31. - Caching call action happens before making the last.fm call
  32. - Less lines of code
  33.  
  34. New:
  35. - If the second parameter "type" is set, it generates the collage with a different time period
  36.  
  37. v.1.0b 12/03/2011
  38. - First version of the script, basic 7 days collage
  39. - Shows "hipstervictory.jpg" when it can't find an album cover
  40. - Basic cache: lasts 1h in server
  41.  
  42. ***************************/
  43. require("phplastfm/lastfmapi/lastfmapi.php");
  44. set_time_limit(240);
  45. //ini_set('max_execution_time', 240);
  46.  
  47. //Variables used for last.fm setup
  48. $authVars = array(
  49. 'apiKey' => '6605f8ac34c2fd6e2c55db932a50d596',
  50. 'secret' => '3e2f997c8901d9272ebfd1aa738be3b9',
  51. );
  52. $config = array(
  53. 'enabled' => true,
  54. 'path' => './',
  55. 'cache_length' => 1800
  56. );
  57. // Pass the array to the auth class to eturn a valid auth
  58. $auth = new lastfmApiAuth('setsession', $authVars);
  59.  
  60. $apiClass = new lastfmApi();
  61. $userClass = $apiClass->getPackage($auth, 'user', $config);
  62.  
  63. if ($_GET["type"]!=null && ($_GET["type"]== "overall" || "12month" || "6month" || "3month" || "7day")){
  64. $period= $_GET["type"];
  65. } else if ($_GET["type"]== null) {
  66. $period = "7day";
  67. } else {
  68. echo "Invalid period set.";
  69. exit;
  70. }
  71.  
  72. // Setup the variables
  73. $methodVars = array(
  74. 'user' => $_GET["user"],
  75. //'period' => '7day',
  76. 'period' => $period,
  77. /* 'limit' => 9 */ 'limit' => 27
  78. );
  79.  
  80. // Check if the newbs are doing things the way they should
  81. if ($_GET['user']== null){
  82. echo 'No user has been specified.';
  83. exit;
  84. } else if (is_cached($_GET['user'], $period)){ // Sends the cached
  85. return is_cached($_GET['user'], $period);
  86. exit;
  87. }
  88.  
  89. if ( $chart = $userClass->getTopAlbums($methodVars) ) { //get all the results according to the variables
  90. // does the magic here
  91. generate_collage($chart, $period);
  92. exit;
  93.  
  94. } else { // if the user doesn't exist
  95. die('<b>Error '.$userClass->error['code'].' - </b><i>'.$userClass->error['desc'].'</i>');
  96. echo "The user specified doesn't exist.";
  97. }
  98.  
  99. // Functions
  100.  
  101. //function that generates the chart according to the values passed via parameter
  102. function generate_collage ($chart, $period) {
  103.  
  104. $img = array(); //array that will store the image resources
  105. $caption_artist = array(); //in case the parameter is set to true, this will add the artist - album name.
  106. $caption_album = array(); //in case the parameter is set to true, this will add the artist - album name.
  107.  
  108. // Set a maximum height and width
  109. $width = 300; //in the future, these two values might be passed via parameters, maybe not, who knows
  110. $height = 300;
  111.  
  112. // Content type
  113. header('Content-type: image/jpeg');
  114.  
  115. // Create main image
  116. $output = imagecreatetruecolor(900, 900);
  117.  
  118. $img_url = array(); //array that will contain all the url pics
  119.  
  120. for ($j=0; $j<count($chart); $j++) { //this cycle (v.1.57b
  121.  
  122. if ((preg_match('/noimage/', $chart[$j]['images']['large'], $matches)==1) && $j< count($chart)) {
  123. // $img_url= 'res/HIPSTERVICTORY.jpg'; //change it for artist - album?
  124. continue;
  125. } else {
  126.  
  127. if (preg_match('/userserve/', $chart[$j]['images']['large'], $matches)==1){
  128. //$img_url[]= str_replace('/126/', '/_/', $chart[$j]['images']['large']);
  129. $img_url[]= str_replace('/126/', '/300x300/', $chart[$j]['images']['large']);
  130. if ($_GET['caption'] == true) { $caption_artist[] = $chart[$j]['artist']['name']; $caption_album[] = $chart[$j]['name']; }
  131. } else {
  132. $img_url[]= $chart[$j]['images']['large'];
  133. if ($_GET['caption'] == true) { $caption_artist[] = $chart[$j]['artist']['name']; $caption_album[] = $chart[$j]['name']; }
  134. }
  135.  
  136. }
  137.  
  138. if (count($img_url)>8){
  139. break;
  140. }
  141. }
  142.  
  143. // Get and resample images
  144.  
  145. for ($i=0; $i<count($img_url); $i++) {
  146.  
  147. // Get the extension
  148. $ext = substr($img_url[$i], -3);
  149.  
  150. // Get new dimensions
  151. list($width_orig, $height_orig) = getimagesize($img_url[$i]);
  152.  
  153. switch ($ext) {
  154. case 'png':
  155. $img[$i]= imagecreatefrompng($img_url[$i]);
  156. break;
  157.  
  158. case 'jpg':
  159. $img[$i]= imagecreatefromjpeg($img_url[$i]);
  160. break;
  161.  
  162. case 'gif':
  163. $img[$i]= imagecreatefromgif($img_url[$i]);
  164. break;
  165. }
  166.  
  167. if (preg_match('/300x300/', $img_url[$i], $matches)!=1){ //if the image hasn't been resized on the server as a
  168.  
  169. $img_res = imagecreatetruecolor(300, 300); //temp image used for resizing that will be deleted later
  170. imagecopyresampled($img_res, $img[$i], 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
  171.  
  172. } else {
  173. $img_res = $img[$i];
  174. }
  175.  
  176. // Get the coordinates where the image should be placed
  177. $x = ($i % 3)* 300; $y = (int) ($i / 3)*300; //the magic of math...
  178.  
  179. //places image into collage
  180. imagecopymerge($output, $img_res, $x, $y, 0, 0, 300, 300, 100);
  181.  
  182. if ($_GET['caption'] == true) {
  183.  
  184. //color of the font of the artist & band name
  185.  
  186. $start_x = 1;
  187. $start_y = 1;
  188. $color_tran = imagecolorsforindex($img_res, imagecolorat($img_res, $start_x, $start_y)); //extracts the color from the image at the specified index
  189.  
  190. //assigns the color
  191.  
  192. $font_color = imagecolorallocate($output, (0xff-$color_tran['red']), (0xff-$color_tran['green']), (0xff-$color_tran['blue'])); //the opposite color
  193.  
  194. // Path to our ttf font file
  195. $font_file = './richard_miller.ttf';
  196.  
  197. // Draw the artist & band name with the specified params
  198. imagefttext($output, 9, 0, ($x + 2), ($y + 12), $font_color, $font_file, $caption_artist[$i]); //5th parameter (y) should not be less than 10
  199. imagefttext($output, 9, 0, ($x + 2), ($y + 24), $font_color, $font_file, $caption_album[$i]); //5th parameter (y) should not be less than 10
  200.  
  201. //imagefttext($output, 9, 0, ($x + 2), ($y + 12), $font_color, $font_file, $img_url[$i]); //5th parameter (y) should not be less than 10 //For tests!
  202. //imagefttext($output, 9, 0, ($x + 2), ($y + 48), $font_color_opposite, $font_file, $opposite); //5th parameter (y) should not be less than 10
  203.  
  204. }
  205.  
  206. //destroys image to clear up space in memory
  207. imagedestroy($img_res);
  208. }
  209.  
  210. // Now finally, display the image... While saving it in the cache, seeing as it takes way too long
  211.  
  212. if ($_GET['caption']== true) {
  213. $cache = 'cache/'. strtolower($_GET['user']) . '_' . $period . '_caption.jpg';
  214. } else {
  215. $cache = 'cache/'. strtolower($_GET['user']) . '_' . $period . '.jpg';
  216. }
  217.  
  218. imagejpeg($output, null, 80);
  219. imagejpeg($output, $cache, 80); //saves a copy to the cache
  220.  
  221. //destroys image afterwards, to clear up space in memory
  222. imagedestroy($output);
  223. }
  224.  
  225. // function to create or return a cache
  226. function is_cached($user, $period) {
  227.  
  228. if ($_GET['caption']== true) {
  229. $possible_cached_file= strtolower($user). '_' . $period . '_caption.jpg';
  230. } else {
  231. $possible_cached_file= strtolower($user). '_' . $period . '.jpg';
  232.  
  233. }
  234.  
  235. $path = scandir('cache/');
  236.  
  237. if (array_search($possible_cached_file, $path) != false) { //if the file is present, let's proceed to analyze it
  238. $fpath= 'cache/' . $possible_cached_file;
  239. //$age = time() - (7 * 24 * 60 * 60); //seven days old
  240. //$age = time() - (12 * 60 * 60); //12 hours old
  241. $age = time() - (2 * 60 * 60); //2 hours old
  242.  
  243. if ($age < filectime($fpath)){ //if the file cached on the server is less than the specified age
  244. header('Content-type: image/jpeg');
  245. readfile($fpath);
  246. exit;
  247. } else { // if it isn't
  248. return false;
  249. }
  250.  
  251. } else { // if the file isn't there
  252. return false;
  253. }
  254.  
  255. }
  256.  
  257. ?>
Add Comment
Please, Sign In to add comment