How to create file object form assets folder?


#1

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:


#2

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.


#3

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


Kirby 3: Class 'Asset' not found
#4

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:


#5

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.


#6

That’ld be great, @bastianallgeier. Thx!

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


#7

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


#8

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!


#9

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!


#10

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?


#11

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")) ?>">

#12

That’s the way (I like it)

Thanks


#13

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">

#14

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

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

#15

That’s definitely the way.

Thanks again