proxy.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. // fallback to getting ID from path
  48. $id = explode("/", $image["path"]);
  49. for($i=count($id) - 1; $i>0; $i--){
  50. if(trim($id[$i]) != ""){
  51. $id = $id[$i];
  52. break;
  53. }
  54. }
  55. }
  56. if(is_array($id)){
  57. header("X-Error: Missing bing id parameter");
  58. $proxy->do404();
  59. die();
  60. }
  61. switch($_GET["s"]){
  62. case "portrait": $req = "?w=50&h=90&p=0&qlt=90"; break;
  63. case "landscape": $req = "?w=160&h=90&p=0&qlt=90"; break;
  64. case "square": $req = "?w=90&h=90&p=0&qlt=90"; break;
  65. case "thumb": $req = "?w=236&h=180&p=0&qlt=90"; break;
  66. case "cover": $req = "?w=207&h=270&p=0&qlt=90"; break;
  67. }
  68. $proxy->stream_linear_image("https://" . $image["host"] . "/th/id/" . urlencode($id) . $req, "https://www.bing.com");
  69. die();
  70. }
  71. // resize image ourselves
  72. $payload = $proxy->get($_GET["i"], $proxy::req_image, true);
  73. // get image format & set imagick
  74. $image = null;
  75. $format = $proxy->getimageformat($payload, $image);
  76. try{
  77. if($format !== false){
  78. $image->setFormat($format);
  79. }
  80. $image->readImageBlob($payload["body"]);
  81. $image_width = $image->getImageWidth();
  82. $image_height = $image->getImageHeight();
  83. switch($_GET["s"]){
  84. case "portrait":
  85. $width = 50;
  86. $height = 90;
  87. break;
  88. case "landscape":
  89. $width = 160;
  90. $height = 90;
  91. break;
  92. case "square":
  93. $width = 90;
  94. $height = 90;
  95. break;
  96. case "thumb":
  97. $width = 236;
  98. $height = 180;
  99. break;
  100. case "cover":
  101. $width = 207;
  102. $height = 270;
  103. break;
  104. }
  105. $ratio = $image_width / $image_height;
  106. if($image_width > $width){
  107. $image_width = $width;
  108. $image_height = round($image_width / $ratio);
  109. }
  110. if($image_height > $height){
  111. $ratio = $image_width / $image_height;
  112. $image_height = $height;
  113. $image_width = $image_height * $ratio;
  114. }
  115. $image->setImageBackgroundColor("#504945");
  116. $image->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);
  117. $image->stripImage();
  118. $image->setFormat("jpeg");
  119. $image->setImageCompressionQuality(90);
  120. $image->setImageCompression(Imagick::COMPRESSION_JPEG2000);
  121. $image->resizeImage($image_width, $image_height, Imagick::FILTER_LANCZOS, 1);
  122. $proxy->getfilenameheader($payload["headers"], $_GET["i"]);
  123. header("Content-Type: image/jpeg");
  124. echo $image->getImageBlob();
  125. }catch(ImagickException $error){
  126. header("X-Error: Could not convert the image: (" . $error->getMessage() . ")");
  127. $proxy->do404();
  128. }
  129. }catch(Exception $error){
  130. header("X-Error: " . $error->getMessage());
  131. $proxy->do404();
  132. die();
  133. }