proxy.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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(
  29. !isset($image["query"]) ||
  30. !isset($image["path"]) ||
  31. $image["path"] != "/th"
  32. ){
  33. header("X-Error: Invalid bing image path");
  34. $proxy->do404();
  35. die();
  36. }
  37. parse_str($image["query"], $str);
  38. if(!isset($str["id"])){
  39. header("X-Error: Missing bing ID");
  40. $proxy->do404();
  41. die();
  42. }
  43. switch($_GET["s"]){
  44. case "portrait": $req = "&w=50&h=90&p=0&qlt=90"; break;
  45. case "landscape": $req = "&w=160&h=90&p=0&qlt=90"; break;
  46. case "square": $req = "&w=90&h=90&p=0&qlt=90"; break;
  47. case "thumb": $req = "&w=236&h=180&p=0&qlt=90"; break;
  48. case "cover": $req = "&w=207&h=270&p=0&qlt=90"; break;
  49. }
  50. $proxy->stream_linear_image("https://" . $image["host"] . "/th?id=" . urlencode($str["id"]) . $req, "https://www.bing.com");
  51. die();
  52. }
  53. // resize image ourselves
  54. $payload = $proxy->get($_GET["i"], $proxy::req_image, true);
  55. // get image format & set imagick
  56. $image = null;
  57. $format = $proxy->getimageformat($payload, $image);
  58. try{
  59. if($format !== false){
  60. $image->setFormat($format);
  61. }
  62. $image->readImageBlob($payload["body"]);
  63. $image_width = $image->getImageWidth();
  64. $image_height = $image->getImageHeight();
  65. switch($_GET["s"]){
  66. case "portrait":
  67. $width = 50;
  68. $height = 90;
  69. break;
  70. case "landscape":
  71. $width = 160;
  72. $height = 90;
  73. break;
  74. case "square":
  75. $width = 90;
  76. $height = 90;
  77. break;
  78. case "thumb":
  79. $width = 236;
  80. $height = 180;
  81. break;
  82. case "cover":
  83. $width = 207;
  84. $height = 270;
  85. break;
  86. }
  87. $ratio = $image_width / $image_height;
  88. if($image_width > $width){
  89. $image_width = $width;
  90. $image_height = round($image_width / $ratio);
  91. }
  92. if($image_height > $height){
  93. $ratio = $image_width / $image_height;
  94. $image_height = $height;
  95. $image_width = $image_height * $ratio;
  96. }
  97. $image->setImageBackgroundColor("#504945");
  98. $image->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);
  99. $image->stripImage();
  100. $image->setFormat("jpeg");
  101. $image->setImageCompressionQuality(90);
  102. $image->setImageCompression(Imagick::COMPRESSION_JPEG2000);
  103. $image->resizeImage($image_width, $image_height, Imagick::FILTER_LANCZOS, 1);
  104. $proxy->getfilenameheader($payload["headers"], $_GET["i"]);
  105. header("Content-Type: image/jpeg");
  106. echo $image->getImageBlob();
  107. }catch(ImagickException $error){
  108. header("X-Error: Could not convert the image: (" . $error->getMessage() . ")");
  109. $proxy->do404();
  110. }
  111. }catch(Exception $error){
  112. header("X-Error: " . $error->getMessage());
  113. $proxy->do404();
  114. die();
  115. }