1
0

proxy.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. include "data/config.php";
  3. include "lib/curlproxy.php";
  4. $proxy = new proxy();
  5. if(!isset($_GET["i"])){
  6. header("X-Error: No URL(i) provided!");
  7. $proxy->do404();
  8. die();
  9. }
  10. try{
  11. // original size request, stream file to browser
  12. if(
  13. !isset($_GET["s"]) ||
  14. $_GET["s"] == "original"
  15. ){
  16. $proxy->stream_linear_image($_GET["i"]);
  17. die();
  18. }
  19. // bing request, ask bing to resize and stream to browser
  20. $image = parse_url($_GET["i"]);
  21. if(
  22. isset($image["host"]) &&
  23. preg_match(
  24. '/^[A-z0-9.]*bing\.(net|com)$/i',
  25. $image["host"]
  26. )
  27. ){
  28. if(!isset($image["path"])){
  29. header("X-Error: Missing bing image path");
  30. $proxy->do404();
  31. die();
  32. }
  33. //
  34. // get image ID
  35. // formations:
  36. // https://tse2.mm.bing.net/th/id/OIP.3yLBkUPn8EXA1wlhWP2BHwHaE3
  37. // https://tse2.mm.bing.net/th?id=OIP.3yLBkUPn8EXA1wlhWP2BHwHaE3
  38. //
  39. $id = null;
  40. if(isset($image["query"])){
  41. parse_str($image["query"], $str);
  42. if(isset($str["id"])){
  43. $id = $str["id"];
  44. }
  45. }
  46. if($id === null){
  47. $id = explode("/th/id/", $image["path"], 2);
  48. if(count($id) !== 2){
  49. // malformed
  50. return $url;
  51. }
  52. $id = $id[1];
  53. }
  54. if(is_array($id)){
  55. header("X-Error: Missing bing id parameter");
  56. $proxy->do404();
  57. die();
  58. }
  59. switch($_GET["s"]){
  60. case "portrait": $req = "?w=50&h=90&p=0&qlt=90"; break;
  61. case "landscape": $req = "?w=160&h=90&p=0&qlt=90"; break;
  62. case "square": $req = "?w=90&h=90&p=0&qlt=90"; break;
  63. case "thumb": $req = "?w=236&h=180&p=0&qlt=90"; break;
  64. case "cover": $req = "?w=207&h=270&p=0&qlt=90"; break;
  65. }
  66. $proxy->stream_linear_image("https://" . $image["host"] . "/th/id/" . urlencode($id) . $req, "https://www.bing.com");
  67. die();
  68. }
  69. // resize image ourselves
  70. $payload = $proxy->get($_GET["i"], $proxy::req_image, true);
  71. // get image format & set imagick
  72. $image = null;
  73. $format = $proxy->getimageformat($payload, $image);
  74. try{
  75. if($format !== false){
  76. $image->setFormat($format);
  77. }
  78. $image->readImageBlob($payload["body"]);
  79. $image_width = $image->getImageWidth();
  80. $image_height = $image->getImageHeight();
  81. switch($_GET["s"]){
  82. case "portrait":
  83. $width = 50;
  84. $height = 90;
  85. break;
  86. case "landscape":
  87. $width = 160;
  88. $height = 90;
  89. break;
  90. case "square":
  91. $width = 90;
  92. $height = 90;
  93. break;
  94. case "thumb":
  95. $width = 236;
  96. $height = 180;
  97. break;
  98. case "cover":
  99. $width = 207;
  100. $height = 270;
  101. break;
  102. }
  103. $ratio = $image_width / $image_height;
  104. if($image_width > $width){
  105. $image_width = $width;
  106. $image_height = round($image_width / $ratio);
  107. }
  108. if($image_height > $height){
  109. $ratio = $image_width / $image_height;
  110. $image_height = $height;
  111. $image_width = $image_height * $ratio;
  112. }
  113. $image->setImageBackgroundColor("#504945");
  114. $image->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);
  115. $image->stripImage();
  116. $image->setFormat("jpeg");
  117. $image->setImageCompressionQuality(90);
  118. $image->setImageCompression(Imagick::COMPRESSION_JPEG2000);
  119. $image->resizeImage($image_width, $image_height, Imagick::FILTER_LANCZOS, 1);
  120. $proxy->getfilenameheader($payload["headers"], $_GET["i"]);
  121. header("Content-Type: image/jpeg");
  122. echo $image->getImageBlob();
  123. }catch(ImagickException $error){
  124. header("X-Error: Could not convert the image: (" . $error->getMessage() . ")");
  125. $proxy->do404();
  126. }
  127. }catch(Exception $error){
  128. header("X-Error: " . $error->getMessage());
  129. $proxy->do404();
  130. die();
  131. }