tags:

views:

602

answers:

7

To clarify the title question, I have code such as:

<img src='gallery/main.php?g2_view=core.DownloadItem&g2_itemId=8161&g2_serialNumber=2&g2_GALLERYSID=5b24df90ee45f0e01795a3c01a1d634b'>

with the file actually residing in the file system under my webroot. Is there any way in PHP to retrieve the images real path being served such as:

<img src='images/gallery/album1/file1.jpg'>

Eg someFunction(longURL) ==> images/gallery/album1/file1.jpg

Thank you,

A: 

Which script are you using?

Garrett
I am displaying the images in a script of my own, but the Gallery script is the one that manages the images themselves. They exist in the filesystem but are displayed through links such as the one i posted.I am looking to retrieve the real path from the long one.
barfoon
A: 

Well obviously in main.php the query parameters are resolved to a real path. Just do whatever you are doing there?

If you need some help post some of the main.php code and I'll give you a hand.

Graphain
I've actually tried this - the code in Gallery is very abstracted and complex. Just wondered if there was a simpler way to resolve the path.
barfoon
+4  A: 

Given that url, it's quite easy to understand that it's using the g2_itemId=8161, which means that it's probably getting the path of the image from a database.

With that logic in mind, you can query the database and get the path you want programmatically.

function someFunction($id) {
   // select path_name from g2_items where item_id = $id;
}
someFunction("8161");
Luca Matteis
Good pick, especially given the gallery SID. (I didn't look closely enough and thought 8161 might lead to file8161.jpg or something).
Graphain
path_name isnt stored in the database as a single string. It is built up of a bunch of different chunks of the path. Its all fragmented and really confusing (Gallery experts out there - WHY?!). I would have just queried for it if I knew it was sitting there in the database.
barfoon
barfoon, can you email me the main.php file at my user name @hotmail.com? It's hard to diagnose this without some code :-)
Graphain
You can browse the code at: http://gallery.svn.sourceforge.net/viewvc/gallery/trunk/gallery2/
blueyed
+3  A: 

I'm quite sure, Gallery2 (which you are apparently using) has an internal method for this - at least it does this resolving at some place. You'd have to find that piece of code and could either use it directly (if it's e.g. a static method) or abstract it from there.

You may want to ask on the Gallery2 forums - it may even have been answered there already.

Using Google Codesearch, I've found that main.php appears to have the code to do this already:

$path = GalleryDataCache::getCachePath(
    array('type' => 'fast-download', 'itemId' => $itemId));

$path appears to be a file, which, when included provides maybe the vars you'll need.

blueyed
A: 

if you just want to change the img src url, you can use gallery2's rewrite module and rewrite

<img src='gallery/main.php?g2_view=core.DownloadItem&g2_itemId=8161&g2_serialNumber=2'>

to

<img src='images/gallery/album1/file1.jpg'>
ax
A: 

assuming this just uses "Location" redirects, then you should be able to use the following function to do this for you

function resolve_url($url)
{
    $location = $url;
    $lastlocation = '';

    while ($location != $lastlocation)
    {
        $lastlocation = $location;
        $context = stream_context_create(
            array(
                'method' => 'GET'
            )
        );

        $metadata = stream_get_meta_data(fopen($location, 'rb', false, $context));

        $headers = $metadata['wrapper_data'];

        foreach($headers AS $header)
        {
            if (preg_match("^Location: (.*)", $header, $parts))
            {
                $location = $parts[1];
            }
        }
    }

    return $location;
}
Mez
+1  A: 

Here's the way to do it using the Gallery2 API. I wrote it as a convenience function.

function get_g2_path($id) {
  include("embed.php");
  GalleryEmbed::init();
  list ($ret, $photo) = GalleryCoreApi::loadEntitiesById($id);
  if ($ret) { return null; }
  list ($ret, $path) = $photo->fetchPath();
  if ($ret) { return null; }
  return $path;
}

// Here's an example of how you'd call it:
print get_g2_path(8161);

Some notes:

  1. you have to provide the right path to the embed.php file that comes with Gallery2
  2. you only want to call GalleryEmbed::init() once per request, so if you want to call this function twice, move the first two lines of the function somewhere else
  3. you probably want to do something slightly more sane with error handling
bharat
This is perfect - thanks a lot.If I could reward you the bounty now, I would.
barfoon
Another follow up - this returns the path, sometimes when it is from the cache. These cached images are sometimes the small thumbnails. Is there any way to retrieve the path to the full sized image?
barfoon
Sure. In that case, you just need to track back from the cached image (which is called a derivative) to its source. Here's the code to do that:include("embed.php"); GalleryEmbed::init(); function get_g2_path($id) { list ($ret, $entity) = GalleryCoreApi::loadEntitiesById($id); if ($ret) { return null; } switch ($entity->getClassName()) { case "GalleryDerivativeImage": return get_g2_path($entity->getDerivativeSourceId()); break; case "GalleryPhotoItem": list ($ret, $path) = $entity->fetchPath(); if ($ret) { return null; } return $path; }}
bharat
(sorry, apparently you can't format code in the comments.. hope you can make sense of that)
bharat
Thank you so much for this - this is incredible. It works perfectly.
barfoon
No problem. FYI, we made this *much much* simpler in Gallery 3. :-)
bharat