PageRenderTime 100ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/api/GetArt.php

http://thegamesdb.googlecode.com/
PHP | 224 lines | 122 code | 46 blank | 56 comment | 9 complexity | 037800230b4091b7a0f92d47d901ab9f MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1, GPL-2.0, AGPL-3.0, BSD-3-Clause
  1. <?php
  2. ###=============###
  3. ###PREREQUISITES###
  4. ###--------------------------###
  5. ## Include base functions, db connection, etc
  6. include("include.php");
  7. include('../simpleimage.php');
  8. ## Get requested game id from api call
  9. $requestedID = $_REQUEST['id'];
  10. if (empty($id) || !is_numeric($id)) {
  11. print "<Error>An integer formatted id is required</Error>\n";
  12. exit;
  13. }
  14. ###==============###
  15. ###VITAL FUNCTIONS###
  16. ###----------------------------###
  17. ## Function to generate a fanart thumb image if does not already exist
  18. function makeFanartThumb($sourcefile, $targetfile) {
  19. ## Get the image sizes and read it into an image object
  20. $sourcefile_id = imagecreatefromjpeg($sourcefile);
  21. $width = imageSX($sourcefile_id);
  22. $height = imageSY($sourcefile_id);
  23. ## Settings
  24. //$scale = 0.1;
  25. $destWidth = 300;
  26. $destHeight = 169;
  27. ## Create a new destination image object - for scale resize replace $destWidth, $destHeight with: $width * $scale, $height * $scale
  28. $result_id = imagecreatetruecolor($destWidth, $destHeight);
  29. ## Copy our source image resized into the destination object - for scale resize replace $destWidth, $destHeight with: $width * $scale, $height * $scale
  30. imagecopyresampled($result_id, $sourcefile_id, 0, 0, 0, 0, $destWidth, $destHeight, $width, $height);
  31. ## Return the JPG
  32. imagejpeg ($result_id, $targetfile, 90);
  33. ## Wrap it up
  34. imagedestroy($sourcefile_id);
  35. imagedestroy($result_id);
  36. }
  37. ## Function to process all screenshots for the requested game id
  38. function processScreenshots($gameID)
  39. {
  40. ## Select all fanart rows for the requested game id
  41. $ssResult = mysql_query(" SELECT filename FROM banners WHERE keyvalue = $gameID AND keytype = 'screenshot' ORDER BY filename ASC ");
  42. ## Process each fanart row incrementally
  43. while($ssRow = mysql_fetch_assoc($ssResult))
  44. {
  45. ## Construct file names
  46. $ssOriginal = $ssRow['filename'];
  47. $ssThumb = "screenshots/thumb" . str_replace("screenshots", "", $ssRow['filename']);
  48. ## Check to see if the original fanart file actually exists before attempting to process
  49. if(file_exists("../banners/$ssOriginal"))
  50. {
  51. ## Check if thumb already exists
  52. if(!file_exists("../banners/$ssThumb"))
  53. {
  54. ## If thumb is non-existant then create it
  55. $image = new SimpleImage();
  56. $image->load("../banners/$ssOriginal");
  57. $image->resizeToWidth(300);
  58. $image->save("../banners/$ssThumb");
  59. //makeFanartThumb("../banners/$ssOriginal", "../banners/$ssThumb");
  60. }
  61. ## Get Fanart Image Dimensions
  62. list($image_width, $image_height, $image_type, $image_attr) = getimagesize("../banners/$ssOriginal");
  63. $ssWidth = $image_width;
  64. $ssHeight = $image_height;
  65. ## Output Fanart XML Branch
  66. print "<screenshot>\n";
  67. print "<original width=\"$ssWidth\" height=\"$ssHeight\">$ssOriginal</original>\n";
  68. print "<thumb>$ssThumb</thumb>\n";
  69. print "</screenshot>\n";
  70. }
  71. }
  72. }
  73. ## Function to process all fanart for the requested game id
  74. function processFanart($gameID)
  75. {
  76. ## Select all fanart rows for the requested game id
  77. $faResult = mysql_query(" SELECT filename FROM banners WHERE keyvalue = $gameID AND keytype = 'fanart' ORDER BY filename ASC ");
  78. ## Process each fanart row incrementally
  79. while($faRow = mysql_fetch_assoc($faResult))
  80. {
  81. ## Construct file names
  82. $faOriginal = $faRow['filename'];
  83. $faThumb = str_replace("original", "thumb", $faRow['filename']);
  84. ## Check to see if the original fanart file actually exists before attempting to process
  85. if(file_exists("../banners/$faOriginal"))
  86. {
  87. ## Check if thumb already exists
  88. if(!file_exists("../banners/$faThumb"))
  89. {
  90. ## If thumb is non-existant then create it
  91. makeFanartThumb("../banners/$faOriginal", "../banners/$faThumb");
  92. }
  93. ## Get Fanart Image Dimensions
  94. list($image_width, $image_height, $image_type, $image_attr) = getimagesize("../banners/$faOriginal");
  95. $faWidth = $image_width;
  96. $faHeight = $image_height;
  97. ## Output Fanart XML Branch
  98. print "<fanart>\n";
  99. print "<original width=\"$faWidth\" height=\"$faHeight\">$faOriginal</original>\n";
  100. print "<thumb>$faThumb</thumb>\n";
  101. print "</fanart>\n";
  102. }
  103. }
  104. }
  105. function processBoxart($gameID)
  106. {
  107. ## Select all boxart rows for the requested game id
  108. $baResult = mysql_query(" SELECT filename FROM banners WHERE keyvalue = $gameID AND keytype = 'boxart' ORDER BY filename ASC ");
  109. ## Process each boxart row incrementally
  110. while($baRow = mysql_fetch_assoc($baResult))
  111. {
  112. ## Construct file names
  113. $baOriginal = $baRow['filename'];
  114. $type = (preg_match('/front/', $baOriginal)) ? 'front' : 'back';
  115. ## Check to see if the original boxart file actually exists before attempting to process
  116. if(file_exists("../banners/$baOriginal"))
  117. {
  118. ## Get boxart image dimensions
  119. list($image_width, $image_height, $image_type, $image_attr) = getimagesize("../banners/$baOriginal");
  120. $baWidth = $image_width;
  121. $baHeight = $image_height;
  122. ## Output Boxart XML Branch
  123. echo "<boxart side=\"$type\" width=\"$baWidth\" height=\"$baHeight\">$baOriginal</boxart>\n";
  124. }
  125. }
  126. }
  127. function processBanner($gameID)
  128. {
  129. ## Select all boxart rows for the requested game id
  130. $banResult = mysql_query(" SELECT filename FROM banners WHERE keyvalue = $gameID AND keytype = 'series' ORDER BY filename ASC ");
  131. ## Process each boxart row incrementally
  132. while($banRow = mysql_fetch_assoc($banResult))
  133. {
  134. ## Construct file names
  135. $banOriginal = $banRow['filename'];
  136. ## Check to see if the original boxart file actually exists before attempting to process
  137. if(file_exists("../banners/$banOriginal"))
  138. {
  139. ## Output Boxart XML Branch
  140. echo "<banner width=\"760\" height=\"140\">$banOriginal</banner>";
  141. }
  142. }
  143. }
  144. function processClearLOGO($gameID)
  145. {
  146. ## Select all boxart rows for the requested game id
  147. $clResult = mysql_query(" SELECT filename, resolution FROM banners WHERE keyvalue = $gameID AND keytype = 'clearlogo' LIMIT 1 ");
  148. ## Process each boxart row incrementally
  149. while($clRow = mysql_fetch_assoc($clResult))
  150. {
  151. ## Construct file names
  152. $clOriginal = $clRow['filename'];
  153. $clResolution = $clRow['resolution'];
  154. $clResolution = explode("x", $clResolution, 2);
  155. ## Check to see if the original boxart file actually exists before attempting to process
  156. if(file_exists("../banners/$clOriginal"))
  157. {
  158. ## Output Boxart XML Branch
  159. echo "<clearlogo width=\"$clResolution[0]\" height=\"$clResolution[1]\">$clOriginal</clearlogo>";
  160. }
  161. }
  162. }
  163. ###===============###
  164. ###MAIN XML OUTPUT###
  165. ###-----------------------------###
  166. print "<Data>\n";
  167. print "<baseImgUrl>http://thegamesdb.net/banners/</baseImgUrl>\n";
  168. ## Open Images XML Branch
  169. print "<Images>\n";
  170. processFanart($requestedID);
  171. processBoxart($requestedID);
  172. processBanner($requestedID);
  173. processScreenshots($requestedID);
  174. processClearLOGO($requestedID);
  175. ## Close Images XML Branch
  176. print "</Images>\n";
  177. print "</Data>";
  178. ?>