1
0

Images.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace Sakura\API;
  3. class Images
  4. {
  5. private $chevereto_api_key;
  6. private $imgur_client_id;
  7. private $smms_client_id;
  8. private $lsky_api_key;
  9. public function __construct() {
  10. $this->chevereto_api_key = iro_opt('chevereto_api_key');
  11. $this->lsky_api_key = iro_opt('lsky_api_key');
  12. $this->imgur_client_id = iro_opt('imgur_client_id');
  13. $this->smms_client_id = iro_opt('smms_client_id');
  14. }
  15. /**
  16. * LSky Pro upload interface
  17. */
  18. public function LSKY_API($image) {
  19. $upload_url = iro_opt('lsky_url') . '/api/v1/upload';
  20. $filename = $image['cmt_img_file']['name'];
  21. $filedata = $image['cmt_img_file']['tmp_name'];
  22. $Boundary = wp_generate_password();
  23. $bits = file_get_contents($filedata);
  24. $args = array(
  25. 'headers' => array(
  26. 'Authorization' => 'Bearer ' . $this->lsky_api_key,
  27. 'Accept' => 'application/json',
  28. 'Content-Type' => 'multipart/form-data; boundary='.$Boundary,
  29. ),
  30. 'body' => "--$Boundary\r\nContent-Disposition: form-data; name=\"file\"; filename=\"$filename\"\r\n\r\n$bits\r\n\r\n--$Boundary--"
  31. );
  32. $response = wp_remote_post($upload_url, $args);
  33. $reply = json_decode($response['body']);
  34. if ($reply->status == true) {
  35. $status = 200;
  36. $success = true;
  37. $message = 'success';
  38. $link = $reply->data->links->url;
  39. $proxy = iro_opt('comment_image_proxy') . $link;
  40. } else {
  41. $status = 400;
  42. $success = false;
  43. $message = $reply->message;
  44. $link = 'https://view.moezx.cc/images/2019/10/28/default_d_h_large.gif';
  45. $proxy = iro_opt('comment_image_proxy') . $link;
  46. }
  47. $output = array(
  48. 'status' => $status,
  49. 'success' => $success,
  50. 'message' => $message,
  51. 'link' => $link,
  52. 'proxy' => $proxy,
  53. );
  54. return $output;
  55. }
  56. /**
  57. * Chevereto upload interface
  58. */
  59. public function Chevereto_API($image) {
  60. $upload_url = iro_opt('cheverto_url') . '/api/1/upload';
  61. $args = array(
  62. 'body' => array(
  63. 'source' => base64_encode($image),
  64. 'key' => $this->chevereto_api_key,
  65. ),
  66. );
  67. $response = wp_remote_post($upload_url, $args);
  68. $reply = json_decode($response['body']);
  69. if ($reply->status_txt == 'OK' && $reply->status_code == 200) {
  70. $status = 200;
  71. $success = true;
  72. $message = 'success';
  73. $link = $reply->image->image->url;
  74. $proxy = iro_opt('comment_image_proxy') . $link;
  75. } else {
  76. $status = $reply->status_code;
  77. $success = false;
  78. $message = $reply->error->message;
  79. $link = 'https://view.moezx.cc/images/2019/10/28/default_d_h_large.gif';
  80. $proxy = iro_opt('comment_image_proxy') . $link;
  81. }
  82. $output = array(
  83. 'status' => $status,
  84. 'success' => $success,
  85. 'message' => $message,
  86. 'link' => $link,
  87. 'proxy' => $proxy,
  88. );
  89. return $output;
  90. }
  91. /**
  92. * Imgur upload interface
  93. */
  94. public function Imgur_API($image) {
  95. $upload_url = iro_opt('imgur_upload_image_proxy');
  96. $args = array(
  97. 'headers' => array(
  98. 'Authorization' => 'Client-ID ' . $this->imgur_client_id,
  99. ),
  100. 'body' => array(
  101. 'image' => base64_encode($image),
  102. ),
  103. );
  104. $response = wp_remote_post($upload_url, $args);
  105. $reply = json_decode($response['body']);
  106. if ($reply->success && $reply->status == 200) {
  107. $status = 200;
  108. $success = true;
  109. $message = 'success';
  110. $link = $reply->data->link;
  111. $proxy = iro_opt('comment_image_proxy') . $link;
  112. } else {
  113. $status = $reply->status;
  114. $success = false;
  115. $message = $reply->data->error;
  116. $link = 'https://view.moezx.cc/images/2019/10/28/default_d_h_large.gif';
  117. $proxy = iro_opt('comment_image_proxy') . $link;
  118. }
  119. $output = array(
  120. 'status' => $status,
  121. 'success' => $success,
  122. 'message' => $message,
  123. 'link' => $link,
  124. 'proxy' => $proxy,
  125. );
  126. return $output;
  127. }
  128. /**
  129. * smms upload interface
  130. */
  131. public function SMMS_API($image) {
  132. $client_id = $this->smms_client_id;
  133. $upload_url = 'https://sm.ms/api/v2/upload';
  134. $filename = $image['cmt_img_file']['name'];
  135. $filedata = $image['cmt_img_file']['tmp_name'];
  136. $Boundary = wp_generate_password();
  137. $bits = file_get_contents($filedata);
  138. $args = array(
  139. 'headers' => "Content-Type: multipart/form-data; boundary=$Boundary\r\n\r\nAuthorization: Basic $client_id\r\n\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97",
  140. 'body' => "--$Boundary\r\nContent-Disposition: form-data; name=\"smfile\"; filename=\"$filename\"\r\n\r\n$bits\r\n\r\n--$Boundary--"
  141. );
  142. $response = wp_remote_post($upload_url, $args);
  143. $reply = json_decode($response['body']);
  144. if ($reply->success && $reply->code == 'success') {
  145. $status = 200;
  146. $success = true;
  147. $message = $reply->message;
  148. $link = $reply->data->url;
  149. $proxy = iro_opt('comment_image_proxy') . $link;
  150. } else if (preg_match("/Image upload repeated limit/i", $reply->message, $matches)) {
  151. $status = 200; // sm.ms 接口不规范,建议检测到重复的情况下返回标准化的 code,并单独把 url 放进一个字段
  152. $success = true;
  153. $message = $reply->message;
  154. $link = str_replace('Image upload repeated limit, this image exists at: ', '', $reply->message);
  155. $proxy = iro_opt('comment_image_proxy') . $link;
  156. } else {
  157. $status = 400;
  158. $success = false;
  159. $message = $reply->message;
  160. $link = 'https://view.moezx.cc/images/2019/10/28/default_d_h_large.gif';
  161. $proxy = iro_opt('comment_image_proxy') . $link;
  162. }
  163. $output = array(
  164. 'status' => $status,
  165. 'success' => $success,
  166. 'message' => $message,
  167. 'link' => $link,
  168. 'proxy' => $proxy,
  169. );
  170. return $output;
  171. }
  172. public static function cover_gallery($size = 'source') {
  173. if (iro_opt('random_graphs_options') == 'local') {
  174. $img_array = glob(STYLESHEETPATH . '/manifest/gallary/*.{gif,jpg,jpeg,png}', GLOB_BRACE);
  175. if (count($img_array) == 0){
  176. return ['status'=>False,'msg'=>'ERROR:请联系管理员查看gallary目录中是否存在图片!'];
  177. }
  178. $img = array_rand($img_array);
  179. $imgurl = trim($img_array[$img]);
  180. $imgurl = str_replace(STYLESHEETPATH, get_template_directory_uri(), $imgurl);
  181. } elseif (iro_opt('random_graphs_options') == 'external_api') {
  182. $imgurl = iro_opt('random_graphs_link');
  183. } else {
  184. // global $sakura_image_array;
  185. $temp = file_get_contents(STYLESHEETPATH . "/manifest/manifest.json");
  186. // $img_array = json_decode($sakura_image_array, true);
  187. $img_array = json_decode($temp, true);
  188. if (!$img_array){
  189. return ['status'=>False,'msg'=>'ERROR:请联系管理员查看manifest.json中是否存在图片!'];
  190. }
  191. $array_keys = array_keys($img_array);
  192. $img = array_rand($array_keys);
  193. if (iro_opt('random_graphs_link',NULL) && iro_opt('random_graphs_link') != home_url( '/')) {
  194. $img_domain = iro_opt('random_graphs_link');
  195. } else {
  196. $img_domain = get_template_directory_uri();
  197. }
  198. $format = strpos($_SERVER['HTTP_ACCEPT'], 'image/webp') ? 'webp' : 'jpeg';
  199. $imgurl = "{$img_domain}/manifest/{$format}/{$array_keys[$img]}.{$size}.{$format}";
  200. }
  201. return ['status'=>True,'url'=>$imgurl];
  202. }
  203. public static function mobile_cover_gallery() {
  204. if (iro_opt('random_graphs_options') == 'local') {
  205. $img_array = glob(STYLESHEETPATH . '/manifest/gallary/*.{gif,jpg,jpeg,png}', GLOB_BRACE);
  206. if (count($img_array) == 0){
  207. return ['status'=>False,'msg'=>'没有找到图片,请联系管理员检查gallary目录下是否存在图片'];
  208. }
  209. $img = array_rand($img_array);
  210. $imgurl = trim($img_array[$img]);
  211. $imgurl = str_replace(STYLESHEETPATH, get_template_directory_uri(), $imgurl);
  212. } elseif (iro_opt('random_graphs_options') == 'external_api') {
  213. //$imgurl = iro_opt('random_graphs_link');
  214. $imgurl = iro_opt('random_graphs_link_mobile');
  215. } else {
  216. // global $sakura_mobile_image_array;
  217. $temp = file_get_contents(STYLESHEETPATH . '/manifest/manifest_mobile.json');
  218. // $img_array = json_decode($sakura_mobile_image_array, true);
  219. $img_array = json_decode($temp, true);
  220. if (!$img_array){
  221. return ['status'=>False,'msg'=>'没有找到图片,请联系管理员检查manifest_mobile.json下是否存在图片'];
  222. }
  223. $array_keys = array_keys($img_array);
  224. $img = array_rand($array_keys);
  225. // $img_domain = iro_opt('random_graphs_link_mobile') ?: get_template_directory_uri();
  226. if (iro_opt('random_graphs_link_mobile',NULL) && iro_opt('random_graphs_link_mobile') != home_url( '/')) {
  227. $img_domain = iro_opt('random_graphs_link_mobile');
  228. } else {
  229. $img_domain = get_template_directory_uri();
  230. }
  231. $format = strpos($_SERVER['HTTP_ACCEPT'], 'image/webp') ? 'webp' : 'jpeg';
  232. $imgurl = "{$img_domain}/manifest/{$format}/{$array_keys[$img]}.source.{$format}";
  233. }
  234. return ['status'=>True,'url'=>$imgurl];
  235. }
  236. public static function feature_gallery($size = 'source') {
  237. if (iro_opt('post_cover_options') == 'type_2') {
  238. return ['status'=>True,'url'=>iro_opt('post_cover')];
  239. } else {
  240. $imgurl = self::cover_gallery($size);
  241. }
  242. return $imgurl;
  243. }
  244. }