scriptz-team

[PHP] ImgUr Uploader

Jun 6th, 2012
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. <?php
  2. /* _____ _____ _ _____ _____ _____ _____ _____ _____
  3. ___| | __ |_| _ |_ _|___ ___|_ _| __| _ | |
  4. |_ -| --| -| | __| | | |- _|___| | | | __| | | | |
  5. |___|_____|__|__|_|__| |_| |___| |_| |_____|__|__|_|_|_|
  6. |s C R i P T z - T E A M . i N F O|----------------------------
  7.  
  8. [PHP] ImgUr Uploader
  9.  
  10. */
  11. error_reporting(0);
  12. ?>
  13. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  14. <html xmlns="http://www.w3.org/1999/xhtml">
  15. <head>
  16. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  17. <title>ImgUr Uploader</title>
  18. <style type="text/css">
  19. html {
  20. background: #f1f1f1;
  21. }
  22. a {
  23. color: #5783ad;
  24. }
  25. body {
  26. background: #fff;
  27. margin: 22px auto;
  28. width: 764px;
  29. padding: 22px;
  30. text-align: center;
  31. font-family: Georgia, "Times New Roman", Times, serif;
  32. color: #464646;
  33. }
  34. h2 {
  35. font-size: 3em;
  36. font-weight: normal;
  37. }
  38. #green_box {
  39. background: none repeat scroll 0 0 #D5E7C8;
  40. border: 1px solid #A4D083;
  41. color: #4E8426;
  42. font-size: 11px;
  43. line-height: 190%;
  44. margin-bottom: 5px;
  45. padding: 8px;
  46. }
  47. #red_box {
  48. background: none repeat scroll 0 0 #F3E3E6;
  49. border: 1px solid #E599AA;
  50. color: #80001C;
  51. font-size: 11px;
  52. line-height: 190%;
  53. margin-bottom: 5px;
  54. padding: 8px;
  55. }
  56. #big {
  57. font-size: 20px;
  58. font-weight: bold;
  59. font-family: Calibri,Verdana;
  60. }
  61. </style>
  62. </head>
  63. <body>
  64. <h2>ImgUr Uploader</h2>
  65. <div id="content" align="center">
  66. <?php
  67. if (isset($_FILES['userfile']) && $_FILES['userfile'] != "") {
  68. $api_key = "37fa9221ba13d0f9050ece201934238f";
  69. $file = getcwd() . '/' . basename($_FILES['userfile']['name']);
  70. move_uploaded_file($_FILES['userfile']['tmp_name'], $file);
  71. list($width, $height, $file_type) = getimagesize($file);
  72.  
  73. if ($file_type == 3) {
  74. $image = imagecreatefrompng($file);
  75. imagealphablending($image, false);
  76. imagesavealpha($image, true);
  77. ob_start();
  78. imagepng($image);
  79. $data = ob_get_contents();
  80. ob_end_clean();
  81. }
  82.  
  83. if ($file_type == 2) {
  84. $image = imagecreatefromjpeg($file);
  85. imagealphablending($image, false);
  86. imagesavealpha($image, true);
  87. ob_start();
  88. imagejpeg($image);
  89. $data = ob_get_contents();
  90. ob_end_clean();
  91. }
  92.  
  93. if ($file_type == 1) {
  94. $image = imagecreatefromgif($file);
  95. imagealphablending($image, false);
  96. imagesavealpha($image, true);
  97. ob_start();
  98. imagegif($image);
  99. $data = ob_get_contents();
  100. ob_end_clean();
  101. }
  102.  
  103. $pvars = array(
  104. 'image' => base64_encode($data),
  105. 'key' => $api_key
  106. );
  107. $timeout = 30;
  108. $curl = curl_init();
  109. $post = http_build_query($pvars);
  110.  
  111. curl_setopt($curl, CURLOPT_URL, 'http://imgur.com/api/upload.xml');
  112. curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
  113. curl_setopt($curl, CURLOPT_POST, 1);
  114. curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
  115. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  116. "Content-type: application/x-www-form-urlencoded"
  117. ));
  118. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  119. $xml_raw = curl_exec($curl);
  120. curl_close($curl);
  121. unlink($file);
  122.  
  123. $xml = new SimpleXMLElement($xml_raw);
  124.  
  125. if ($xml->error_code != '') {
  126. $imgur_error_code = $xml->error_code;
  127. $imgur_error_msg = $xml->error_msg;
  128.  
  129. settype($imgur_error_code, "string");
  130. settype($imgur_error_msg, "string");
  131.  
  132. echo '<div id="red_box"><h1>Error #' . $imgur_error_code . ', ' . $imgur_error_msg . '</h1></div>';
  133. die();
  134. } else {
  135. imagedestroy($image);
  136. $imgur_original = $xml->original_image;
  137. $imgur_large_tbn = $xml->large_thumbnail;
  138. $imgur_small_tbn = $xml->small_thumbnail;
  139. $imgur_image_hash = $xml->image_hash;
  140. $imgur_delete_hash = $xml->delete_hash;
  141. $imgur_page = $xml->imgur_page;
  142. $img_delete_page = $xml->delete_page;
  143.  
  144. settype($imgur_original, "string");
  145. settype($imgur_large_tbn, "string");
  146. settype($imgur_small_tbn, "string");
  147.  
  148. ?>
  149.  
  150. <div id="green_box">
  151. <div align="center">
  152.  
  153. <div id="insert_box_img">
  154. <h1>Small thumbnail :</h1>
  155. <img src="<?php
  156. echo $imgur_small_tbn;
  157. ?>" border="0" />
  158.  
  159. <h1>Large thumbnail :</h1>
  160. <img src="<?php
  161. echo $imgur_large_tbn;
  162. ?>" border="0" />
  163. </div>
  164. </div>
  165.  
  166. </div>
  167. <?php
  168. }
  169. die();
  170. }
  171. ?>
  172.  
  173. <form enctype="multipart/form-data" action="" method="POST">
  174. Image: <input name="userfile" type="file" id="big"/>
  175. <input type="submit" value="Upload" id="big"/>
  176. </form>
  177. </div>
  178. </body>
  179. </html>
Add Comment
Please, Sign In to add comment