Create file from url

It’s a pain to get thumbnails for Vimeo videos so I’ve connected to their API and am trying to save a thumbnail image from Vimeo to Kirby. I’m almost there but am getting the error The media type for "....jpg" cannot be detected.

Does $page->createFile() allow creation of files from urls? Wish I could tell it that the MIME is jpg or similar. The main reason I need to saved is so I can crop it to the right size before displaying on the page.

1 Like

You could try using using php get file contents $vidimage = file_get_contents('http://www.vimeo.com/image.jpg'); and then use F::write to put it on disk.

Thats how i ripped Jpegs from mp3 files and stored them in the panel. It supports URL’s should work fine.

1 Like

@jimbobrjames if I use F::write, wouldn’t I need to store the img contents to a temporary folder before I can do something like $page->createFile()? If so, where would you put that folder? Under assets? or site/cache? or maybe my own cache?

1 Like

its bit confusing with the diffrent methods on diffrent objects, which is for creating the attachment and which creates the meta txt, or both.

I first curled a image directly into page content folder, but then had trouble creating the according filename.mp4.txt meta file, since source property was missing (I dont even remeber which of the “file create methods” I tried)

ANYWAYS this is my solution (copy image from instagram). with help from kirby reference and examples, maybe helpful for somebody


// $obj is return object from instagram with file url and more

$source_filename = $obj->id . '-media.mp4';
$source_path = getcwd() . '/tmp/' . $source_filename;
file_put_contents($source_path, curlFile($obj->media_url));

$file = File::create([
  'filename' => $source_filename,
  'parent' => $page,
  'source' => $source_path,
  'content' =>  [
    'caption' => 'on my'
  ]
]);

function curlFile($url)
{
  $process = curl_init($url);
  curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
  $res = curl_exec($process);
  curl_close($process);
  return $res;
}