proxy.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. include "lib/curlproxy.php";
  3. $proxy = new proxy();
  4. if(!isset($_GET["i"])){
  5. header("X-Error: No URL(i) provided!");
  6. $proxy->do404();
  7. die();
  8. }
  9. try{
  10. // original size request, stream file to browser
  11. if(
  12. !isset($_GET["s"]) ||
  13. $_GET["s"] == "original"
  14. ){
  15. $proxy->stream_linear_image($_GET["i"]);
  16. die();
  17. }
  18. // bing request, ask bing to resize and stream to browser
  19. if(
  20. preg_match(
  21. '/bing.net$/',
  22. parse_url($_GET["i"], PHP_URL_HOST)
  23. )
  24. ){
  25. switch($_GET["s"]){
  26. case "portrait": $req = "&w=50&h=90&p=0&qlt=90"; break;
  27. case "landscape": $req = "&w=160&h=90&p=0&qlt=90"; break;
  28. case "square": $req = "&w=90&h=90&p=0&qlt=90"; break;
  29. case "thumb": $req = "&w=236&h=180&p=0&qlt=90"; break;
  30. case "cover": $req = "&w=207&h=270&p=0&qlt=90"; break;
  31. }
  32. $proxy->stream_linear_image($_GET["i"] . $req, "https://bing.net");
  33. die();
  34. }
  35. // resize image ourselves
  36. $payload = $proxy->get($_GET["i"], $proxy::req_image, true);
  37. // get image format & set imagick
  38. $image = null;
  39. $format = $proxy->getimageformat($payload, $image);
  40. try{
  41. if($format !== false){
  42. $image->setFormat($format);
  43. }
  44. $image->readImageBlob($payload["body"]);
  45. $image_width = $image->getImageWidth();
  46. $image_height = $image->getImageHeight();
  47. switch($_GET["s"]){
  48. case "portrait":
  49. $width = 50;
  50. $height = 90;
  51. break;
  52. case "landscape":
  53. $width = 160;
  54. $height = 90;
  55. break;
  56. case "square":
  57. $width = 90;
  58. $height = 90;
  59. break;
  60. case "thumb":
  61. $width = 236;
  62. $height = 180;
  63. break;
  64. case "cover":
  65. $width = 207;
  66. $height = 270;
  67. break;
  68. }
  69. $ratio = $image_width / $image_height;
  70. if($image_width > $width){
  71. $image_width = $width;
  72. $image_height = round($image_width / $ratio);
  73. }
  74. if($image_height > $height){
  75. $ratio = $image_width / $image_height;
  76. $image_height = $height;
  77. $image_width = $image_height * $ratio;
  78. }
  79. $image->setImageBackgroundColor("#504945");
  80. $image->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);
  81. $image->stripImage();
  82. $image->setFormat("jpeg");
  83. $image->setImageCompressionQuality(90);
  84. $image->setImageCompression(Imagick::COMPRESSION_JPEG2000);
  85. $image->resizeImage($image_width, $image_height, Imagick::FILTER_LANCZOS, 1);
  86. $proxy->getfilenameheader($payload["headers"], $_GET["i"]);
  87. header("Content-Type: image/jpeg");
  88. echo $image->getImageBlob();
  89. }catch(ImagickException $error){
  90. header("X-Error: Could not convert the image: (" . $error->getMessage() . ")");
  91. $proxy->do404();
  92. }
  93. }catch(Exception $error){
  94. header("X-Error: " . $error->getMessage());
  95. $proxy->do404();
  96. die();
  97. }