Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » SS3.0 - How to create dynamic image from external URL

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 589 Views
  • pouic
    Avatar
    Community Member
    16 Posts

    SS3.0 - How to create dynamic image from external URL Link to this post

    I'm trying to use this URL http://img.youtube.com/vi/'.$id.'/0.jpg to load image.

    now, need load the content inside on Image object for using the function return $this->Image()->CMSThumbnail() and display the image on gridfield.

    any idea?

  • pouic
    Avatar
    Community Member
    16 Posts

    Re: SS3.0 - How to create dynamic image from external URL Link to this post

    private function getFileByURL($url, $fileName){
    $basePath         = Director::baseFolder() . DIRECTORY_SEPARATOR;
    $folder            = Folder::find_or_make(self::$media_upload_folder); // relative to assets
    $relativeFilePath   = $folder->Filename . $fileName;
    $fullFilePath      = $basePath . $relativeFilePath;
          
    if (!file_exists($fullFilePath)){
    // download the file
    $fp = fopen($fullFilePath, 'w');
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    $data = curl_exec($ch);
    curl_close($ch);
    fclose($fp);
    }

    $file = new Image();
    $file->ParentID   = $folder->ID;
    $file->OwnerID   = (Member::currentUser()) ? Member::currentUser()->ID : 0;
    $file->Name   = basename($relativeFilePath);
    $file->Filename   = $relativeFilePath;
    $file->Title   = str_replace('-', ' ', substr($fileName, 0, (strlen ($fileName)) - (strlen (strrchr($fileName,'.')))));
    $file->write();

    return $file;
       }

    using example

    public function loadYoutubeThumbFromID($id){
    $f = $this->getFileByURL('http://img.youtube.com/vi/'.$id.'/1.jpg', $id . '-thumb.jpg');
    return $f->CMSThumbnail();
    }

    589 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.