Jump to content

Finding images


matjazp
 Share

Recommended Posts

Any better way to find all images? The fastest way. Without script timeout. A lot of images...

$timer = Debug::timer();

$allImages = array();           

foreach (wire('fields')->find('type=FieldtypeImage') as $f) {
    foreach (wire('pages')->findMany("$f>0, include=all") as $p) {
        $images = $p->getUnformatted($f->name);
        foreach ($images as $i) {
            $allImages[] = $i;
            foreach ($i->getVariations() as $variation) $allImages[] = $variation;
         }
    }
}

$totalItems = count($allImages);
echo "All images: " . $totalItems . " Timer: " . Debug::timer($timer);

 

Link to comment
Share on other sites

Only thing I can think of is to limit the search also by template:

foreach (wire('fields')->find("type=FieldtypeImage") as $f) {
    foreach (wire('templates') as $t) {
        foreach (wire('pages')->findMany("template=$t, $f>0, include=all") as $p) {
            // etc...
        }
    }
}

 

Link to comment
Share on other sites

Thx, diogo. Found out that find/findmany is very fast, it's getVariations() call where 95% of time is lost, probably due to filesystem calls (DirectoryIterator). I guess there is no way to improve that? 

Link to comment
Share on other sites

8 minutes ago, diogo said:

You could also get the images directly from the assets/files directory with PHP.

I could, but that would give me all the files, not just images. And even I get the images by extension, I would have to know if image is of FiletypeFile of FieldtypeImage, and also would have to differentiate between originals and variations. If PW would store variations in DB... 

Also, I'm searching the forum and cant't find the post, where someone described (could be you?) how to iterate through a lot of pages (all of them) but without running out of memory and execution time. 

Link to comment
Share on other sites

50 minutes ago, matjazp said:

where someone described (could be you?) how to iterate through a lot of pages (all of them) but without running out of memory and execution time. 

This one?

Edited by szabesz
link added :)
  • Like 2
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...