index.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. // Read config file for settings
  3. require_once("config.php");
  4. // Allow cross site posting
  5. header("Access-Control-Allow-Origin: *");
  6. // To print the max file size in human readable format
  7. $max_filesize_msg = human_readable_size($max_file_size, 0);
  8. // If file has been posted
  9. if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
  10. try {
  11. if (!file_exists($image_path))
  12. mkdir($image_path, 0777, true);
  13. if (!file_exists($thumb_path))
  14. mkdir($thumb_path, 0777, true);
  15. //getting file name
  16. $file_name = $_FILES['file']['name'];
  17. //getting temp_name of file
  18. $tmp_name = $_FILES['file']['tmp_name'];
  19. // Ensure the file has image size parameters
  20. $image_info = @getimagesize($tmp_name);
  21. if ($image_info == false)
  22. throw new Exception('Please upload valid image file.');
  23. // Check the types of files that are allowed to be uploaded
  24. $type = $_FILES['file']['type'];
  25. if (!in_array($type, $allowed_types))
  26. throw new Exception("Only jpg, jpeg, png, webp, and gif image type supported.");
  27. // Check the file size is acceptable
  28. if (filesize($tmp_name) > $max_file_size)
  29. throw new Exception("File over allowed size of {$max_filesize_msg}");
  30. // Get the extension of the file
  31. $file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
  32. $file_id = rand_str(6);
  33. // Convert webp files into jpg image
  34. if ($type == 'image/webp') {
  35. $new_file_name = "{$file_id}.jpg";
  36. $new_thumb_name = "{$file_id}_thumb.jpg";
  37. // Convert webp to jpeg with 80% quality and save to save path
  38. $im = imagecreatefromwebp($tmp_name);
  39. imagejpeg($im, $image_path . $new_file_name, 80);
  40. imagedestroy($im);
  41. }
  42. // Resize image if it's JPEG and more then 1200px in any side
  43. elseif ($type == 'image/jpeg' && ($image_info[0] > $max_image_size || $image_info[1] > $max_image_size)) {
  44. $new_file_name = "{$file_id}.jpg";
  45. $new_thumb_name = "{$file_id}_thumb.jpg";
  46. // Resize image to a smaller size
  47. $source = $tmp_name;
  48. $dest = $image_path . $new_file_name;
  49. resizeAndSaveImage($source, $dest, $max_image_size);
  50. }
  51. // Move the uploaded file to save path
  52. else {
  53. $new_file_name = "{$file_id}.{$file_ext}";
  54. $new_thumb_name = "{$file_id}_thumb.{$file_ext}";
  55. // Move the uploaded file to save path (for all other formats)
  56. move_uploaded_file($tmp_name, $image_path . $new_file_name);
  57. }
  58. // Generate thumbnails
  59. $source = $image_path . $new_file_name;
  60. $dest = $thumb_path . $new_thumb_name;
  61. resizeAndSaveImage($source, $dest, 150);
  62. // Generate the file url and reply
  63. $url = $protocol . $domain . $image_url . $new_file_name;
  64. $out = array("status" => "OK", "url" => $url);
  65. echo json_encode($out);
  66. }
  67. // Catch and print the exception
  68. catch (Exception $e) {
  69. $out = array("status" => "FAIL", "msg" => $e->getMessage());
  70. echo json_encode($out);
  71. }
  72. } else {
  73. $v = "?v=" . rand(1111, 9999);
  74. ?>
  75. <!DOCTYPE html>
  76. <html lang="en">
  77. <head>
  78. <meta charset="UTF-8">
  79. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  80. <title>Mini Image Host</title>
  81. <link rel="stylesheet" href="style.css<?= $v ?>">
  82. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" />
  83. </head>
  84. <body>
  85. <?php
  86. // Display the images in gallery
  87. if (isset($_REQUEST['gallery'])) {
  88. $files = scandir($image_path);
  89. ?>
  90. <div class="wrapper wrapper-big">
  91. <i class="fas fa-upload" onclick="location.href='.';"></i>
  92. <header onclick="location.href='.';">Mini Image Host</header>
  93. <div style="margin: 30px 0;">
  94. <?php
  95. $i = 0;
  96. foreach ($files as $file) {
  97. $parts = explode(".", $file);
  98. $thumb_name = $parts[0] . "_thumb." . $parts[1];
  99. $file_url = $protocol . $domain . $image_url . $file;
  100. $file_exists = false;
  101. if (file_exists($thumb_path . $thumb_name)) {
  102. $file_exists = true;
  103. $img_scr_url = $protocol . $domain . $thumb_url . $thumb_name;
  104. } elseif (!is_dir($image_path . $file)) {
  105. $file_exists = true;
  106. $img_scr_url = $file_url;
  107. }
  108. if ($file_exists) {
  109. ?>
  110. <div class="popup" onclick="showPopup('popup_<?= $i ?>','<?= $file_url ?>')">
  111. <img src="<?= $img_scr_url ?>" alt="<?= $file_url ?>" />
  112. <span class="popuptext" id="popup_<?= $i ?>">Link copied.</span>
  113. </div>
  114. <?php
  115. $i++;
  116. }
  117. }
  118. ?>
  119. </div>
  120. </div>
  121. <?php } else { ?>
  122. <div class="wrapper">
  123. <header onclick="location.href='.';">Mini Image Host</header>
  124. <div class="mirror-div">
  125. Select mirror:
  126. <select id="mirror">
  127. <?php
  128. foreach ($mirror_list as $mirror => $host) {
  129. echo "<option value=\"$host\">$mirror</option>";
  130. }
  131. ?>
  132. </select>
  133. <i class="fas fa-image" onclick="location.href='/?gallery';" style="float:right; font-size: 20pt;"></i>
  134. </div>
  135. <form action="#">
  136. <input class="file-input" type="file" name="file" hidden>
  137. <i class="fas fa-cloud-upload-alt"></i>
  138. <p>Browse Image to Upload</p>
  139. <span class="small">Max file size:
  140. <?= $max_filesize_msg ?>
  141. </span>
  142. </form>
  143. <section class="progress-area"></section>
  144. <section class="uploaded-area"></section>
  145. </div>
  146. <script>
  147. const form = document.querySelector("form");
  148. const fileInput = document.querySelector(".file-input");
  149. const progressArea = document.querySelector(".progress-area");
  150. const uploadedArea = document.querySelector(".uploaded-area");
  151. // form click event
  152. form.addEventListener("click", () => {
  153. fileInput.click();
  154. });
  155. fileInput.onchange = ({
  156. target
  157. }) => {
  158. let file = target.files[0];
  159. if (file) {
  160. let fileName = file.name;
  161. if (fileName.length >= 12) {
  162. let lastIndex = fileName.lastIndexOf('.');
  163. let name = fileName.slice(0, lastIndex);
  164. let ext = fileName.slice(lastIndex + 1);
  165. fileName = name.substring(0, 13) + "... ." + ext;
  166. //let splitName = fileName.split('.');
  167. //fileName = splitName[0].substring(0, 13) + "... ." + splitName[1];
  168. }
  169. let mirror = document.getElementById("mirror").value;
  170. if (mirror.length > 0) {
  171. mirror = "https://" + mirror + "/";
  172. }
  173. uploadFile(fileName, mirror);
  174. }
  175. }
  176. </script>
  177. <?php } ?>
  178. <script src="script.js<?= $v ?>"></script>
  179. </body>
  180. </html>
  181. <?php
  182. }
  183. // Function used to generate a random string for filename
  184. function rand_str($length = 10)
  185. {
  186. $characters = '23456789abcdefghjkmnpqrtuvwxyzABCDEFGHJKLMNPQRTUVWXYZ';
  187. $charactersLength = strlen($characters);
  188. $randomString = '';
  189. for ($i = 0; $i < $length; $i++) {
  190. $randomString .= $characters[mt_rand(0, $charactersLength - 1)];
  191. }
  192. return $randomString;
  193. }
  194. // Conver byte size to human redable size
  195. function human_readable_size($raw_size, $return_array = true)
  196. {
  197. $size_arr = array("B", "KB", "MB", "GB", "TB", "PB");
  198. $max = count($size_arr) - 1;
  199. for ($i = $max; $i >= 0; $i--) {
  200. $value = pow(1024, $i);
  201. if ($raw_size > $value) {
  202. $size_hr = round(($raw_size / $value), 2);
  203. return $return_array ? array($size_hr, $size_arr[$i]) : "{$size_hr} {$size_arr[$i]}";
  204. }
  205. }
  206. }
  207. // Resize and save image
  208. function resizeAndSaveImage($source, $dest, $maxSize = 200)
  209. {
  210. // get source image size
  211. $img_details = getimagesize($source);
  212. $w = $img_details[0];
  213. $h = $img_details[1];
  214. $img_type = $img_details[2];
  215. // specifying the required image size
  216. if ($w > $h) {
  217. $new_width = $maxSize;
  218. $new_height = ceil($maxSize * $h / $w);
  219. } else {
  220. $new_height = $maxSize;
  221. $new_width = ceil($maxSize * $w / $h);
  222. }
  223. if ($img_type == IMAGETYPE_GIF) {
  224. $imgt = "ImageGIF";
  225. $imgcreatefrom = "ImageCreateFromGIF";
  226. } elseif ($img_type == IMAGETYPE_JPEG) {
  227. $imgt = "ImageJPEG";
  228. $imgcreatefrom = "ImageCreateFromJPEG";
  229. } elseif ($img_type == IMAGETYPE_PNG) {
  230. $imgt = "ImagePNG";
  231. $imgcreatefrom = "ImageCreateFromPNG";
  232. }
  233. if ($imgt) {
  234. $old_image = $imgcreatefrom($source);
  235. $new_image = imagecreatetruecolor($new_width, $new_height);
  236. imagecopyresized($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $w, $h);
  237. $save = $imgt($new_image, $dest);
  238. }
  239. }