1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- function get_image_results($query)
- {
- require "config.php";
- require "misc/tools.php";
- $url = "https://www.google.$config_google_domain/search?&q=$query&hl=$config_google_language&tbm=isch";
- $response = request($url);
- $xpath = get_xpath($response);
- $mh = curl_multi_init();
- $chs = $alts = $results = array();
- foreach($xpath->query("//img[@data-src]") as $image)
- {
- $alt = $image->getAttribute("alt");
- $src = $image->getAttribute("data-src");
- if (!empty($alt))
- {
- $ch = curl_init($src);
- curl_setopt_array($ch, $config_curl_settings);
- array_push($chs, $ch);
- curl_multi_add_handle($mh, $ch);
- array_push($alts, $alt);
- }
- }
- $running = null;
- do {
- curl_multi_exec($mh, $running);
- } while ($running);
- for ($i=0; count($chs)>$i; $i++)
- {
- $img_base64 = base64_encode(curl_multi_getcontent($chs[$i]));
- array_push($results,
- array (
- "base64" => $img_base64,
- "alt" => htmlspecialchars($alts[$i])
- )
- );
- }
- return $results;
- }
- function print_image_results($results)
- {
- echo "<div class=\"image-result-container\">";
- foreach($results as $result)
- {
- $src = $result["base64"];
- $alt = $result["alt"];
- echo "<a title=\"$alt\" href=\"data:image/jpeg;base64,$src\" target=\"_blank\">";
- echo "<img src=\"data:image/jpeg;base64,$src\" width=\"350\" height=\"200\">";
- echo "</a>";
- }
- echo "</div>";
- }
- ?>
|