Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

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


Go to End


2 Posts   3296 Views

Avatar
pouic

Community Member, 19 Posts

21 September 2012 at 10:24am

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?

Avatar
pouic

Community Member, 19 Posts

16 October 2012 at 9:04am

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();
        }