Change default image directory

By default, all images for a page should be added to their respective directories in the content directory. However, in my structure, all images are in an images directory in the asset folder. Now if I add a new image through a panel in a specific page it is being added in the page directory in the content folder not in the image directory in the assets folder. How do I fix this?

The assets folder is not intended to store user added files, but rather site wide images like a logo, icons etc… The only. way you could achieve that would be via a hook that moves an uploaded image to the assets folder. But you would have to link to such images manually and can’t use Kirby drag and drop functionality.

Why are you doing this? If you want to store all images in one place, you can either do so in the content folder (site options) or in a separate images folder in content.

This is my current directory structure

- assets
    -images
        -team
            //all team page images
        -about 
            //all about page images
- content
    -team
        team.txt
    -about
        about.txt

and I link all images using <?php echo kirby()->urls()->assets . '/images/"any image name"' ?>

According to you, I should have a directory structure like this:-

- assets
- content
    -team
        team.txt
    -about
        about.txt
     -images
        -team
            //all team page images
        -about 
            //all about page images
  1. How will I get the path to my images?
  2. if a new image will be added from suppose the team page panel. it will not get inserted in the content/images/team directory rather it will go in content/team directory.

How do i fix these ?

If you put them in different subdirectories, you might as well put them into the corresponding content folders. What is your reason for not doing this?

I want to keep all my images in a single folder. The folder can have sub-directories or not but all images should be at the same parent location.

In your template, you could the get the images like this:

<?php 
// fetch all images from /images/about
$images = page('images/about')->images();
foreach($images as $image):
 echo $image->url();
endforeach;

If you upload an image to content/team, it will get stored in that folder. To upload an image to the images/team folder, you would have to go into that folder. Or move the image from the content/team folder to the content/images/team folder via an upload hook.

Great ! Thanks for that.
But one issue still prevails that if I add a new image from the about panel it will go into content/about and not in content/images/about. This code will fail then.

See edited post above.

Thank you I’ll look into that.
One last thing can you modify the above code to access images from assets/images (if my images folder is in assets) .

Also what will be the move logic in the the upload hook:

kirby()->hook('pane.file.upload', function($page) {
    //move logic here
})

You can’t use the same code as above for the assets folder. To use images from the assets folder in a similar way, you would have to read the images from. the folder, then create a new Asset for each file. Or just call a file manually, but that probably doesn’t make sense.

As for the code within the hook, maybe this helps: https://gist.github.com/samnabi/f4ecb351f2ec2bd596e5 You would have to adapt that, because you don’t want to shrink images, but it is a starting point.

Cool! Thanks for the help!