1
0

upload_map_image.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) 2008 - 2018 The OGP Development Team
  6. *
  7. * http://www.opengamepanel.org/
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. */
  24. require_once("modules/config_games/server_config_parser.php");
  25. function smart_resize_image($file,
  26. $string = null,
  27. $width = 0,
  28. $height = 0,
  29. $proportional = false,
  30. $output = 'file',
  31. $delete_original = true,
  32. $use_linux_commands = false,
  33. $quality = 100) {
  34. if ( $height <= 0 && $width <= 0 ) return false;
  35. if ( $file === null && $string === null ) return false;
  36. # Setting defaults and meta
  37. $info = $file !== null ? getimagesize($file) : getimagesizefromstring($string);
  38. $image = '';
  39. $final_width = 0;
  40. $final_height = 0;
  41. list($width_old, $height_old) = $info;
  42. $cropHeight = $cropWidth = 0;
  43. # Calculating proportionality
  44. if ($proportional)
  45. {
  46. if ($width == 0)$factor = $height/$height_old;
  47. elseif ($height == 0)$factor = $width/$width_old;
  48. else $factor = min( $width / $width_old, $height / $height_old );
  49. $final_width = round( $width_old * $factor );
  50. $final_height = round( $height_old * $factor );
  51. }
  52. else
  53. {
  54. $final_width = ( $width <= 0 ) ? $width_old : $width;
  55. $final_height = ( $height <= 0 ) ? $height_old : $height;
  56. $widthX = $width_old / $width;
  57. $heightX = $height_old / $height;
  58. $x = min($widthX, $heightX);
  59. $cropWidth = ($width_old - $width * $x) / 2;
  60. $cropHeight = ($height_old - $height * $x) / 2;
  61. }
  62. # Loading image to memory according to type
  63. switch ( $info[2] ) {
  64. case IMAGETYPE_JPEG: $file !== null ? $image = imagecreatefromjpeg($file) : $image = imagecreatefromstring($string); break;
  65. case IMAGETYPE_GIF: $file !== null ? $image = imagecreatefromgif($file) : $image = imagecreatefromstring($string); break;
  66. case IMAGETYPE_PNG: $file !== null ? $image = imagecreatefrompng($file) : $image = imagecreatefromstring($string); break;
  67. default: return false;
  68. }
  69. # This is the resizing/resampling/transparency-preserving magic
  70. $image_resized = imagecreatetruecolor( $final_width, $final_height );
  71. if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
  72. $transparency = imagecolortransparent($image);
  73. $palletsize = imagecolorstotal($image);
  74. if ($transparency >= 0 && $transparency < $palletsize)
  75. {
  76. $transparent_color = imagecolorsforindex($image, $transparency);
  77. $transparency = imagecolorallocate($image_resized, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
  78. imagefill($image_resized, 0, 0, $transparency);
  79. imagecolortransparent($image_resized, $transparency);
  80. }
  81. elseif ($info[2] == IMAGETYPE_PNG)
  82. {
  83. imagealphablending($image_resized, false);
  84. $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
  85. imagefill($image_resized, 0, 0, $color);
  86. imagesavealpha($image_resized, true);
  87. }
  88. }
  89. imagecopyresampled($image_resized, $image, 0, 0, $cropWidth, $cropHeight, $final_width, $final_height, $width_old - 2 * $cropWidth, $height_old - 2 * $cropHeight);
  90. # Taking care of original, if needed
  91. if ( $delete_original )
  92. {
  93. if ( $use_linux_commands ) exec('rm '.$file);
  94. else @unlink($file);
  95. }
  96. # Preparing a method of providing result
  97. switch ( strtolower($output) )
  98. {
  99. case 'browser':
  100. $mime = image_type_to_mime_type($info[2]);
  101. header("Content-type: $mime");
  102. $output = NULL;
  103. break;
  104. case 'file':
  105. $output = $file;
  106. break;
  107. case 'return':
  108. return $image_resized;
  109. break;
  110. default:
  111. break;
  112. }
  113. # Writing image according to type to the output destination and image quality
  114. switch ( $info[2] ) {
  115. case IMAGETYPE_GIF: imagegif($image_resized, $output); break;
  116. case IMAGETYPE_JPEG: imagejpeg($image_resized, $output, $quality); break;
  117. case IMAGETYPE_PNG:
  118. $quality = 9 - (int)((0.9*$quality)/10.0);
  119. imagepng($image_resized, $output, $quality);
  120. break;
  121. default: return false;
  122. }
  123. return true;
  124. }
  125. function exec_ogp_module() {
  126. global $db;
  127. if( isset($_POST['home_id']) and isset($_POST['mod_id']) and isset($_POST['map']) and isset($_POST['extension']) )
  128. {
  129. $home_id = $_POST['home_id'];
  130. $mod_id = $_POST['mod_id'];
  131. $map = $_POST['map'];
  132. $extension = $_POST['extension'];
  133. $home_info = $db->getGameHome($home_id);
  134. $mods = $home_info['mods'];
  135. $current_mod_info = $mods[$mod_id];
  136. $mod_name = $current_mod_info['mod_name'];
  137. $mod_key = $current_mod_info['mod_key'];
  138. if ( strtolower($mod_name) == "none")
  139. $mod = $mod_key;
  140. else
  141. $mod = $mod_name;
  142. $server_xml = read_server_config(SERVER_CONFIG_LOCATION."/".$home_info['home_cfg_file']);
  143. if ($server_xml->protocol == "gameq")
  144. $query_name = $server_xml->gameq_query_name;
  145. elseif ($server_xml->protocol == "lgsl")
  146. $query_name = $server_xml->lgsl_query_name;
  147. else
  148. $query_name = $mod; // If query name does not exist use mod key instead.
  149. $dest_path = "protocol". DIRECTORY_SEPARATOR ."lgsl". DIRECTORY_SEPARATOR ."maps". DIRECTORY_SEPARATOR ."$query_name". DIRECTORY_SEPARATOR ."$mod";
  150. $dest_file = $dest_path . DIRECTORY_SEPARATOR . "$map.$extension";
  151. if( $_FILES['map-image']['error'] == 0 )
  152. {
  153. if( !file_exists($dest_path))
  154. {
  155. if(!@mkdir($dest_path, 0700, true))
  156. {
  157. echo get_lang_f('cant_create_folder',$dest_path);
  158. return;
  159. }
  160. }
  161. if(@move_uploaded_file( $_FILES["map-image"]["tmp_name"], $dest_file ))
  162. {
  163. print_lang('uploaded_successfully');
  164. smart_resize_image($dest_file, null, 160, 120, true);
  165. }
  166. else
  167. {
  168. echo get_lang_f('cant_write_file',$dest_file);
  169. }
  170. }
  171. else
  172. {
  173. switch ($_FILES['map-image']['error'])
  174. {
  175. case UPLOAD_ERR_INI_SIZE:
  176. echo get_lang_f('exceeded_php_directive','upload_max_filesize='.ini_get('upload_max_filesize'));
  177. case UPLOAD_ERR_FORM_SIZE:
  178. echo get_lang_f('exceeded_php_directive','post_max_size='.ini_get('post_max_size'));
  179. default:
  180. print_lang('unknown_errors');
  181. }
  182. }
  183. }
  184. }
  185. ?>