image.php 1.8 KB

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