logo sykohpath.com

				where code goes to die
			
	
tagstagstagstags
1332523761

MyAnimeList Signature Generator

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&#40;"$url", "r"&#41;;
  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("n", "", $buffer);
  23. $buffer = str_replace("r", "", $buffer);
  24. $buffer = str_replace("t", "", $buffer);
  25. //parse RSS tags
  26.   //grab all item things
  27. $pattern = "/<item>(.*?)</item>/";
  28. preg_match_all($pattern, $buffer, $match_items, PREG_SET_ORDER);
  29. if(!empty($match_items)){
  30.   $x=0;
  31.   foreach($match_items as $matches){
  32.    //[0] includes item tags, [1] does not
  33.    //title
  34.    $pattern = "/<title>(.*?)</title>/";
  35.    preg_match($pattern, $matches[1], $match_title);
  36.    //description to determine status
  37.    $pattern = "/<description>(.*?)</description>/";
  38.    preg_match($pattern, $matches[1], $match_description);
  39.    //date
  40.    $pattern = "/<pubDate>(.*?)</pubDate>/";
  41.    preg_match($pattern, $matches[1], $match_date);
  42.    //link
  43.    $pattern = "/<link>(.*?)</link>/";
  44.    preg_match($pattern, $matches[1], $match_link);
  45.    //grab only "watching"
  46.    if(strpos($match_description[1], "Watching") !== FALSE || strpos($match_description[1], "Completed") !== FALSE){
  47.     $anime[$x]->title = $match_title[1];
  48.     $anime[$x]->status = $match_description[1];
  49.     $anime[$x]->date = $match_date[1];
  50.     $anime[$x]->link = $match_link[1];
  51.     //optional formatting - will change "title - TV - Completed - X of Y episodes" to "title - X/Y"
  52.     $anime[$x]->status = str_replace(" of ", "/", $anime[$x]->status);
  53.     $anime[$x]->status = str_replace(" episodes", "", $anime[$x]->status);
  54.     //Pre PHP 5.3
  55.      $before = explode(" - ", $anime[$x]->title);
  56.      $anime[$x]->title = $before[0];
  57.      $anime[$x]->status = strstr($anime[$x]->status, " - ");
  58.     //PHP 5.3 only
  59.      //$anime[$x]->title = strstr($anime[$x]->title, " - ", true);
  60.      //$anime[$x]->status = strstr($anime[$x]->status, " - ", false);
  61.     $anime[$x]->status = str_replace(" - ", "", $anime[$x]->status);
  62.     //increment object counter
  63.     $x++;
  64.    }
  65.   }
  66. }
  67. $image = imagecreate(600,110);
  68. $background = imagecolorallocate($image, 0,0,0);
  69. $bgtextcolor = imagecolorallocate($image, 50,50,50);
  70. $textcolor = imagecolorallocate($image, 200,200,200);
  71. $linecolor = imagecolorallocate($image, 255,0,0);
  72. // ---- scrambled text background ---- // (OPTIONAL)
  73.   $characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890~!@#$%^&*()_+-=`[]{}:;'|,.<>/?";
  74.   
  75.   for($vcntr=-1;$vcntr<60;$vcntr+=2) {
  76.    for($cntr=-1; $cntr<160; $cntr+=2){
  77.     $letter = substr($characters, rand(0,(strlen($characters)-1)),1);
  78.     
  79.     $spacing = (rand(1,10)+($cntr*4));
  80.     $vspacing = (rand(1,10)+($vcntr*4));
  81.   
  82.     imagestring($image, 5, $spacing, $vspacing, $letter, $bgtextcolor);
  83.    }
  84.   }
  85. // ---- logo image merge ---- // (OPTIONAL)
  86.   $logo = @imagecreatefromgif("http://www.sykohpath.com/images/sykohpath.gif");
  87.   if($logo){
  88.    //logo was loaded
  89.    imagecopymerge($image, $logo, 15, 12, 0, 0, 50, 50, 100);
  90.    imagerectangle($image, 15, 12, 65, 62, $linecolor);
  91.   }
  92. // ---- generic linkback ---- // (OPTIONAL)
  93.   imagestring($image, 4, 485, 90, "sykohpath.com", $linecolor);
  94. // ---- anime title status lines ---- // (REQUIRED)
  95.   for($i=0;$i<5;$i++){
  96.    if(!empty($anime[$i])){
  97.      //readability lines
  98.     imagerectangle($image, 85, ($i*16)+25, 580, ($i*16)+25, $linecolor);
  99.     imagestring($image, 5, 85,($i*16)+10, $anime[$i]->title, $textcolor);
  100.     imagestring($image, 5, 400,($i*16)+10, "(" . $anime[$i]->status . ")", $textcolor);
  101.     imagestring($image, 5, 480,($i*16)+10, date("Y M d", strtotime($anime[$i]->date)), $textcolor);
  102.    }
  103.   }
  104. // ---- border ---- // (OPTIONAL)
  105. imagesetthickness($image, 2);
  106. imagerectangle($image, 0, 1, 599, 108, $linecolor);
  107. // ---- generate the image (finally) ---- //
  108. header( "Content-type: image/png");
  109. imagepng($image);
  110. // ---- free up resources ---- //
  111. imagecolordeallocate($linecolor);
  112. imagecolordeallocate($textcolor);
  113. imagecolordeallocate($background);
  114. imagedestroy($image);
  115. ?>

php, script, myanimelist, mal,


3 comments.