1
0

proxy.php 2.7 KB

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