Rolling you own Gallery2 component

By Abhishek Dujari (Vangel)
Gallery2 is a Media Manager and can be embedded in other applications like CMS. Gallery2 also has an API that we can use to embed Gallery into our Web Apps. Below is only a sample of what can be done. If you find it interesting enough, please contribute your improvements and suggestions as well as any code. I have only been working with CakePHP for little over two weeks, so not saying this is the best way. My first post so go easy :)

First of all you need to have installed CakePHP 1.2.xxx version (may work with 1.1xx but I have not tested that). You also need to have a working installation of Gallery 2. For this case I have used Gallery 2.3 svn. Upload a few images in Gallery2 before you begin. Don't worry about albums etc.


Note: You need to have Gallery2 installed on the same server as Cake. I used /dohnut for cake and /gallery2 for gallery2. Also do not restrict Guest view in Gallery2 or this will not work. make sure the group 'Everyone' in G2 (gallery2) has Full View access, by default they do.

Now if everything works individually and we just want to get on with it. Here's what we do.

1. Create a Gallery Component in your /app/controller/components name it gallery.php and paste the following code. Modify where necessary.

Component Class:

Download code <?php 
Class GalleryComponent extends Object{
    function 
startup(&$controller)
    {
        
/* Change this or specify in config or var?
'embedUri' can point to our example page, it should 
actually point to your embedded Gallery index page 
when you have one */
    
require_once('/var/www/html/gallery2/embed.php');
   
$ret GalleryEmbed::init(array('fullInit' => true
  
'embedUri' => '/dohnut/posts/randomstuff''g2Uri' =>
'/gallery2/main.php')); // Change this, could be in Config or var?
     
if ($ret) {
         
$data =  $ret->getAsHtml();
         return 
false;
         }
            return 
true;
    } 
/* Read the Fun Stuff towards the end of this article*/  
    
function imageBlock($options = array('blocks' => 'randomImage',
    
'show' => 'title|date')){
            
     
/*
      * 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($options);
     if (
$ret) {
          
$error $ret->getAsHtml();
         return 
$error;
     }
     
/* $bodyHtml contains the image block. 
         Print it somewhere on your website */
    
return $bodyHtml;
    }
}
?>

2. Now in any controller of your choice, which you can bake if you want we will write a function and its corresponding view. So let's say we have posts_controller.php. This function will display a single random image on each page reload.

Controller Class:

Download code <?php 
Class PostsController extends AppController {
    var 
$name  'Posts';
    var 
$components = array('Gallery');
    .
    .
    
/* your rest of the functions */
    
   // Our gallery function
  
function randomstuff(){
    
$this->set('g2data',$this->Gallery->imageBlock());
  }

}
?>

3. Now for the view.
create /views/posts/randomstuff.ctp with the following

View Template:

Download code
<H1> Random Pictures </H1.
<?php echo $g2data?>

..and we're done here!

now point your browser to the correct url http://.../posts/randomstuff and hit refresh as many times as it takes you to get satisfied.
Not so hard was it. :)

Resources:
http://codex.gallery2.org/Gallery2:API - The full API documentation


Fun Stuff: The function imageBlock is an actual copy paste from the API documentation with very minor changes if any. Try to work out other examples in their API.

Hope to see a better Gallery2 integration in Cake than there are in some *other* apps.

 

Comments 664

CakePHP Team Comments Author Comments
 

Comment

1 Looks good

Gallery2 is itself a pretty complete application, but your integration is simple and clean. For someone so new to cake, it's a bold article!

A few comments:
1. You never need include() or require() in Cake as there is always a better way. /vendors exists specifically for that.
2. Generally you can use the path constants that Cake defines to get to webroot and most paths, although really this isn't necessary here. And yeah, your comment about putting the path in config or elsewhere makes for a much more reusable component, but that's just me nitpicking.
3. Since this is output related it feels slightly more like a Helper. Converting this to a helper would be a trivial exercise. Then using it would be as simple as: echo $gallery->imageBlock(array('blocks' => 'randomImage'));
Congrats on a good article and welcome to CakePHP!
Posted Aug 5, 2008 by Tom OReilly
 

Question

2 RE:Slide Show

Nice post. I was wondering though whether there is a way that I can have the images roll in a slide show kind of way. How would I be able to accomplish that with the gallery component.
Posted Feb 18, 2009 by Abel Birya
 

Comment

3 Problem

Hi,
first, i want to thank you for this article.
I've followed all the steps, but i can't get it running. when i try to go into "posts/randomstuff", i get this errors:

Random Pictures 
Error (ERROR_BAD_PARAMETER)
in modules/core/classes/GalleryEmbed.class at line 1001 (GalleryCoreApi::error) 
in modules/core/classes/GalleryEmbed.class at line 852 (GalleryEmbed::getBlock) 
in /home/maroxe/Public/cake/app/controllers/components/gallery.php at line 25 (GalleryEmbed::getImageBlock) 
in /home/maroxe/Public/cake/app/controllers/posts_controller.php at line 63 (GalleryComponent::imageBlock) 
in /home/maroxe/Public/cake/cake/libs/object.php at line 115 (PostsController::randomstuff) 
in /home/maroxe/Public/cake/cake/dispatcher.php at line 227 (Object::dispatchMethod) 
in /home/maroxe/Public/cake/cake/dispatcher.php at line 194 (Dispatcher::_invoke) 
in /home/maroxe/Public/cake/app/webroot/index.php at line 88 (Dispatcher::dispatch) 
in /home/maroxe/Public/cake/index.php at line 61

this is the part i've modified in my gallery.php:

[...]
require_once('/home/maroxe/Public/gallery2/embed.php'); 
   $ret = GalleryEmbed::init(array('fullInit' => true,  
  'embedUri' => '/cake/index.php/posts/randomstuff', 'g2Uri' => 
'/gallery2/main.php'));
[...]

Posted Apr 3, 2009 by maroxe
 

Comment

4 Problem

Ok after an hour of search I found the problem. ImageBlock is a plugin that must be installed with Gallery2, you have to login as admin, go to admin site, plugins, download other plugins and install imageblock. After that the code posted by Vangel will work fine. I hope this help.
Posted Apr 29, 2009 by zzz zzz