blogblogblogblog SyKoHPaTh

MyAnimeList Signature Generator

73
ok, time for a long script!

This generates an image that shows your recent viewing history on My Anime List. Of course, it's all tuned for me, but it's all easily changeable. I've seen a few signatures for MAL already, but I didn't like how they worked, exactly. Some were just plain badly coded (not that mine is 100% perfect), so I decided to write my own (viewable here):

Code Sample:
  1. <?php
  2. /* -----------------------
  3. MyAnimeList.net User History Signature Generator ()
  4. ----------------------- */
  5. $url = "http://myanimelist.net/rss.php?type=rw&u=" . $_GET['u'];
  6. $file = fopen("$url", "r");
  7. //store page contents into buffer
  8. if (!$file) {
  9. //if we can't create a file MAL is down, so create appropriate message as image instead!
  10. $image = imagecreate(600,110);
  11. $textcolor = imagecolorallocate($image, 255,255,255);
  12. imagestring($image, 5, 85,26, "Unable to connect to MyAnimeList.net", $textcolor);
  13. exit; //should make this end prettier
  14. } else {
  15. $buffer = "";
  16. while (!feof($file)) {
  17. $buffer .= fgets($file,1024);
  18. }
  19. fclose($file);
  20. }
  21. //strip special chars
  22. $buffer = str_replace("
  23. ", "", $buffer);
  24. $buffer = str_replace("
  25. ", "", $buffer);
  26. $buffer = str_replace("\t", "", $buffer);
  27. //parse RSS tags
  28. //grab all item things
  29. $pattern = "/(.*?)<\/item>/";
  30. preg_match_all($pattern, $buffer, $match_items, PREG_SET_ORDER);
  31. if(!empty($match_items)){
  32. $x=0;
  33. foreach($match_items as $matches){
  34. //[0] includes item tags, [1] does not
  35. //title
  36. $pattern = "/<title>(.*?)<\/title>/";
  37. preg_match($pattern, $matches[1], $match_title);
  38. //description to determine status
  39. $pattern = "/(.*?)<\/description>/";
  40. preg_match($pattern, $matches[1], $match_description);
  41. //date
  42. $pattern = "/(.*?)<\/pubDate>/";
  43. preg_match($pattern, $matches[1], $match_date);
  44. //link
  45. $pattern = "/<link>(.*?)<\/link>/";
  46. preg_match($pattern, $matches[1], $match_link);
  47. //grab only "watching"
  48. if(strpos($match_description[1], "Watching") !== FALSE || strpos($match_description[1], "Completed") !== FALSE){
  49. $anime[$x]->title = $match_title[1];
  50. $anime[$x]->status = $match_description[1];
  51. $anime[$x]->date = $match_date[1];
  52. $anime[$x]->link = $match_link[1];
  53. //optional formatting - will change "title - TV - Completed - X of Y episodes" to "title - X/Y"
  54. $anime[$x]->status = str_replace(" of ", "/", $anime[$x]->status);
  55. $anime[$x]->status = str_replace(" episodes", "", $anime[$x]->status);
  56. //Pre PHP 5.3
  57. $before = explode(" - ", $anime[$x]->title);
  58. $anime[$x]->title = $before[0];
  59. $anime[$x]->status = strstr($anime[$x]->status, " - ");
  60. //PHP 5.3 only
  61. //$anime[$x]->title = strstr($anime[$x]->title, " - ", true);
  62. //$anime[$x]->status = strstr($anime[$x]->status, " - ", false);
  63. $anime[$x]->status = str_replace(" - ", "", $anime[$x]->status);
  64. //increment object counter
  65. $x ;
  66. }
  67. }
  68. }
  69. $image = imagecreate(600,110);
  70. $background = imagecolorallocate($image, 0,0,0);
  71. $bgtextcolor = imagecolorallocate($image, 50,50,50);
  72. $textcolor = imagecolorallocate($image, 200,200,200);
  73. $linecolor = imagecolorallocate($image, 255,0,0);
  74. // ---- scrambled text background ---- // (OPTIONAL)
  75. $characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890~!@#$%^&*()_ -=`[]{}:;'|,.<>/?";
  76. for($vcntr=-1;$vcntr<60;$vcntr =2) {
  77. for($cntr=-1; $cntr<160; $cntr =2){
  78. $letter = substr($characters, rand(0,(strlen($characters)-1)),1);
  79. $spacing = (rand(1,10) ($cntr*4));
  80. $vspacing = (rand(1,10) ($vcntr*4));
  81. imagestring($image, 5, $spacing, $vspacing, $letter, $bgtextcolor);
  82. }
  83. }
  84. // ---- logo image merge ---- // (OPTIONAL)
  85. $logo = @imagecreatefromgif("http://www.sykohpath.com/images/sykohpath.gif");
  86. if($logo){
  87. //logo was loaded
  88. imagecopymerge($image, $logo, 15, 12, 0, 0, 50, 50, 100);
  89. imagerectangle($image, 15, 12, 65, 62, $linecolor);
  90. }
  91. // ---- generic linkback ---- // (OPTIONAL)
  92. imagestring($image, 4, 485, 90, "sykohpath.com", $linecolor);
  93. // ---- anime title status lines ---- // (REQUIRED)
  94. for($i=0;$i<5;$i ){
  95. if(!empty($anime[$i])){
  96. //readability lines
  97. imagerectangle($image, 85, ($i*16) 25, 580, ($i*16) 25, $linecolor);
  98. imagestring($image, 5, 85,($i*16) 10, $anime[$i]->title, $textcolor);
  99. imagestring($image, 5, 400,($i*16) 10, "(" . $anime[$i]->status . ")", $textcolor);
  100. imagestring($image, 5, 480,($i*16) 10, date("Y M d", strtotime($anime[$i]->date)), $textcolor);
  101. }
  102. }
  103. // ---- border ---- // (OPTIONAL)
  104. imagesetthickness($image, 2);
  105. imagerectangle($image, 0, 1, 599, 108, $linecolor);
  106. // ---- generate the image (finally) ---- //
  107. header( "Content-type: image/png");
  108. imagepng($image);
  109. // ---- free up resources ---- //
  110. imagecolordeallocate($linecolor);
  111. imagecolordeallocate($textcolor);
  112. imagecolordeallocate($background);
  113. imagedestroy($image);
  114. ?>



php, script, myanimelist, mal