image_instantiate pulling data from wrong image

Hey all

I'm trying to build a simple plugin to do some stuff off the tags on an image. I started with the no_show example (I know its not supported, but it seemed like a decent starting place for what I wanted to do).

Anyway, I have a plugin that basically looks like:
`
...
zp_register_filter('image_instantiate', 'some_method');
...
function some_method ($imageObj) {
global $_zp_current_admin_obj, $_zp_loggedin;

$currentUsername = $_zp_current_admin_obj->getUser();

print "Current User: ".$currentUsername."
";
print "Current Image: ".$imageObj->getFileName()."
";

return $imageObj;
}
`
At first glance, this looks like it works. On each album page, it prints out the username and filename for each image displayed.

My issue is that, in an album with 2 images, when I specifically open the second image, it has the filename (and other metadata) for the first image in the album.

E.g. if I have an album "foo" with two files:
A.JPG
B.JPG

If I open up the second image at
https://myserver.com/foo/B.JPG
It shows:
Current User: [me]
Current Image: A.JPG

I'm sure this has something to do with my misunderstanding with how image_instantiate works, but can anyone point me in the right direction?

Thanks

Comments

  • OK, I don't think I understand how image_instantiate is supposed to work...

    Digging around in source code, I found some references to $_zp_current_image. So I tried that:
    `
    function some_method ($imageObj) {
    global $_zp_current_admin_obj, $_zp_current_image;

    $currentUsername = $_zp_current_admin_obj->getUser();
    $currentImage = $_zp_current_image;

    print "Current User: ".$currentUsername."
    ";
    print "File Name: ".$currentImage->getFileName()."
    ";
    print "Tags: ".$currentImage->getTags()."
    ";
    foreach ($currentImage->getTags() as $tag)
    {
    print " Tag: ".$tag."
    ";
    }

    return $imageObj;
    }
    `
    This works as I would expect on the image detail page (sorry if I'm using the wrong terminology there - I just mean clicking through to view an image from an album view).

    However, when looking at an album view with the two images in it, it shows an empty list for the first image, and the image details for the first image by the second image. I would have expected it to show the correct details by each image, or nothing...

    Meanwhile, my original method using imageObj->getTags() instead of $_zp_current_image->getTags() showed the correct details next to each image in the album view, but the totally wrong details in the image detail view.

    How can I reliably get a handle on the tags for each image regardless of whether I'm looking at an album view or an image detail view?

    Thanks
  • fretzl Administrator, Developer
    Why don't you just put something like this in the functions.php file (in your theme folder)

    `
    function printDetails() {
    global $_zp_current_admin_obj, $_zp_current_image;
    $currentUsername = $_zp_current_admin_obj->getUser();
    $currentImage = $_zp_current_image;
    echo "Current User: ".$currentUsername."
    ";
    echo "File Name: ".$currentImage->getFileName()."
    ";
    if ( $currentImage->getTags() ) {
    echo "Tags:
    ";
    foreach ($currentImage->getTags() as $tag) {
    echo $tag."
    ";
    }
    }
    }
    `
    You can then use `<?php printDetails(); ?>` where you want the details to appear.
    Note that on album.php you have to call it somewhere within the `next_image()` loop.
    Also note that there is a function `printTags()` available.
  • acrylian Administrator, Developer
    Fretzl's way is how to do it. The filter you used is something different and invoke when the image object is setup. That's a bit something different.

    If you want to show the data on the album view, you need to place fretzl's function within the `next_image` loop. `$_zp_current_image`is setup there as well.

    You can all do using the object model directly (see the tutorial) but the filter is not needed in any case.
  • Well... Because I eventually want to filter out images based on tag data, and I didn't think that's something that should be done from the theme, but rather from a plugin. I want the filtering to be done regardless of theme.

    This was just the first step - I'm trying to figure out how to loop through the items in an album or an item detail page and get the tags, and then filter them out of all results.

    I actually did hack something up that works, although setting
    imgObject->exists = 0
    and/or
    imgObject->setShow(0)
    both result in a placeholder "image does not exist" image. I was hoping to remove it from the display altogether. Presumably I'm catching things too late using image_instantiate?

    I guess let me be more clear - I want to exclude items from search results, album views and item detail views based on certain tag values. Possible? Am I so far off the track of how to go about this?

    Thanks
  • acrylian Administrator, Developer
    `image_instantiate` will be to late because that takes affect when the image object you want to filter is being setup. Here you could do things with the image.

    The right filter should be `image_filter` (`album_filter` for albums) and for search `search_criteria`.

    `exists` btw checks if the image exists so if there is on on the file system. `show` is the publish state.
  • Awesome, thanks acrylian - I'll give that a whirl.

    Thanks
  • hm... Sorry, two follow up questions...

    1 - image_filter appears to take a list of filenames from the file system rather than a list of image objects (that I can get the tags from). Is the right way to turn the filename into an image object:
    `
    $imageObj = newImage($_zp_current_album, $filename);
    `
    Or is there a better way? Can I rely on $_zp_current_album in image_filter?

    2 - It looks like image_filter gets called twice? E.g.:
    `
    ...
    zp_register_filter('image_filter', 'some_method');
    ...

    function some_method ($image_array) {
    print "--------------------------------------
    ";
    foreach ($image_array as $image) {
    print "Image: '".$image."'
    ";
    }
    print "--------------------------------------
    ";

    return $image_array;
    }
    `
    The print sections get printed out twice, E.g.:
    `
    --------------------------------------
    Image: 'IMG_0016.JPG-2-.JPG'
    Image: 'IMG_0013.JPG'
    --------------------------------------
    --------------------------------------
    Image: 'IMG_0016.JPG-2-.JPG'
    Image: 'IMG_0013.JPG'
    --------------------------------------
    `
    Am I registering some_method wrong? It only displays each image once, so not sure why the filter is getting called twice. I guess if I filter everything the same way both times its not a problem, though perhaps a waste of CPU cycles, but it just feels like I'm missing something...

    Thanks
  • acrylian Administrator, Developer
    `$imageObj = newImage($_zp_current_album, $filename);` is the right way to setup an image object.

    It makes sense to call it twice: You need to filter also the album thumb if set to random and the image list itself. I have to admit I think never have used the image_filter myself yet (those were made by a former colleague). I have to look where the filter calls are actually and if we have an example usage somewhere.

    But generally you should not attach functions to a filter that echo/print something. Just return the values only so the chain works if other calls are attached to the filter (just mention it to be sure).
  • Yep, definitely - all the prints are just debug statements for me to figure out what's going on. Maybe I should echo to the log or something, but I got lazy.

    Thanks for the info!
Sign In or Register to comment.