Get relative URL for images

How do I get relative URL’s for images?

Might be super simple but I can;t seem to figure the best way to get relative images URLs as opposed to getting absolute URLs with:

<?php echo $image->url() ?>

EDIT.

Managed to get it working with the following:

<?php if($image = $project->images()->sortBy('sort', 'asc')->first()) { ?>

    <img src="<?php echo $image->page()->uri(); echo '/',  $project->file()->filename() ?>" alt="Alt Text"/>
				
  <?php } else { ?>
			
    <img src="assets/images/hold-image-960x600.jpg"/>
			
  <?php } ?>

Ugly?

Why do you need relative URLs? Absolute ones are generally more robust.

I need relatives URLs so that I can implement this http://eugenedae.github.io/Magnific-Popup-History-API-Demo/

contrary to what I’ve posted, this bit of code

<?php echo $image->page()->uri(); echo '/',  $project->file()->filename() ?>

is actually for a ‘data’ attribute. If I get it working I’ll post the finished site here / in Kirby showcase.

But relative URLs won’t work on subpages of your site as they are always relative to the current page. Don’t absolute URLs work with the plugin? Most plugins support and actually recommend that.

Sorry for delay. It’s a 1 page site and the reason for relative as opposed to absolute is for cleaner URLS. Using the realtive URL would give me:

www.site.com/project/?item=project/image.jpg

and absolute would be

www.site.com/project/?item=http://www.site.com/project/image.jpg

I think first looks better.

As it turns out I’ve decided no to bother implementing this for now. You saved me some time. Thanks

Ah, now I get what you wanted to achieve.

But wouldn’t the following look even better?

www.example.com/project/?item=image.jpg

The project part is already in the URL, which makes the filename unique in this case.
And this syntax can be easily achieved using $page->file('something')->filename() as you wrote.

Excellent.

www.example.com/project/?item=image.jpg

is even better. I have 2 client projects which I think I’m going to use Kirby for (I’ve been experimenting with it for my own site up until know) as It’s such a flexible, quick and satisfying(!) CMS. Thanks for you help.

Forum / support is also v good.

Victor

1 Like