Extracting image URLs via the core API
|
billw
Joined: 2008-05-10
Posts: 3 |
Posted: Sun, 2008-05-11 22:16
|
|
I'm using the core API to locate image blocks, but what I really want is not the HTML, but the URLs to a) the thumbnail and b) one of the scaled derivative images (not the original source). I can't find a way to get those directly--I can extract the thumb URL out of the htmlBody returned from getImageBlock, but the link to the larger image is a link to an HTML page, not just the scaled image. I can see from the URLs that, for example, the thumb is: /gallery2/d/87-2/aphoto.jpg and the scaled image that I want is: /gallery2/d/88-2/aphoto.jpg Is there any way via the API to get the URL of the above scaled image, or to consistently derive it from the thumb URL? Thanks. |
|
| Login or register to post comments |

Posts: 965
In general you can download any image by knowing its id, that goes for derivatives too, so by happy coincidence since thumbnails and derivative id's often (though not necessarily) follow sequentially from the id of the original image you could download the image, the thumbnail and the derivative directly (from your example above) with the following urls:
/gallery2/main.php?g2_view=core.DownloadItem&g2_itemId=86
/gallery2/main.php?g2_view=core.DownloadItem&g2_itemId=87
/gallery2/main.php?g2_view=core.DownloadItem&g2_itemId=88
The "-2" is a serial number added to prevent problems with browser caching (I think) if the image changes (whereupon Gallery will change the serial number), I'm sure an expert will jump in to correct me if I'm wrong in saying that it's not so important but you can also add it to the above urls by adding
...&g2_serialNumber=2Your shortform urls above are generated by the urlrewrite module, but they can confuse things.The difficulty then is to determine the correct itemId for the derivative that you want given the original item Id (presuming that's the information you're starting with.) That's a "simple matter of programming" and hard to generalise since there can be many derivatives with/without watermarks, different sizes etc and how many you have depends on your installation. So any method of determining the correct derivative has to cope with these different things. If you're lucky then you can just add 2 to the id of the original - but this will fail, if for instance two people have uploaded images simultaneously and the sequence numbers for the new Gallery entities are given out to each alternately.
If you're in a hurry you can install the simpleviewersource module (http://codex.gallery2.org/Gallery2:Modules:simpleviewersource) which has a View (DownloadMax) that given the original itemId will return a redirect to a Gallery derivative whose size is greater-than or equal to a size you specify, for example:
gallery2/main.php?g2_view=simpleviewersource.DownloadMax&g2_itemId=86&g2_maxImageHeight=600&g2_maxImageWidth=600would return you the smallest Gallery image derivative of item id=86 that's bigger in both dimensions than 600x600 (or the original, if nothing matches that criterion). You can omit either or both of the size parameters if you wish.
Posts: 3
I finally got back to this … thanks for the response.
What I’m doing is using Gallery 2 for all image management, but I want to display the thumbs and scaled images outside of gallery because I already have custom pages for image display for example, if I’m using thickbox or some other rollover where what I really want is a) the thumb, and b) the “preferred” size scaled image, such as to fit in a predefined display div.
I wrote a wrapper goes through the derivatives and fetches me what I specify. In some case it will return a scaled image (looking at the derivative sizes) or if the original image didn’t require any scaling, it will return that.
Posts: 3
I did need to add the serial numbers to the URLs to prevent cache problems.
I can now display every image, thumb, and scaled image in the gallery with:
$gbrowser = new GalleryBrowser(array( 'baseUrl' => '/gallery2', 'preferredScaledSize' => 600, // max width and height )); $rootAlbum = $gbrowser->getRootAlbum(); $albums = $rootAlbum->getAlbums(); foreach ($albums as $album) { display("title={$album->title} summary={$album->summary}"); $images = $album->getImages(); show($images); } function show($images) { foreach ($images as $image) { display($image->title); display($image->summary); display( '<img src="' . $image->thumbUrl . '" />"'); display( '<img src="' . $image->url . '" />"'); display( '<img src="' . $image->scaledUrl . '" />"'); } } function display($what) { echo "<p>{$what}</p>"; }