Jump to content

Problem retrieving filename from image field in API generated page


bmacnaughton
 Share

Recommended Posts

I am not able to get the original file path from an image.

I create a page (primary purpose is to hold images created by users) via code similar to this:

$p = new Page();
$p->of(false);
$p->template = 'wall-design-page';
$p->parent = wire('pages')->get('path=/wall-files/, include=all');
// various application specific field settings
$p->addStatus(Page::statusUnpublished);
$p->save();

// hardcoded for this example but dynamically generated
$filepath = '/serverroot/walls/tvxjmr8/png/2016-03-22-020945.eddi.png';

$p->wd_shared_image = $filepath;
$p->save();

Later I fetch these pages:

$wallpages = wire('pages')->find("template=wall-design-page, include=all");

And add them to an images field on the page and output information (for debugging):

foreach ($wallpages as $p) {
    $image = $p->wd_shared_image;
    $page->images->add($image);
    // debugging output
    $wall .= $p->wd_shared_image . ', ' .
        $image->url . ', ' . $image->path . ', ' .
        $image->filename . '<br/>';
}

That results in the following output:

2016-03-22-020945_eddi.png, /site/assets/files/1616/, /serverroot/dist/site/assets/files/1616/, 

The filename is missing after the last comma. Neither the URL nor the path include the filename. What happened to it?

Link to comment
Share on other sites

This looks like wd_shared_image is set to hold multiple images, so $p->wd_shared_image stringifies to a list of all of its childrens' image names but only url and path hold a sensible value as $p->wd_shared_image returns a PageImages instance.

  • Like 1
Link to comment
Share on other sites

// should return the full path
$image->filename

null

// use
$image->basename

null

// debugmode?
$config->debug = true;

true

This looks like wd_shared_image is set to hold multiple images, so $p->wd_shared_image stringifies to a list of all of its childrens' image names but only url and path hold a sensible value as $p->wd_shared_image returns a PageImages instance.

$p->wd_shared_image is an image field, not an images field, so it should only hold one image, right?

$p->wd_shared_image is returning a Pageimages object with count($image) === 1. But if I try

$image = $image.eq(0);

I get "Call to undefined function eq()".

Link to comment
Share on other sites

Thank you. This is helpful; I hadn't realized that an image field was potentially multiple images. And now I get file names. But not the original file name I supplied.

My filename was:

'/serverroot/walls/tvxjmr8/png/2016-03-22-020945.eddi.png'

And the output I now get has the first dot (between 020945 and eddi) replaced with an underscore. Is there some way to get back my original filename? Does it matter?

Link to comment
Share on other sites

That's the sanitizer (filename) at work. From PW's point of view, it doesn't matter. It will get the correct file (it has been renamed). So the question really should be, does it matter on your side? E.g. Do you have another API outside PW or another APP that will expect your image files named using a particular format? If the answer  is no, then it doesn't matter. If yes, I think Adrian has a file renamer module that can be configured to your heart's desire.

  • Like 3
Link to comment
Share on other sites

That's the sanitizer (filename) at work. From PW's point of view, it doesn't matter. It will get the correct file (it has been renamed). So the question really should be, does it matter on your side? E.g. Do you have another API outside PW or another APP that will expect your image files named using a particular format? If the answer  is no, then it doesn't matter. If yes, I think Adrian has a flie renamer module that can be configured to your heart's desire.

Thank you. It looks like PW has copied the file to a new name. It doesn't rename the existing file, so everything should work fine.

I appreciate everyone's help with the multiple problems.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...