You are here

private function StatusHeartPost::getMedia in Heartbeat 8

1 call to StatusHeartPost::getMedia()
StatusHeartPost::sendRequest in modules/statusmessage/src/StatusHeartPost.php

File

modules/statusmessage/src/StatusHeartPost.php, line 120

Class

StatusHeartPost
@property \Drupal\statusmessage\MarkupGenerator generator @property message

Namespace

Drupal\statusmessage

Code

private function getMedia() {
  $fids = array();
  if ($images = $this->generator
    ->getImages()) {
    $num = count($images) > 10 ? 10 : count($images);
    for ($i = 0; $i < $num; $i++) {
      if ($i > 0 && $images[$i]['width'] === $images[$i - 1]['width'] && $images[$i]['height'] === $images[$i - 1]['height'] && $images[$i]['size'] === $images[$i - 1]['size']) {
        continue;
      }
      if (count($fids) < 6 && $images[$i]['width'] > 400) {
        $ext = strtolower(pathinfo($images[$i]['url'], PATHINFO_EXTENSION));
        if (!$this
          ->verifyExtension($ext)) {
          $ext = explode($ext, $images[$i]['url']);
          $ext = count($ext) > 1 ? $ext[1] : $ext[0];
        }
        $ext = strpos($ext, '?') ? substr($ext, 0, strpos($ext, '?')) : $ext;
        $fileName = strlen($ext) > 0 ? substr($images[$i]['url'], 0, strpos($images[$i]['url'], $ext)) . $ext : $images[$i]['url'];
        if (UrlHelper::isValid($images[$i]['url'])) {
          $data = file_get_contents($images[$i]['url']);
          $file = file_save_data($data, 'public://' . substr($fileName, strrpos($fileName, '/') + 1), FILE_EXISTS_REPLACE);
          $fids[] = $file
            ->id();
        }
      }
    }
  }
  else {
    if ($this->generator
      ->getImage()) {
      $ext = strtolower(pathinfo($this->generator
        ->getImage(), PATHINFO_EXTENSION));
      $ext = strpos($ext, '?') ? substr($ext, 0, strpos($ext, '?')) : $ext;
      $fileUrl = strlen($ext) > 0 ? substr($this->generator
        ->getImage(), 0, strpos($this->generator
        ->getImage(), $ext)) . $ext : $this->generator
        ->getImage();
      $mainImage = file_get_contents($fileUrl);
      $file = file_save_data($mainImage, 'public://' . substr($fileUrl, strrpos($this->generator
        ->getImage(), '/') + 1), FILE_EXISTS_REPLACE);
      $fids[] = $file
        ->id();
    }
  }
  return $fids;
}