Jump to content

Path with filename for image


wjkaluza
 Share

Recommended Posts

I am still learning the tags in processwire so I think this will be an easy question.  I am trying to create a tag that will display the path from the root to an image file that includes the image filename and extension.

I think I am close with the following, but how do I add the filename and extension?

<?php echo $page->image1_thumb->url ?>

Thank you.

Bill

Link to comment
Share on other sites

Hi Bill,

 

Welcome to PW. Everything (almost) you need is here in the docs http://processwire.com/api/fieldtypes/images/ and in the cheat sheet http://cheatsheet.processwire.com  :)

 

 

$image->filename// Image filename, including server path

$image->basename// Image filename, without server path

 

Others are: $image->name; should also work..etc...

Edited by kongondo
Link to comment
Share on other sites

Thank you for the references.  I have read through them, but I am obviously not understanding something that should be very simple.

If my image is called image1-cover and I want to display the image filename, including server path, how do I write the statement?  The following does not work:

<?php echo $image1_cover->filename ?>

Bill

Link to comment
Share on other sites

Bill,

Are you aware of the difference between single and multiple image fields? Check the "Maximum files allowed" field on the details tab of your image field. If it is set to anything but "1" then you will need to do something like:

<?php echo $image1_cover->first()->url; ?>

If it is "1", then you must omit the "first()"

I have used "url" as I would imagine that you don't really want "filename" as it would look something like:

/var/www/root_folder/site/assets/files/xxxx/filename.jpg

Does that help?

  • Like 1
Link to comment
Share on other sites

Bill,

Just to clarify what Adrian is saying...If you have a multiple image field (i.e. set to a number <1>), that image field will return an array (0 = unlimited, btw). Hence, you would need to loop through the array using a foreach if you want the multiple images, or can select a particular image in the array, using either "first", which grabs the first image, or eq($n) which is retrieval using an index...where $n is the index :)

  • Like 1
Link to comment
Share on other sites

Thank you both for your help.

I did read through the description of image and images fieldtypes, and I have studied the cheatsheet so I think that I understand the difference between single and multiple image fields.  I am using several single image fields  for different types of media (images, audio and video) to populate an xml file that will be read by a multi-media player.

The simple issue I am confronting is that I don't yet understand processwire's syntax well enough to structure the tag to provide the path to the file, including file name and extension so that the xml file can find the file and recognize its type.

image1_cover is a .jpg file, but the following line is returning nothing:

<?php echo $image1_cover->filename ?>

I can't believe that I have been unable to figure this out after several hours of effort, but it's obvious that I am missing something important--and probably simple.

Any ideas?

Bill

Link to comment
Share on other sites

Ok, I think I might know what is going on. You say "If my image is called image1-cover".

Is "image1-cover" the filename of the image, eg image1-cover.jpg or is it the name of the image field in PW?

You need to reference the image by its field name.

On a page you can do:

echo $page->image1_cover->first()->filename;

This line references the current page, then the image field name (the first image if the field is a multiple image field), then the filename of that image.

You can also reference fields from other pages, but I'll leave it simple for now.

Does that help?

  • Like 1
Link to comment
Share on other sites

Thanks guys.

Your emails caused me to re-examine the manner in which I was setting up the image field.

I had been focused on the logic and syntax of my code.  In fact, the problem was that I incorrectly assumed that I could leave the setting for Maximum Files Allowed on the Details tab at the default value of 0.  Now, I think I understand that 0 means there is no limit to the number of images--an array.  The tags I was using were for a single image which resulted in nul output.

Now, I just need to find a tag that provides the path only from the root.

Thank you, again.

Bill

Link to comment
Share on other sites

You can leave the setting at 0. Using first(), last(), or eq(n) will allow you to access whatever image you want from a multiple images field.

->url will get you the path from the root. eg. /site/assets/files/xxxx/filename.jpg

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...