How to create file object form assets folder?

Hi everyone :raised_hand_with_fingers_splayed:

In Kirby 2, I was able to create file object base on images from the assets folder.
I was using this line :

new Asset(“images/…../……jpg”)

In kirby 3, this class is not working anymore.

I tried to use File::create($props) , but i’m not sure what to put in the $props array.
https://getkirby.com/docs/reference/file/create

Is someone know how to create file object from the assets folder ? :slight_smile:

1 Like

You can use

$image = new Kirby\Image\image('assets/images/myimage.jpg);

You can’t call resize() or so on that object, though. We will have a replacement for the Assets class at a later date.

1 Like

Good, so I cannot create thumbnail like so:
$image = new Kirby\Image\image("assets/images/myimage.jpg");
$image->thumb(['width'=> 320])->url();

I have encountered the same during the beta’s. I’m convinced the Kirby team will come up with something which makes this possible soon.

You can however already use the Darkroom factory to generate thumbnails, but in my experience you’ll have to redo a lot of the things Kirby otherwise does for you.

Let’s hope this lands soon in Kirby :crossed_fingers:

Sorry for missing a better solution right now. I think we already have a ticket for it and will fix this in a future release.

4 Likes

That’ld be great, @bastianallgeier. Thx!

I’ld need something like that to deal with virtual pages that come with images attached.

Yes, please re-introduce the Asset class! It‘s been quite useful.

2 Likes

So, where are we standing with this?

For the time being, I would glady define global images / etc in the settings page, but this isn’t editable either - so how would I go about just grabbing an image from the assets folder, resize and output it, say, in my header snippet?

thx!

1 Like

Woaw, after searching for so long, I finally found an answer with this post: it’s currently not possible to get an Image object from the assets folder… =’)

The documentation is quite vague about assets in general, I haven’t even found the default path to the assets folder anywhere (it should at least be noted in the $kirby->url('assets') documentation I think), I just know it from Kirby 2.
I think it would be quite nice if we could do $kirby->asset('images/myimage.png') to get the corresponding File object, but then it would be necessary to have a method to get the Image object I guess.

Anyway, I hope this will soon be updated, thanks for the great work!

I’m replacing:

<?= (new Asset("assets/images/menu.svg"))->content() ?>

with

<?= new Kirby\Image\image("assets/images/menu.svg") ?>

but I only get the image URL. What am I doing wrong?

The new Asset class is available on the develop branch.

What do you expect to get? An image tag?

You have to use an image tag and put the image object as src attribute:

<img src="<?= url(new Kirby\Image\image("assets/images/menu.svg")) ?>">

That’s the way (I like it)

Thanks

Ah, but in that way I’m not getting the svg code, is the same as if I use:

<img src="assets/images/menu.svg">

That’s why I asked what you expected. svg() helper to the rescue:

<?php echo svg(new Kirby\Image\image("assets/images/menu.svg")) ?>

That’s definitely the way.

Thanks again

Hi, :raised_hand_with_fingers_splayed:

i’m still not sure what is the best solution to resize images from the assets folder.

I’m trying to create a srcset for my image.

Does someone has a working solution? :slight_smile:

@simon.bed You can use the Asset class:

$asset = new Asset("assets/images/someimage.jpg");
echo $asset->resize(100);
1 Like

@texnixe This solution has worked for me, but breaks when I try and combine it with the CDN setup described in the cookbook. The new asset breaks the file::version component returning the following error:

Argument 2 passed to Kirby\Toolkit\F::{closure}() must be an instance of Kirby\Cms\File, instance of Kirby\Cms\Asset given, called in /Users/max/Sites/prism/kirby/src/Cms/FileModifications.php on line 194

Please let me know if you see an easy way around this. Thanks!

Quick guess, check if it works if your remove the type hint File from this line:

'file::version' => function (Kirby $kirby, File $file, array $options = []) {

@texnixe thanks for the help. that fixes the error and allows the CDN link to be generated. However the image asset does not show up in the kirby /media folder. So the CDN can’t to pull it in. Any thoughts on how to handle that?

As a workaround, I’m making an assets folder within the content folder content/assets and hide it from the panel. This way I can access images with the standard Kirby tools and the CDN plugin should work as expected. Seems fine this way!