Rolling you own Gallery2 component

This article is also available in the following languages:
By Vangel
update Jan 28 2010: This Article is deprecated and here for reference and historical purposes. It is still usable however as comments below suggest and I agree, Helper is more suited for such simple functionality.

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:

<?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:

<?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:


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

my blog http://vangel.3ezy.com

Comments

  • Posted 01/28/10 09:55:32 PM
    Thanks lorenzo for your help and the idea of dividing to component and helper.

    I will port it to helper, that is not an issue. But the HTML and the output is by Gallery2, its the way its API works. I have noticed that in most cases it will adapt to the CSS we have in Cake. Some html however seems is just linked to its theme or modules (hardcoded styles?).

    Your view on session handling tells me you understand what I am referring to. So yeah, I'll need both com and helper, just helper will not do.

    let me work on an update so I can post a new one and then update this article to point to the new release. So devs can have both for their reference. It's been awhile since I published this (mid 2008), and the publish date is now 2010 so i hope it doesn't confuse anyone.
  • Posted 12/07/09 07:54:46 PM
    Sorry for not responding. I setup my email to receive comments but for some reason I never saw them (Junked?). Anyways, Cakephp has come a long way. Thanks for posting your feedback.

    Why I used component? Easiest at the time for adding the embed. Cake pretty much just magically talk to embed.php code of G2.

    There is no reason you cannot use it as Vendor or Helper. It all depends on what you want to do with G2. (I also needed to sync Login info between Cake and G2 which i achieved by extending the code above using Init methods so helper extension would be only to extend the Views functionality portion)

    I am working on Mobile platform G2 interface using CakePHP. I can drop the mobil UA code into cake and see how it goes.
    • Posted 01/05/10 10:33:31 AM
      Since your code only exposes html generating methods, could you make this into a helper?

      I can't find reasons to use a component here.
      • Posted 01/27/10 05:51:44 PM
        Since your code only exposes html generating methods, could you make this into a helper?

        I can't find reasons to use a component here.

        I could port it helper. Or i can add the login and other sample functions. The G2API outputs everything in HTML. It cannot be helped. However there are image processing function, for e.g. which I dont think will bode well as helper.

        What do you suggest?
        • Posted 01/28/10 08:03:45 AM
          You could just split the session handling part of your class and leave it in the component and the html related parts into a helper.

          It is just a concern of using the correct tools cake gives you. It is not a good practice to send formatted html to the view. The view layer is responsible for this task.

          I'll publish it, surely someone will find this very useful. And the recommendations could be done very easily.
  • Posted 04/29/09 10:27:53 AM
    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 04/03/09 06:41:20 PM
    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 02/18/09 02:47:26 AM
    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 08/05/08 09:54:24 AM
    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!

Comments are closed for articles over a year old