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.

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   2438 Views


Community Member, 19 Posts

21 September 2012 at 10:24am

I'm trying to use this URL'.$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?


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

            $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,'.')))));

            return $file;

using example

public function loadYoutubeThumbFromID($id){
            $f =  $this->getFileByURL(''.$id.'/1.jpg', $id . '-thumb.jpg');
            return $f->CMSThumbnail();