/dd-includes/service.gimages.php

https://github.com/DatumDroid/DatumDroid-API · PHP · 177 lines · 73 code · 31 blank · 73 comment · 10 complexity · 1ed4a8e2c8f1c4381dbdb7d093494661 MD5 · raw file

  1. <?php
  2. /**
  3. * Wrapper class around the Google Image API for PHP
  4. *
  5. * http://code.google.com/apis/imagesearch/v1/jsondevguide.html
  6. * http://w-shadow.com/blog/2008/02/28/get-google-image-search-results-with-php/
  7. *
  8. * @package DatumDroid_API
  9. * @subpackage Services
  10. */
  11. // Exit if accessed directly
  12. if ( !defined( 'DD_DIR' ) ) exit;
  13. class DD_Service_Google_Images extends DD_Search_Service {
  14. /**
  15. * Possible values:
  16. * * large - l
  17. * * medium - m
  18. * * icon - i
  19. * * any - '' (null) (default)
  20. *
  21. * @var string
  22. */
  23. var $image_size = '';
  24. /**
  25. * Possible values: face|photo|clipart|lineart|(null)
  26. *
  27. * For any, send null or '' (default)
  28. *
  29. * @var string
  30. */
  31. var $image_type = '';
  32. /**
  33. * The maximum number of results that this service can return
  34. * @var int
  35. */
  36. var $max = '21';
  37. /**
  38. * Construct the class
  39. *
  40. * Also unset the user agent set by the parent class
  41. *
  42. * @param mixed $args Request arguments
  43. */
  44. function DD_Service_Google_Images( $args = array() ) {
  45. parent::DD_Search_Service( $args );
  46. // Do not send any user agent with the cURL call, otherwise different types of results are returned which we can't match
  47. $this->user_agent = false;
  48. }
  49. /**
  50. * @param int $size required
  51. * @return object
  52. */
  53. function image_size( $size ) {
  54. $this->image_size = in_array( $size, array( 'l', 'm', 'i', '' ) ) ? $size : $this->image_size;
  55. return $this;
  56. }
  57. /**
  58. * @param int $type required
  59. * @return object
  60. */
  61. function image_type( $type ) {
  62. $this->image_type = in_array( $type, array( 'face', 'photo', 'clipart', 'lineart', '' ) ) ? $type : $this->image_type;
  63. return $this;
  64. }
  65. /**
  66. * Build and perform the query, return the results.
  67. * @param $reset_query boolean optional.
  68. * @return object
  69. */
  70. function results( $reset_query = true ) {
  71. $request = 'http://www.google.com/images?hl=' . $this->lang;
  72. $request .= '&q=' . urlencode( $this->query );
  73. if ( isset( $this->page ) ) {
  74. $request .= '&start=' . ( $this->page - 1 ) * min( $this->rpp, $this->max );
  75. }
  76. if ( isset( $this->image_type ) ) {
  77. $request .= '&imgtype=' . $this->image_type;
  78. }
  79. if ( isset( $this->image_size ) ) {
  80. $request .= '&imgsz=' . $this->image_size;
  81. }
  82. if ( isset( $this->geocode ) ) {
  83. $request .= '&geocode=' . $this->geocode;
  84. }
  85. if ( $reset_query ) {
  86. $this->query = '';
  87. }
  88. // Make the call and extract out results
  89. return $this->refine( $this->process( $request ) );
  90. }
  91. /**
  92. * Function to prepare data for return to client
  93. * @access private
  94. * @param string $data
  95. */
  96. function refine( $data = '' ) {
  97. $retVal = array();
  98. // Extract the image information. This is found inside of a javascript call to setResults
  99. preg_match( '/dyn.setResults\(\[(.*?)\]\);/is', $data, $match );
  100. if ( !isset( $match[1] ) )
  101. return $retVal;
  102. // Grab all the arrays
  103. preg_match_all( '/\[(.*?)\"\]/', $match[1], $m );
  104. foreach ( $m[1] as $item ) {
  105. // Explode on each paramter (comma delimeter)
  106. $item = urldecode( str_replace( '\x', '%', $item ) );
  107. preg_match_all( '/\"(.*?)\"/', $item, $params );
  108. // Check for more than one paramter. Not sure why, but there seem to be empty array sets between actual results
  109. if ( count( $params[1] ) > 0 ) {
  110. $params = $params[1];
  111. // Important array indices
  112. // 0 - Link to Google image result. This is the page that displays when you click a result through normal image search
  113. // 3 - URL of source image
  114. // 4 - Width of the thumbnail image
  115. // 5 - Height of the thumbnail image
  116. // 6 - Title of the image
  117. // 9 - Width, height and size of the image (In the format of 'width &times; height - size')
  118. // 10 - Image type
  119. // 11 - Originating domain
  120. // 18 - URL of google's thumbnail
  121. // Match correct values
  122. preg_match( '/([\d]+) &times; ([\d]+) - ([\d]+)/', $params[9], $dimensions );
  123. $query = dd_convert_url_query( 'http://www.google.com' . $params[0] );
  124. // Make the array
  125. $t = null;
  126. $t->imgurl = $query['imgurl'];
  127. $t->source = $query['imgrefurl'];
  128. $t->title = strip_tags( $params[6] );
  129. $t->width = $dimensions[1];
  130. $t->height = $dimensions[2];
  131. $t->size = $dimensions[3];
  132. $t->type = $params[10];
  133. $t->domain = $params[11];
  134. $t->thumb = null;
  135. $t->thumb->src = $params[18];
  136. $t->thumb->width = $params[4];
  137. $t->thumb->height = $params[5];
  138. $retVal[] = $t;
  139. }
  140. if ( count( $retVal ) == $this->rpp )
  141. break;
  142. }
  143. return $retVal;
  144. }
  145. }
  146. ?>