简体   繁体   English

如何自动在页面上显示第一个Google图片?

[英]How do I automatically display the first google image on my page?

I have a page that automatically retrieves two random entries from a MySQL database and asks the user to compare them every time the page is refreshed. 我有一个页面,该页面会自动从MySQL数据库中检索两个随机条目,并在每次刷新页面时要求用户对其进行比较。 How can I convert those two strings into the first google image result for them automatically? 如何将这两个字符串自动转换为第一个Google图片结果? This is what I have so far: 这是我到目前为止的内容:

<?php //I retrieve the names and group names here//
$firstpersonaName = (string) $row["personaName"];
$firstpersonaGroupName = (string) $row["groupName"];
    $firstpersonaGroupNameForGoogle = preg_split('( )', $firstpersonaGroupName);
?> //then convert any group names containing spaces into arrays here//

<?php  //then here I build the query that displays a google image page//
    $newname = '';
    foreach ($firstpersonaGroupNameForGoogle as $firstpersonaPartofGroupName) {
        $newname = $firstpersonaPartofGroupName . '+';
    }
    $newname = rtrim($newname, "+");
    echo "https://www.google.com/search?q=" . $firstpersonaName . "+" . $newname . '&tbm=isch'; 
?>

This gives me stuff like: https://www.google.com/search?q=charlie+always+sunny&tbm=isch 这给了我类似的东西: https : //www.google.com/search?q=charlie+always+sunny&tbm=isch

So how do I take that link and turn it into the link of the first image? 那么,如何获取该链接并将其转换为第一张图片的链接? Or any of the first couple really. 还是前几对夫妇中的任何一个。 (In this case: http://cdn3.denofgeek.us/sites/denofgeekus/files/sunny_0.jpg ) (在这种情况下: http : //cdn3.denofgeek.us/sites/denofgeekus/files/sunny_0.jpg

Okay so here's what I ended up doing to randomly generate two pics per query: 好的,这就是我最终要为每个查询随机生成两张图片的目的:

First I downloaded this and added it to the same directory as the webpage: http://simplehtmldom.sourceforge.net/ 首先,我下载了该文件并将其添加到与网页相同的目录中: http : //simplehtmldom.sourceforge.net/

Then I simply added this PHP code in the div where I wanted the picture to show up: 然后,我只是在想要显示图片的div中添加了此PHP代码:

<?php
    // Include the php dom parser    
    include_once 'simple_html_dom.php';
    //build the google images query
    $newname = '';
    foreach ($firstpersonaGroupNameForGoogle as $firstpersonaPartofGroupName) {
        $newname = $firstpersonaPartofGroupName . '+';
    }
    $newname = rtrim($newname, "+");
    //echo "https://www.google.com/search?q=" . $firstpersonaName . "+" . $newname . '&tbm=isch'; 
    $newname = "https://www.google.com/search?q=" . $firstpersonaName . "+" . $newname . '&tbm=isch';

    //use parser on queried page
    $html = file_get_html($newname);
    //echo $html;

    //create an array for all pics on page
    $picarray = array();
    $picurl = '';

    // Find all images 
    foreach($html->find('img') as $element) {
       //echo $element->src . '<br>';
       $picurl = $element->src;
       array_push($picarray,$picurl);
    }
    //then pick two random ones
    $picurl = $picarray[array_rand($picarray)];
    echo "<img src=" . $picurl . ">";
    $picurl = $picarray[array_rand($picarray)];
    echo "<img src=" . $picurl . ">";

?>

They're pretty small resolution (about 150px) but that actually works great with what I'm trying to do. 它们的分辨率很小(约150像素),但实际上与我想做的一样很好。 If you wanted to retrieve a non-thumbnail pic that's a whole different can of worms. 如果您想获取一个非缩略图图片,那是完全不同的蠕虫罐头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当页面使用JavaScript / jQuery加载时,如何获取选择框的第一个选项以自动显示在div中? - How do I get the first option of a select box to display in a div automatically when the page loads with JavaScript/jQuery? 如何替换页面上所有被阻止的Google广告(带有图片),而不是第一个? CSS选择器出现问题 - How do I replace all the blocked google ads (with an image) on my page instead of just the first one? Trouble with CSS selectors 如何显示每个带有.className的div的图像? - How do I display the first image from each div with .className? 如何在同一页面上显示图像输入结果? - How do I display image input results on same page? 如何在服务器上捕获图像并显示在html页面上? - How do I grab an image on a server and display on a html page? 如何在页面上多次显示我的js变量 - How do i display my js variable multiple times on a page 如何在我的页面上永久禁用 Google Analytics 跟踪? - How do I permanentely disable Google Analytics Tracking on my page? 如何刷新Google Maps标记但不刷新整个页面 - how do i refresh google maps marker but not my whole page 如何单击图像使其成为我的页面背景? - How do I click an image to make it my page background? 如何自动更新我的页面以添加输入的内容? 我收到 TypeError - How do I automatically update my page to add what is inputted? I am receiving TypeError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM