unsplash.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. class unsplash{
  3. public function __construct(){
  4. include "lib/fuckhtml.php";
  5. $this->fuckhtml = new fuckhtml();
  6. include "lib/backend.php";
  7. $this->backend = new backend("unsplash");
  8. }
  9. public function getfilters($page){
  10. return [
  11. "order_by" => [
  12. "display" => "Order by",
  13. "option" => [
  14. "relevance" => "Relevance",
  15. "latest" => "Newest",
  16. "editorial" => "Curated"
  17. ]
  18. ],
  19. "orientation" => [
  20. "display" => "Order by",
  21. "option" => [
  22. "any" => "Any orientation",
  23. "landscape" => "Landscape",
  24. "portrait" => "Portrait",
  25. "squarish" => "Square"
  26. ]
  27. ],
  28. "license" => [
  29. "display" => "License",
  30. "option" => [
  31. "any" => "Any license",
  32. "only" => "Unsplash+",
  33. "none" => "Free"
  34. ]
  35. ]
  36. ];
  37. }
  38. private function get($proxy, $url, $get = [], $referer){
  39. $curlproc = curl_init();
  40. if($get !== []){
  41. $get = http_build_query($get);
  42. $url .= "?" . $get;
  43. }
  44. curl_setopt($curlproc, CURLOPT_URL, $url);
  45. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  46. curl_setopt($curlproc, CURLOPT_HTTPHEADER,
  47. ["User-Agent: " . config::USER_AGENT,
  48. "Accept: */*",
  49. "Accept-Language: en-US",
  50. "Accept-Encoding: gzip, deflate, br, zstd",
  51. "Referer: {$referer}",
  52. "client-geo-region: global",
  53. "x-client-version: 8999df28be3f138bf2c646df5d656e4dc6970ba0",
  54. "DNT: 1",
  55. "Sec-GPC: 1",
  56. "Connection: keep-alive",
  57. "Sec-Fetch-Dest: empty",
  58. "Sec-Fetch-Mode: cors",
  59. "Sec-Fetch-Site: same-origin",
  60. "Priority: u=0",
  61. "TE: trailers"]
  62. );
  63. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  64. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  65. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  66. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  67. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  68. $this->backend->assign_proxy($curlproc, $proxy);
  69. $data = curl_exec($curlproc);
  70. if(curl_errno($curlproc)){
  71. throw new Exception(curl_error($curlproc));
  72. }
  73. curl_close($curlproc);
  74. return $data;
  75. }
  76. public function image($get){
  77. if($get["npt"]){
  78. [$filter, $proxy] =
  79. $this->backend->get(
  80. $get["npt"],
  81. "images"
  82. );
  83. $filter = json_decode($filter, true);
  84. }else{
  85. $search = $get["s"];
  86. if(strlen($search) === 0){
  87. throw new Exception("Search term is empty!");
  88. }
  89. $proxy = $this->backend->get_ip();
  90. $filter = [
  91. "page" => 1,
  92. "per_page" => 20,
  93. "query" => $search
  94. ];
  95. // add filters
  96. if($get["order_by"] != "relevance"){
  97. $filter["order_by"] = $get["order_by"];
  98. }
  99. if($get["orientation"] != "any"){
  100. $filter["orientation"] = $get["orientation"];
  101. }
  102. if($get["license"] != "any"){
  103. $filter["plus"] = $get["license"];
  104. }
  105. }
  106. $out = [
  107. "status" => "ok",
  108. "npt" => null,
  109. "image" => []
  110. ];
  111. // https://unsplash.com/s/photos/shibuya-night?order_by=latest&orientation=landscape&license=free
  112. // https://unsplash.com/s/photos/%3Ctest-%3F!-haha == <test ?! haha
  113. // ?page=1&per_page=20&query=japan
  114. try{
  115. $json =
  116. $this->get(
  117. $proxy,
  118. "https://unsplash.com/napi/search/photos",
  119. $filter,
  120. "https://unsplash.com/s/photos/" . rawurlencode(str_replace(" ", "-", $filter["query"])),
  121. );
  122. }catch(Exception $error){
  123. throw new Exception("Failed to fetch JSON");
  124. }
  125. $json = json_decode($json, true);
  126. if($json === null){
  127. throw new Exception("Could not decode the JSON payload");
  128. }
  129. foreach($json["results"] as $image){
  130. $base = explode("?", $image["urls"]["raw"])[0];
  131. if(
  132. (bool)$image["premium"] ||
  133. (bool)$image["plus"]
  134. ){
  135. // when we get "premium" images, give
  136. // 1. highest resolution with watermarks
  137. // 2. highest resolution without watermarks
  138. // (if width of image is above 900px, it has watermarks)
  139. // 3. thumbnail without watermark
  140. $x900 = $this->imgratio((int)$image["width"], (int)$image["height"], 900);
  141. $x500 = $this->imgratio((int)$image["width"], (int)$image["height"], 500);
  142. $source = [
  143. [
  144. "url" => $base,
  145. "width" => (int)$image["width"],
  146. "height" => (int)$image["height"]
  147. ],
  148. [
  149. "url" => $base . "?w=900",
  150. "width" => $x900[0],
  151. "height" => $x900[1]
  152. ],
  153. [
  154. "url" => $base . "?w=500",
  155. "width" => $x500[0],
  156. "height" => $x500[1]
  157. ]
  158. ];
  159. }else{
  160. $x500 = $this->imgratio((int)$image["width"], (int)$image["height"], 500);
  161. // image is free as in freedom(tm)
  162. $source = [
  163. [
  164. "url" => $base,
  165. "width" => (int)$image["width"],
  166. "height" => (int)$image["height"]
  167. ],
  168. [
  169. "url" => $base . "?w=500",
  170. "width" => $x500[0],
  171. "height" => $x500[1]
  172. ]
  173. ];
  174. }
  175. $title = [];
  176. $image["description"] = trim($image["description"]);
  177. $image["alt_description"] = trim($image["alt_description"]);
  178. if(!empty($image["description"])){ $title[] = $image["description"]; }
  179. if(!empty($image["alt_description"])){ $title[] = $image["alt_description"]; }
  180. $title = implode(": ", $title);
  181. $out["image"][] = [
  182. "title" => $title,
  183. "source" => $source,
  184. "url" => "https://unsplash.com/photos/" . $image["slug"]
  185. ];
  186. }
  187. // next page stuff
  188. if($filter["page"] < (int)$json["total_pages"]){
  189. $filter["page"]++;
  190. $out["npt"] =
  191. $this->backend->store(
  192. json_encode($filter),
  193. "images",
  194. $proxy
  195. );
  196. }
  197. return $out;
  198. }
  199. private function imgratio($width, $height, $max_width){
  200. $ratio = $max_width / $width;
  201. $new_height = floor($height * $ratio);
  202. return [
  203. $max_width,
  204. $new_height
  205. ];
  206. }
  207. }