barfoon
barfoon

Reputation: 28187

How can I programmatically obtain an image's REAL URL/Path?

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,

Upvotes: 1

Views: 1588

Answers (6)

Bharat Mediratta
Bharat Mediratta

Reputation: 476

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

Upvotes: 1

ax.
ax.

Reputation: 60187

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'>

Upvotes: 0

Mez
Mez

Reputation: 24951

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;
}

Upvotes: 0

blueyed
blueyed

Reputation: 27878

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.

Upvotes: 3

Luca Matteis
Luca Matteis

Reputation: 29267

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");

Upvotes: 4

Matt Mitchell
Matt Mitchell

Reputation: 41861

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.

Upvotes: 0

Related Questions