5121 Posts in 1527 Topics by 1119 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 257 Views |
-
SS3.0 - How to create dynamic image from external URL

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?
-
Re: SS3.0 - How to create dynamic image from external URL

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();
}
| 257 Views | ||
|
Page:
1
|
Go to Top |

