Beispiel #1
0
 /**
  * Get the images from a DOM.
  *
  * @param pQuery $dom The DOM to search.
  * @param string $url The URL of the document to add to relative URLs.
  * @param int $maxImages The maximum number of images to return.
  * @return array Returns an array in the form: `[['Src' => '', 'Width' => '', 'Height' => ''], ...]`.
  */
 function domGetImages($dom, $url, $maxImages = 4)
 {
     $Images = array();
     foreach ($dom->query('img') as $element) {
         $Images[] = array('Src' => absoluteSource($element->attr('src'), $url), 'Width' => $element->attr('width'), 'Height' => $element->attr('height'));
     }
     //      Gdn::Controller()->Data['AllImages'] = $Images;
     // Sort by size, biggest one first
     $ImageSort = array();
     // Only look at first 4 images (speed!)
     $i = 0;
     foreach ($Images as $ImageInfo) {
         $Image = $ImageInfo['Src'];
         if (strpos($Image, 'doubleclick.') != false) {
             continue;
         }
         try {
             if ($ImageInfo['Height'] && $ImageInfo['Width']) {
                 $Height = $ImageInfo['Height'];
                 $Width = $ImageInfo['Width'];
             } else {
                 list($Width, $Height) = getimagesize($Image);
             }
             $Diag = (int) floor(sqrt($Width * $Width + $Height * $Height));
             //            Gdn::Controller()->Data['Foo'][] = array($Image, $Width, $Height, $Diag);
             if (!$Width || !$Height) {
                 continue;
             }
             // Require min 100x100 dimension image.
             if ($Width < 100 && $Height < 100) {
                 continue;
             }
             // Don't take a banner-shaped image.
             if ($Height * 4 < $Width) {
                 continue;
             }
             // Prefer images that are less than 800px wide (banners?)
             //            if ($Diag > 141 && $Width < 800) { }
             if (!array_key_exists($Diag, $ImageSort)) {
                 $ImageSort[$Diag] = array($Image);
             } else {
                 $ImageSort[$Diag][] = $Image;
             }
             $i++;
             if ($i > $maxImages) {
                 break;
             }
         } catch (Exception $ex) {
             // do nothing
         }
     }
     krsort($ImageSort);
     $GoodImages = array();
     foreach ($ImageSort as $Diag => $Arr) {
         $GoodImages = array_merge($GoodImages, $Arr);
     }
     return $GoodImages;
 }