pexels.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. class pexels{
  3. public function __construct(){
  4. include "lib/backend.php";
  5. $this->backend = new backend("pexels");
  6. }
  7. public function getfilters($page){
  8. return [
  9. "time" => [ // date_from
  10. "display" => "Time posted",
  11. "option" => [
  12. "any" => "Any time",
  13. "last_24_hours" => "Last 24 hours",
  14. "last_week" => "Last week",
  15. "last_month" => "Last month",
  16. "last_year" => "Last year"
  17. ]
  18. ],
  19. "orientation" => [
  20. "display" => "Orientation",
  21. "option" => [
  22. "any" => "Any orientation",
  23. "landscape" => "Horizontal",
  24. "portrait" => "Vertical",
  25. "square" => "Square"
  26. ]
  27. ],
  28. "color" => [
  29. "display" => "Color",
  30. "option" => [
  31. "any" => "Any color",
  32. "795548" => "Brown",
  33. "F44336" => "Red",
  34. "E91E63" => "Hot pink",
  35. "9C27B0" => "Magenta",
  36. "673AB7" => "Purple",
  37. "3F51B5" => "Indigo",
  38. "2196F3" => "Blue",
  39. "03A9F4" => "Light blue",
  40. "00BCD4" => "Cyan",
  41. "009688" => "Forest green",
  42. "4CAF50" => "Green",
  43. "8BC34A" => "Lime",
  44. "CDDC39" => "Pear",
  45. "FFEB3B" => "Yellow",
  46. "FFC107" => "Gold",
  47. "FF9800" => "Orange",
  48. "FF5722" => "Tomato",
  49. "9E9E9E" => "Gray",
  50. "607D8B" => "Teal",
  51. "000000" => "Black",
  52. "FFFFFF" => "White"
  53. ]
  54. ],
  55. "people_count" => [
  56. "display" => "Head count",
  57. "option" => [
  58. "any" => "Any number",
  59. "0" => "0",
  60. "1" => "1",
  61. "2" => "2",
  62. "3_plus" => "3+",
  63. ]
  64. ],
  65. "people_age" => [
  66. "display" => "People's age",
  67. "option" => [
  68. "any" => "Any age",
  69. "baby" => "Baby",
  70. "child" => "Child",
  71. "teenager" => "Teenager",
  72. "adult" => "Adult",
  73. "senior_adult" => "Senior adult"
  74. ]
  75. ]
  76. ];
  77. }
  78. private function get($proxy, $url, $get = []){
  79. $curlproc = curl_init();
  80. $search = $get["query"];
  81. if($get !== []){
  82. $get = http_build_query($get);
  83. $url .= "?" . $get;
  84. }
  85. curl_setopt($curlproc, CURLOPT_URL, $url);
  86. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  87. curl_setopt($curlproc, CURLOPT_HTTPHEADER,
  88. ["User-Agent: " . config::USER_AGENT,
  89. "Accept: */*",
  90. "Accept-Language: en-US,en;q=0.9",
  91. "Accept-Encoding: gzip, deflate, br, zstd",
  92. "Referer: https://www.pexels.com/search/" . rawurlencode($search) . "/",
  93. "Content-Type: application/json",
  94. "secret-key: H2jk9uKnhRmL6WPwh89zBezWvr", // hardcoded but like, people on github have been using this shit since 23'
  95. "X-Client-Type: react",
  96. "X-Next-Forwarded-CF-Connecting-IP: ",
  97. "X-Next-Forwarded-CF-IPCountry: ",
  98. "X-Next-Forwarded-CF-IPRegionCode: ",
  99. "X-Active-Experiment: ",
  100. "DNT: 1",
  101. "Sec-GPC: 1",
  102. "Alt-Used: www.pexels.com",
  103. "Connection: keep-alive",
  104. "Sec-Fetch-Dest: empty",
  105. "Sec-Fetch-Mode: cors",
  106. "Sec-Fetch-Site: same-origin",
  107. "Priority: u=4",
  108. "TE: trailers"]
  109. );
  110. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  111. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  112. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  113. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  114. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  115. $this->backend->assign_proxy($curlproc, $proxy);
  116. $data = curl_exec($curlproc);
  117. if(curl_errno($curlproc)){
  118. throw new Exception(curl_error($curlproc));
  119. }
  120. curl_close($curlproc);
  121. return $data;
  122. }
  123. public function image($get){
  124. if($get["npt"]){
  125. [$filter, $proxy] =
  126. $this->backend->get(
  127. $get["npt"],
  128. "images"
  129. );
  130. $filter = json_decode($filter, true);
  131. }else{
  132. $search = $get["s"];
  133. if(strlen($search) === 0){
  134. throw new Exception("Search term is empty!");
  135. }
  136. $proxy = $this->backend->get_ip();
  137. // ?query=blue%20footed%20booby&page=1&per_page=24&seo_tags=true
  138. $filter = [
  139. "query" => $search,
  140. "page" => 1,
  141. "per_page" => 24,
  142. "seo_tags" => "true"
  143. ];
  144. // add filters
  145. if($get["time"] != "any"){
  146. $filter["date_from"] = $get["time"];
  147. }
  148. if($get["orientation"] != "any"){
  149. $filter["orientation"] = $get["orientation"];
  150. }
  151. if($get["color"] != "any"){
  152. $filter["color"] = $get["color"];
  153. }
  154. if($get["people_count"] != "any"){
  155. $filter["people_count"] = $get["people_count"];
  156. }
  157. if($get["people_age"] != "any"){
  158. $filter["people_age"] = $get["people_age"];
  159. }
  160. }
  161. $out = [
  162. "status" => "ok",
  163. "npt" => null,
  164. "image" => []
  165. ];
  166. try{
  167. $html =
  168. $this->get(
  169. $proxy,
  170. "https://www.pexels.com/en-us/api/v3/search/photos",
  171. $filter
  172. );
  173. }catch(Exception $error){
  174. throw new Exception("Failed to fetch JSON");
  175. }
  176. $json = json_decode($html, true);
  177. if(!isset($json["data"])){
  178. throw new Exception("Pexels did not return a data object");
  179. }
  180. foreach($json["data"] as $image){
  181. $thumb_size =
  182. $this->imgratio(
  183. (int)$image["attributes"]["width"],
  184. (int)$image["attributes"]["height"],
  185. 350
  186. );
  187. $out["image"][] = [
  188. "title" => $image["attributes"]["title"] . ": " . $image["attributes"]["description"],
  189. "source" => [
  190. [
  191. "url" => $image["attributes"]["image"]["download_link"],
  192. "width" => (int)$image["attributes"]["width"],
  193. "height" => (int)$image["attributes"]["height"]
  194. ],
  195. [
  196. "url" =>
  197. preg_replace(
  198. '/(?:w|h)=[0-9]+$/',
  199. "w=350",
  200. $image["attributes"]["image"]["small"]
  201. ),
  202. "width" => $thumb_size[0],
  203. "height" => $thumb_size[1]
  204. ]
  205. ],
  206. "url" =>
  207. "https://pexels.com/photo/" .
  208. $image["attributes"]["slug"] . "-" .
  209. $image["attributes"]["id"] . "/"
  210. ];
  211. }
  212. // get next page
  213. if((int)$json["pagination"]["current_page"] < (int)$json["pagination"]["total_pages"]){
  214. $filter["page"]++;
  215. $out["npt"] =
  216. $this->backend->store(
  217. json_encode($filter),
  218. "images",
  219. $proxy
  220. );
  221. }
  222. return $out;
  223. }
  224. private function imgratio($width, $height, $max_width){
  225. $ratio = $max_width / $width;
  226. $new_height = floor($height * $ratio);
  227. return [
  228. $max_width,
  229. $new_height
  230. ];
  231. }
  232. }