Rolling you own Gallery2 component
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.
my blog http://vangel.3ezy.com








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.
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.
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?
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.
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'));
[...]
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