Kirby external image upload?

Hi, ive made a field in my panel where i ask for youtube id, and i wanted to upload the youtube thumbnail to the page so it can be used on the parent page that is a galery, is it possible?

Youtube thumb can be retrieved from this link http://img.youtube.com/vi/VIDEOID/0.jpg.

Also, so i don’t spam with other topic, ive used this for my contact page:

Can i change the success message to the alert instead of changing to other page?

i tried to change on
if($email->send()) {
go(‘contact/thank-you’);
to
if($email->send()) {
$alert = “Thank You”;

but it doesnt work.

Thanks in advance.

I would add a separate success message:


...

// try to send it and redirect to the
      // thank you page if it worked
      if($email->send()) {
        $success = "Thank you";
      // add the error to the alert list if it failed
      } else {
        $alert = array($email->error());
      }

  ...
  return compact('alert', 'success');

...

And in the template:

...
<form method="post">
      <?php if($success) : ?>
        <div class="success"><?php echo $success ?></div>
      <?php endif ?>  
...
1 Like

It worked, thanks!
Any idea how to do the youtube thumbnail one?

How about using a panel hook and download the image from the url when the new post with the video is is saved?

1 Like

Not sure what a hook is sorry, i cant find it on kirby docs.

Currently, there is very little information apart from the changelog: http://getkirby.com/changelog/kirby-2-1-0

Basically, what you need to do, is set up a new hook panel.page.create and then inside this function, download the thumb:

kirby()->hook('panel.page.create', function($page) {
  //fetch thumb here ...
});
2 Likes

I’m really sorry, i usualy get by with guides and other documentation but i can’t find anything on uploading a file from an external link, can you tell me what the function is so i can look it up?
Thanks for everything.

Getting the image is not specific to Kirby, you can use the basic PHP functions.
To download a file from an external site there’s the following:

$imagedata = file_get_contents('http://img.youtube.com/vi/' . $page->youtubeUrl() . '/0.jpg');

After you fetched the file, you can store it with the page using:

file_put_contents($page->root() . DS . 'filename.jpg', $imagedata);

This stores the file in the directory of the page that has just been created.

3 Likes

Thanks, it worked. Final code looks like this:

kirby()->hook('panel.page.update', function($page) {
	if($page->template() == 'video')  {
		$imagedata = file_get_contents('http://img.youtube.com/vi/' . $page->youtube() . '/0.jpg');
		file_put_contents($page->root() . DS . 'thumb.jpg', $imagedata);
	}
});
2 Likes