Rolling you own Gallery2 component
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.
Comments
Comment
1 Looks good
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!
Question
2 RE:Slide Show
Comment
3 Problem
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'));
[...]
Comment
4 Problem