SlideShare una empresa de Scribd logo
1 de 3
Retrieve a Random Image URL From Google With PHP
Ron Poulton
     A recent project involved me obtaining a random image from the Web and using it in
P: 1 some specific fashion. I chose to use PHP to access Google Images for a random
     image URL. The image could be generic or related to a specific topic.

   Here's the PHP function that queries Google Images and returns an image URL:
   Expand|Select|Wrap|Line Numbers

            1. function GetRandomImageURL($topic='', $min=0, $max=100)
            2. {
            3.    // get random image from Google
            4.    if ($topic=='') $topic='image';
            5.    $ofs=mt_rand($min, $max);
            6.    $geturl='http://www.google.ca/images?q=' . $topic . '&star
                t=' . $ofs . '&gbv=1';
            7.    $data=file_get_contents($geturl);
            8.
            9.    $f1='<div id="center_col">';
            10. $f2='<a href="/imgres?imgurl=';
            11. $f3='&amp;imgrefurl=';
            12.
            13. $pos1=strpos($data, $f1)+strlen($f1);
            14. if ($pos1==FALSE) return FALSE;
            15. $pos2=strpos($data, $f2, $pos1)+strlen($f2);
            16. if ($pos2==FALSE) return FALSE;
            17. $pos3=strpos($data, $f3, $pos2);
            18. if ($pos3==FALSE) return FALSE;
            19. return substr($data, $pos2, $pos3-$pos2);
            20.}
            21.


   This function generates a random number, sends that off as a "start" parameter with
   an image query to Google, and grabs the HTML code generated. The "gbv=1"
   parameter is required - without it, Google will return a generic page instead of the
   image results.

   If any markers ($f1, $f2 or $f3) are not found, the function will return FALSE.
   Otherwise, the first image (from the "start" parameter) is parsed, and the
   corresponding URL is retrieved.

   Note that Google may alter its HTML along the way, making this function obsolete.
   Also, large "start" values seem to exceed the number of images Google is willing to
   display. At the time of this writing, the maximum was about 980.

   The $topic parameter allows the option of specifying a certain type of image. Pass
   'vehicle' and an image of a vehicle is the target; pass 'balloon', and.. well, you get the
Retrieve a random image url from google with ph1

Más contenido relacionado

Destacado (7)

2012-09-06 Cloud Intacct
2012-09-06 Cloud Intacct 2012-09-06 Cloud Intacct
2012-09-06 Cloud Intacct
 
Change Leadership
Change LeadershipChange Leadership
Change Leadership
 
Law sequence 1 - Lesson Aims
Law sequence 1 - Lesson AimsLaw sequence 1 - Lesson Aims
Law sequence 1 - Lesson Aims
 
Excellence In Leadership Magazine Media Pack 2012
Excellence In Leadership Magazine Media Pack 2012Excellence In Leadership Magazine Media Pack 2012
Excellence In Leadership Magazine Media Pack 2012
 
Leadership & Change Magazine
Leadership & Change MagazineLeadership & Change Magazine
Leadership & Change Magazine
 
Magazine ideas presentation
Magazine ideas presentationMagazine ideas presentation
Magazine ideas presentation
 
Delhi public school magazine
Delhi public school magazineDelhi public school magazine
Delhi public school magazine
 

Similar a Retrieve a random image url from google with ph1

Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
Seri Moth
 
Webgl para JavaScripters
Webgl para JavaScriptersWebgl para JavaScripters
Webgl para JavaScripters
gerbille
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
Stoyan Stefanov
 
Five things for you - Yahoo developer offers
Five things for you - Yahoo developer offersFive things for you - Yahoo developer offers
Five things for you - Yahoo developer offers
Christian Heilmann
 

Similar a Retrieve a random image url from google with ph1 (20)

Simple Photo Processing and Web Display with Perl
Simple Photo Processing and Web Display with PerlSimple Photo Processing and Web Display with Perl
Simple Photo Processing and Web Display with Perl
 
Building Robust jQuery Plugins
Building Robust jQuery PluginsBuilding Robust jQuery Plugins
Building Robust jQuery Plugins
 
Lithium Best
Lithium Best Lithium Best
Lithium Best
 
AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS Directives
 
Photostream
PhotostreamPhotostream
Photostream
 
Photostream
PhotostreamPhotostream
Photostream
 
Drupal Development (Part 2)
Drupal Development (Part 2)Drupal Development (Part 2)
Drupal Development (Part 2)
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
 
Webgl para JavaScripters
Webgl para JavaScriptersWebgl para JavaScripters
Webgl para JavaScripters
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 
Images and PWA in magento
Images and PWA in magentoImages and PWA in magento
Images and PWA in magento
 
Functional Structures in PHP
Functional Structures in PHPFunctional Structures in PHP
Functional Structures in PHP
 
Let's write secure Drupal code! Drupal MountainCamp 2019
Let's write secure Drupal code! Drupal MountainCamp 2019Let's write secure Drupal code! Drupal MountainCamp 2019
Let's write secure Drupal code! Drupal MountainCamp 2019
 
Python tools to deploy your machine learning models faster
Python tools to deploy your machine learning models fasterPython tools to deploy your machine learning models faster
Python tools to deploy your machine learning models faster
 
Undercover Pods / WP Functions
Undercover Pods / WP FunctionsUndercover Pods / WP Functions
Undercover Pods / WP Functions
 
Five things for you - Yahoo developer offers
Five things for you - Yahoo developer offersFive things for you - Yahoo developer offers
Five things for you - Yahoo developer offers
 
New in php 7
New in php 7New in php 7
New in php 7
 
Web Optimization Summit: Coding for Performance
Web Optimization Summit: Coding for PerformanceWeb Optimization Summit: Coding for Performance
Web Optimization Summit: Coding for Performance
 
第3回Grails/Groovy勉強会名古屋「Grails名古屋座談会」
第3回Grails/Groovy勉強会名古屋「Grails名古屋座談会」第3回Grails/Groovy勉強会名古屋「Grails名古屋座談会」
第3回Grails/Groovy勉強会名古屋「Grails名古屋座談会」
 
JavaScript Inheritance
JavaScript InheritanceJavaScript Inheritance
JavaScript Inheritance
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

Retrieve a random image url from google with ph1

  • 1. Retrieve a Random Image URL From Google With PHP
  • 2. Ron Poulton A recent project involved me obtaining a random image from the Web and using it in P: 1 some specific fashion. I chose to use PHP to access Google Images for a random image URL. The image could be generic or related to a specific topic. Here's the PHP function that queries Google Images and returns an image URL: Expand|Select|Wrap|Line Numbers 1. function GetRandomImageURL($topic='', $min=0, $max=100) 2. { 3. // get random image from Google 4. if ($topic=='') $topic='image'; 5. $ofs=mt_rand($min, $max); 6. $geturl='http://www.google.ca/images?q=' . $topic . '&star t=' . $ofs . '&gbv=1'; 7. $data=file_get_contents($geturl); 8. 9. $f1='<div id="center_col">'; 10. $f2='<a href="/imgres?imgurl='; 11. $f3='&amp;imgrefurl='; 12. 13. $pos1=strpos($data, $f1)+strlen($f1); 14. if ($pos1==FALSE) return FALSE; 15. $pos2=strpos($data, $f2, $pos1)+strlen($f2); 16. if ($pos2==FALSE) return FALSE; 17. $pos3=strpos($data, $f3, $pos2); 18. if ($pos3==FALSE) return FALSE; 19. return substr($data, $pos2, $pos3-$pos2); 20.} 21. This function generates a random number, sends that off as a "start" parameter with an image query to Google, and grabs the HTML code generated. The "gbv=1" parameter is required - without it, Google will return a generic page instead of the image results. If any markers ($f1, $f2 or $f3) are not found, the function will return FALSE. Otherwise, the first image (from the "start" parameter) is parsed, and the corresponding URL is retrieved. Note that Google may alter its HTML along the way, making this function obsolete. Also, large "start" values seem to exceed the number of images Google is willing to display. At the time of this writing, the maximum was about 980. The $topic parameter allows the option of specifying a certain type of image. Pass 'vehicle' and an image of a vehicle is the target; pass 'balloon', and.. well, you get the