How to show images from CSV?

Hi, Im trying to adapt this guide https://getkirby.com/docs/guide/virtual-pages/content-from-csv
to my needs.

I have CSV file
data.csv
Inside I have image field with the file name for example some-image-logo.jpg
I have a folder with images here ‘assets/data’ where all these images are placed

How I can get them in the template?

I need the ability to get relative or absolute URL to /assets/data/some-image-logo.jpg
moreover, if it possible after that somehow have an ability to create thumbs

<?= $img->thumb(['width' => 30, 'blur' => 30, 'quality' => 70])->url() ?>

Could you post your model please?

<?php

class GamesPage extends Page
{

    public function children()
    {
        $csv      = csv($this->root() . '/games.csv', ';');
        $children = array_map(function ($game) {
            
            $slug = Str::slug($game['Name']);
            
            return [
                'slug'     => $slug,
                'template' => 'game',
                'model'    => 'game',
                'num'      => 0,
                'content'  => [
                    'title'       		=> $game['Name'],
                    'description'  		=> $game['Description'],
                    'text'  			=> $game['Review'],
                    'iframe_url' 	 	=> $game['Demo URL'],
                    'mobile_url'  		=> $game['Mobile URL'],
                    'date' 				=> $game['Release Date'],
                    'gaming_software'  	=> $game['Software'],
                    'cover'  			=> $game['Image'],
                    'background'  		=> $game['Background'],
                    'rating'  			=> $game['Rating'],
                    'rtp'  				=> $game['RTP'],
                    'volatility'  		=> $game['Volatility'],
                    'reels'  			=> $game['Reels'],
                    'paylines'  		=> $game['Paylines'],
                    'min_bet'  			=> $game['Min Bet'],
                    'max_bet'  			=> $game['Max Bet'],
                    'games_category'  	=> $game['Category'],
                    'games_type'  		=> $game['Type'],
                    'games_features'  	=> $game['Features'],
                    'games_themes'  	=> $game['Themes'],
                    
                ]
            ];
        }, $csv);

        return Pages::factory($children, $this);
    }

}

and this how it looks on site:

I assume this is the image filename?

Then in your template you can get an Asset from the URL using the asset() helper

$image = asset('assets/data/' . $page->cover());
echo $image->url();

Should then also work with $image->resize(200);.

When i’m try to resize it gives error

The file::version component must return a File or FileVersion object

Sorry was my error. Fix it!
Images works and resize too!

Don’t forget to use an if statement

if ( $image = asset('assets/data/' . $page->cover()) ) {
  echo $image->resize(200)->url();
}
1 Like

Something strange happends after i start use this model.
When I first enter the list of items, thumbs don’t creates.
Only after I reload a page image is works.
Moreover, other content pages with images also stops generate images.

As I can see images is goes to jobs folder… What does it mean?

That means that the job for the thumb is created but not the thumb itself… I assume other images from normal content folders don’t have this issue?

Have issue with normal content folder too, if the Model is work. If I delete everything from Games Model images works fine.

Does that mean the presence of the games model affects thumbs of other pages that do not use this model?

:thinking:

That’s right. Today we added a review and there was no image generated either.
Even when you upload an image icon in the panel, you can’t see it until you reload the page.
Now I’m gonna try to delete the media folder and see what happens.

It’s bad as expected.

And I made sure it was the model problem. I go to the page with the model enabled (and it doesn’t matter what template or page type I go to, whether it’s news or review.) and the images are not generated, I remove the model and the images are loaded correctly on all pages.

And you didn’t introduce any empty first lines by chance in the model (or anywhere else)?

1 Like

Holy Mother of God )
It was in the empty 1st line)))).
It’s not possible. :sweat_smile: :sweat_smile: :sweat_smile: