You are here

private function TweetFeed::processTwitterImage in Tweet Feed 4.x

Same name and namespace in other branches
  1. 8.3 src/Controller/TweetFeed.php \Drupal\tweet_feed\Controller\TweetFeed::processTwitterImage()

Process Images from URL

Allows the passage of a URL and a saves the resulting image in that URL to a file that can be attached to our node. These are mostly used in user profiles and avatars associated with user tweets.

Parameters

string url: The twitte.com url of the image being retrieved

string type: The type of image. This affects the folder the image is placed in.

int id: The id associated with this image

bool $add_date: Do we add the year and month on to the end of the path?

Return value

object file The file object for the retrieved image or NULL if unable to retrieve

1 call to TweetFeed::processTwitterImage()
TweetFeed::saveTweet in src/Controller/TweetFeed.php
Save the tweet to our tweet entity.

File

src/Controller/TweetFeed.php, line 487

Class

TweetFeed
Class TweetFeed.

Namespace

Drupal\tweet_feed\Controller

Code

private function processTwitterImage($url, $type, $id, $add_date = FALSE) {

  // I hate myself for this next line.
  $image = @file_get_contents($url);
  if (!empty($image)) {
    $file = FALSE;
    list($width, $height) = getimagesize($url);
    $this
      ->checkPath('public://tweet-feed-' . $type . '-images/', $add_date);
    if ($add_date == TRUE) {
      $file_temp = file_save_data($image, 'public://tweet-feed-' . $type . '-images/' . date('Y-m') . '/' . $id . '.jpg', 1);
    }
    else {
      $file_temp = file_save_data($image, 'public://tweet-feed-' . $type . '-images/' . $id . '.jpg', 1);
    }
    if (is_object($file_temp)) {
      $fid = $file_temp->fid
        ->getValue()[0]['value'];
      $file = [
        'target_id' => $fid,
        'alt' => '',
        'title' => $id,
        'width' => $width,
        'height' => $height,
      ];
    }
    return $file;
  }
  else {
    return FALSE;
  }
}