image.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. function get_image_results($query)
  3. {
  4. require "config.php";
  5. require "misc/tools.php";
  6. $url = "https://www.google.$config_google_domain/search?&q=$query&hl=$config_google_language&tbm=isch";
  7. $response = request($url);
  8. $xpath = get_xpath($response);
  9. $mh = curl_multi_init();
  10. $chs = $alts = $results = array();
  11. foreach($xpath->query("//img[@data-src]") as $image)
  12. {
  13. $alt = $image->getAttribute("alt");
  14. $src = $image->getAttribute("data-src");
  15. if (!empty($alt))
  16. {
  17. $ch = curl_init($src);
  18. curl_setopt_array($ch, $config_curl_settings);
  19. array_push($chs, $ch);
  20. curl_multi_add_handle($mh, $ch);
  21. array_push($alts, $alt);
  22. }
  23. }
  24. $running = null;
  25. do {
  26. curl_multi_exec($mh, $running);
  27. } while ($running);
  28. for ($i=0; count($chs)>$i; $i++)
  29. {
  30. $img_base64 = base64_encode(curl_multi_getcontent($chs[$i]));
  31. array_push($results,
  32. array (
  33. "base64" => $img_base64,
  34. "alt" => htmlspecialchars($alts[$i])
  35. )
  36. );
  37. }
  38. return $results;
  39. }
  40. function print_image_results($results)
  41. {
  42. echo "<div class=\"image-result-container\">";
  43. foreach($results as $result)
  44. {
  45. $src = $result["base64"];
  46. $alt = $result["alt"];
  47. echo "<a title=\"$alt\" href=\"data:image/jpeg;base64,$src\" target=\"_blank\">";
  48. echo "<img src=\"data:image/jpeg;base64,$src\" width=\"350\" height=\"200\">";
  49. echo "</a>";
  50. }
  51. echo "</div>";
  52. }
  53. ?>