Gallery2:GalleryEmbed:getImageBlock - Gallery Codex
Personal tools

Gallery2:GalleryEmbed:getImageBlock

From Gallery Codex

GalleryEmbed::getImageBlock()

With GalleryEmbed::getImageBlock($args); you can include image blocks (random images, specific images, ...) in other pages of your website. It offers all the options of the external imageblock (see site admin -> image block) for embedded G2 installations and for webservers that don't allow url_fopen (and thus don't allow the readfile() method to include the external imageblock).

The GalleryEmbed::init arguments differ for every installation. The following is just an example for a case where G2 is installed in a folder called /gallery2/ under the webroot directory and where the page where we include the image block is in a file in the webroot itself.

Also, we show here a single random image. You can show multiple blocks with a single call, just use a pipe (|) delimited list of blocks in the 'blocks' => string. For example, the 3 most recent images is 'block' => 'recentImage|recentImage|recentImage'. If you copy/paste the whole getImageBlock call you'll get the same image 3 times, so make sure to use a single call with a | separated list!

 <?php
 /* You'll have to change the /gallery2/ thing in the following 2 lines probably */
 require_once(dirname(__FILE__) . '/gallery2/embed.php');
 $ret = GalleryEmbed::init(array('fullInit' => true, 'embedUri' => '/', 'g2Uri' => '/gallery2/main.php'));
 if ($ret) {
     print 'GalleryEmbed::init failed, here is the error message: ' . $ret->getAsHtml();
     exit;
 }
 /*
  * See "Site admin" -> "image block" for all available options. the parameters are the same
  * as for the external imageblock
  */
 list ($ret, $bodyHtml, $headHtml) = GalleryEmbed::getImageBlock(array('blocks' => 'randomImage',
                                                                 'show' => 'title|date'));
 if ($ret) {
     print 'GalleryEmbed::getImageBlock failed, here is the error message: ' . $ret->getAsHtml();
     exit;
 }
 /* $bodyHtml contains the image block. print it somewhere on your website */
 print $bodyHtml;
 /*
  * $headHtml is not required. if you use imageframes for your imageblocks, you need to print 
  * $headHtml in the <head> section of your web page
  */
 ?>

The code above displays a random image.

You can also display a specific image. Simply replace randomImage with specificItem, and define it's ID with itemId, like this:

 list ($ret, $bodyHtml, $headHtml) = GalleryEmbed::getImageBlock(
   array('blocks' => 'specificItem', 'itemId' => 25, 'show' => 'title|date'));

If you know the path of the item but not the ID, you can find the ID like this:

 list ($ret, $itemId) = GalleryCoreApi::fetchItemIdByPath('albumName/imageName.jpg');
 if ($itemId) {
   list ($ret, $bodyHtml, $headHtml) = GalleryEmbed::getImageBlock(
     array('blocks' => 'specificItem', 'itemId' => $retrievedItemID[1], 'show' => 'title|date'));

Horizontal blocks display

Add CSS like this to your page to get the blocks to display horizontally:

 .one-image { float: left; }
 a img { border: 0; }

Just A Little Off the Top, Please

Instead of using

 print $bodyHtml;

above, I chose to reduce the amount of markup that was being injected into my pages with the following code:

 // extract just the parts we need from the cluster of markup gallery2 pours into $bodyHtml
 // only the thumbnail image with correct link to the gallery image it is tied to, 
 // and the info paragraph about it, are extracted
 $doc = new DOMDocument();
 $newdoc = new DOMDocument(); 
   $doc->validateOnParse = true;
   $doc->loadHTML($bodyHtml);
 $xpath = new DOMXpath($doc);
   $imagelink = $xpath->query("//td[@class='IMG']/a");
   $elements = $xpath->query("//p[@class='giInfo']");
 if (!is_null($elements) and !is_null($imagelink)) {
     $node1 = $newdoc->importNode($imagelink->item(0)->cloneNode(TRUE), TRUE);
     $node2 = $newdoc->importNode($elements->item(0)->cloneNode(TRUE), TRUE); 
     $newdoc->appendChild($node1); 
     $newdoc->appendChild($node2); 
     echo "<hr><h3>Gallery Sample</h3>
   <div id="galsample">
   {$newdoc->saveHTML()}
   </div>
 ";

and then used some basic css to style it slightly for the sidebar. Since we try and minimise the use of tables on our website structures wherever possible, this was an ideal way to control the display of the image sample without too much extraneous markup.