Jump to content

findRaw and images


MSP01
 Share

Recommended Posts

Is there any smart way to get image variants (and create them if necessary) when using findRaw?

We were thinking of using it to get array of several thousand products instead of using page array for performance reasons, but of course getting images becomes a problem. Is using a regular page array a better idea after all? We will need to turn it into an JSON format after so the page array isn't needed except to get the information out.

  • Like 2
Link to comment
Share on other sites

  • 1 year later...

I am also wanting a solution for this, or at least get the path to to the image url. I want to use this data in a data grid to render the image. Normal pages->find operator is slow with many pages, so it is no fit for me. I also tried findJoin, but it also seem it does not work for images?

We should open a request at processwire/processwire-requests: ProcessWire feature requests. (github.com). Are you doing it, @MSP01?

  • Like 2
Link to comment
Share on other sites

I ended up doing a file_exists() on the image variation I am wanting to call (built from the returned filename) and if it doesn't exist, then get the full page and call the size on the image. This should only happen once until the reduced size is created and then we're back to being able to load directly from the raw data.

Link to comment
Share on other sites

2 hours ago, adrian said:

I ended up doing a file_exists() on the image variation I am wanting to call (built from the returned filename) and if it doesn't exist, then get the full page and call the size on the image. This should only happen once until the reduced size is created and then we're back to being able to load directly from the raw data.

Hey adrian, can you give us a short code example please? Thanks!

Link to comment
Share on other sites

14 hours ago, bernhard said:

You can always add another field that you just populate on saveReady with the url/path of the image file and that will then be easily available for findRaw()

Thanks for the workaround idea, but it requires to re-save all pages that have an image.

But still I would love to support for getting pageImages in the core findRaw.

Link to comment
Share on other sites

Just like I would normally get the image url via the pages API. I just need one image in my case and so the output of a request like

$result = $this->pages->findRaw(
  'template=shop' . $filters,
  [
    "id",
    "title",
    "myImage.httpUrl",
  ],
  ['flat' => true]
);

should include the path to the image.

Or if I just reference the image file as "mo_image" in the query, I get this result:

image.png.c2572ba9ed57ca941c05396c44d0513b.png

There is much information about the file, so why not include the url in it?

Also I don't understand why the results are always arrays, as they are normally just single values.

This problem also exists for page reference fields which can be seen in Add $pages->findRaw() option to return "single" Page Reference field values directly · Issue #458 · processwire/processwire-requests (github.com) (please vote for it to be fixed).

I know that in RockFinder you can get the image url via the callback approach, which I like. But this again queries the page with all it's fields and so the speed improvement of findRaw is gone.

Link to comment
Share on other sites

19 hours ago, 3fingers said:

Hey adrian, can you give us a short code example please? Thanks!

Really rough, but gets the job done. You could easily put this into a function.

This builds up the name of variation (assuming you want 330x330), checks to see if it exists and if it doesn't then we need to go old school and get the page object and image and generate the variation. Then next time the page is loaded, the variation will exist and we can display it with $imageUrl as the image src directly from the data returned by findRaw.

if(count($p->image) > 0) {
    $imageOrigUrl = $config->urls->files. $p->id . '/' . $p->image[0]['data'];
    $path_parts = pathinfo($imageOrigUrl);
    $imageVarFilename = $path_parts['filename'] . '.330x330.' . $path_parts['extension'];
    $imageUrl = $config->urls->files . $p->id . '/' . $imageVarFilename;
    if(!file_exists($config->paths->files. $p->id . '/' . $imageVarFilename)) {
        $image = $pages->get($p->id)->image->size(330, 330);
        $imageUrl = $image->url;
    }
}

 

  • Like 2
  • Thanks 1
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...