# How to use the new asset class to generate thumbnails

**URL:** <https://forum.getkirby.com/t/how-to-use-the-new-asset-class-to-generate-thumbnails/4245>\
**Category:** Questions\
**Tags:** v2\
**Created:** [May 27, 2016, 7:22pm UTC](https://forum.getkirby.com/t/how-to-use-the-new-asset-class-to-generate-thumbnails/4245 "2016-05-27T19:22:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![drewbaren](https://avatars.discourse-cdn.com/v4/letter/d/a698b9/32.png) [@drewbaren](https://forum.getkirby.com/u/drewbaren)\
**Post date:** [May 27, 2016, 7:22pm UTC](https://forum.getkirby.com/t/how-to-use-the-new-asset-class-to-generate-thumbnails/4245/1 "2016-05-27T19:22:17Z")

</div>

Via Kirby 2.3’s [changelog](https://getkirby.com/changelog/kirby-2-3-0):

> New Asset class, which can now convert any external files/images into Kirby file objects and make them resizable with the thumb api.

I don’t believe there’s an example of this in the docs, and I can’t quite figure it out looking at Kirby’s core. I’m familiar with creating new media objects for files within `assets` ([described here](https://forum.getkirby.com/t/file-object-from-file-in-assets/3720)), but generating thumbnails from those media objects [wasn’t implemented](https://forum.getkirby.com/t/kirby-2-3-0-beta/3809/20) as of the 2.3 beta. Now that external images can be converted into file objects it sounds like generating thumbs from `assets` should be possible.

Here’s my current code. Getting the image’s ratio, height, etc. works, but attempting to get the url for a thumbnail with either method just returns the original image’s location within `assets`. No thumb is generated.

```auto
<?php
  $image = new Media (kirby()->roots()->index() . '/assets/images/hero-default.jpg', $site->url() . "/assets/images/hero-default.jpg");
  echo $image->ratio();
  echo $image->resize(1500)->url();
  echo thumb($image, array('width' => 1500))->url();
?>

```

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [May 27, 2016, 7:33pm UTC](https://forum.getkirby.com/t/how-to-use-the-new-asset-class-to-generate-thumbnails/4245/2 "2016-05-27T19:33:49Z")

</div>

As the change log says, there is a new class for that, called `Asset`. Its only difference to `Media` is that it supports thumbnails. Media itself can’t really support that as the thumb component is not part of the Toolkit while the `Media` class is.

So you can use the following code:

```auto
<?php
  $image = new Asset('assets/images/hero-default.jpg');
  echo $image->ratio();
  echo $image->resize(1500)->url();
  echo thumb($image, array('width' => 1500))->url();

```

---

<div class="post-metadata">

**Author:** ![drewbaren](https://avatars.discourse-cdn.com/v4/letter/d/a698b9/32.png) [@drewbaren](https://forum.getkirby.com/u/drewbaren)\
**Post date:** [May 27, 2016, 8:19pm UTC](https://forum.getkirby.com/t/how-to-use-the-new-asset-class-to-generate-thumbnails/4245/3 "2016-05-27T20:19:54Z")

</div>

Ah, that makes sense. The `Asset` path syntax seems to be a little different from the `Media` syntax [described here](https://getkirby.com/docs/templates/thumbnails#thumbs-for-files-outside-the-content-folder), however. Below is the code that ended up working for me. Thank you for your help, @lukasbestle.

```auto
<?php
  $image = new Asset('/assets/images/hero-default.jpg', $kirby->urls()->assets() . '/images/hero-default.jpg');
  echo $image->resize(1500)->url();
?>

```

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [May 27, 2016, 8:28pm UTC](https://forum.getkirby.com/t/how-to-use-the-new-asset-class-to-generate-thumbnails/4245/4 "2016-05-27T20:28:49Z")

</div>

Ah, yes. Thank you for correcting. Actually it’s even a bit simpler:

```auto
<?php
  $image = new Asset('assets/images/hero-default.jpg');
  echo $image->resize(1500)->url();
?>

```

You don’t need to pass the second param at all. 🙂

---

<div class="post-metadata">

**Author:** ![drewbaren](https://avatars.discourse-cdn.com/v4/letter/d/a698b9/32.png) [@drewbaren](https://forum.getkirby.com/u/drewbaren)\
**Post date:** [May 27, 2016, 8:31pm UTC](https://forum.getkirby.com/t/how-to-use-the-new-asset-class-to-generate-thumbnails/4245/5 "2016-05-27T20:31:00Z")

</div>

Even better, thanks!

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 4:57pm UTC](https://forum.getkirby.com/t/how-to-use-the-new-asset-class-to-generate-thumbnails/4245/6 "2025-02-06T16:57:55Z")

</div>


