/**
  * @param $url
  * @param null|int $width
  * @param null|int $height
  * @param null|int $size
  * @return ExtractedImage
  */
 public static function createFromUrl($url, $width = null, $height = null, $size = null, $fetchMetaData = false)
 {
     if (strlen(trim($url)) == 0) {
         return null;
     }
     if (StringHelper::startsWith($url, '//')) {
         $url = 'http:' . $url;
     }
     $curlHelper = new CurlHelper();
     if (!$curlHelper->exists($url)) {
         return null;
     }
     $image = new ExtractedImage();
     $image->setSource($url);
     if ((is_null($width) || is_null($height)) && $fetchMetaData) {
         $dimensions = getimagesize($url);
         if (!empty($dimensions) && is_array($dimensions) && count($dimensions) >= 2) {
             list($width, $height) = $dimensions;
         }
     }
     if (!is_null($width)) {
         $image->setWidth($width);
     }
     if (!is_null($height)) {
         $image->setHeight($height);
     }
     if (is_null($size) && $fetchMetaData) {
         $curlHelper = new CurlHelper();
         $size = $curlHelper->getContentLength($url);
     }
     $image->setSize($size);
     return $image;
 }
示例#2
0
 /**
  * @param Crawler $crawler
  * @return \pmill\Extract\ExtractedImage[]
  */
 public function extractVideos(Crawler $crawler)
 {
     $foundVideos = $crawler->filter("#primary-project-content .project-module-embed iframe")->extract(array('src'));
     $extract = new Extract();
     /** @var ExtractedImage[] $videos */
     $videos = array();
     foreach ($foundVideos as $url) {
         $providerClassName = $extract->findProvider($url);
         if ($providerClassName == '\\pmill\\Extract\\Provider\\Basic') {
             $video = new ExtractedVideo();
             $video->setEmbedUrl($url);
         } else {
             $pageFetcher = new Curl();
             $videoPageContent = $pageFetcher->fetch($url);
             $videoCrawler = new Crawler($videoPageContent);
             /** @var \pmill\Extract\Interfaces\ExtractProvider $provider */
             $provider = new $providerClassName();
             $provider->setUrl($url);
             $video = $provider->extract($videoCrawler);
         }
         $videos[] = $video;
     }
     return $videos;
 }