Integrating Gallery into MovableType - a tutorial

ryandlugosz

Joined: 2002-12-01
Posts: 2
Posted: Thu, 2003-03-13 18:33

All -

Since I didn't see anyone else who's posted a tutorial on how to do this, I posted one myself once I got my Gallery integrated into MovableType. You can view it here:

http://dlugosz.net/archives/000005.php

Please email me with any comments you have, I'd be happy to hear any suggestions!

-Ryan,

 
pthree

Joined: 2003-03-19
Posts: 8
Posted: Wed, 2003-03-19 06:50

I spent many hours today getting this working. It grabs the last 5 images from my nested album and displays them acrossed my movable type blog.

pthree
http://diversionmary.com

Blog is here http://diversionmary.com/dmblog/
Gallery is here http://diversionmary.com/gallery/art
This is what it looks like in raw php form http://diversionmary.com/dmblog/test.php

And the php/source to do this is such: http://diversionmary.com/dmblog/test.phps

<?
/*
* This little example prints a sorted directory
* listing, with dirs first and files afterwards.
*/

// Open current directory

if($handle = opendir("../albums/drawings")){
// Loop through all files
while(false !== ($file = readdir($handle))){

// Ignore hidden files
if(preg_match ("/thumb/", $file)){

// Put dirs in $dirs[] and files in $files[]
if(is_dir($file)){
$dirs[] = $file;
}else{
$files[] = $file;
}
}
}

// Close directory
closedir($handle);
// if $files[] exists, sort it and print all elements in it. $i = 0;
if(is_array($files)){
rsort($files);
while ($i < 5) {
$test = substr($files[i],0,3); $target_dir="../gallery/drawings";
$thumb_dir="../albums/drawings";
$target_url=substr($files[$i], 0, 3);
echo "<a href=".$target_dir."/".$target_url."><img
src=".$thumb_dir."/".$files[$i]."></a><br> ";
echo "";
$i++;
}
}
}

?>

 
kyliedog
kyliedog's picture

Joined: 2003-03-02
Posts: 25
Posted: Fri, 2003-03-21 08:05

Hi,

I tried out the php file you came up with and it seems to work pretty well. only one issue, it generates the 5 thumbnails but the first one isn't an image

<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class="pn-sub">Code:</font><HR></TD></TR><TR><TD><FONT class="pn-sub"><PRE>
<a href=../gallery/dhs/><img src=../albums/dhs/></a><br>
<a href=../gallery/dhs/aaj><img src=../albums/dhs/aaj.thumb.jpg></a><br>
<a href=../gallery/dhs/aai><img src=../albums/dhs/aai.thumb.jpg></a><br>
<a href=../gallery/dhs/aah><img src=../albums/dhs/aah.thumb.jpg></a><br>
<a href=../gallery/dhs/aag><img src=../albums/dhs/aag.thumb.jpg></a><br>
</TD></TR></TABLE><!-- BBCode End -->

Its very odd but its only pulling in 4 thumbnails and the first isn't one of them. Any suggestions?

 
pthree

Joined: 2003-03-19
Posts: 8
Posted: Fri, 2003-03-21 18:15

hmm weird, can you show me the page? If you toss an "s" after .php (.phps) it is color coded legible source. Perhaps you could try starting over with the source i've included below. Please make note of absolute paths to gallery albums and change to your paths. this will probably alleviate your problems. I also commented things out, so that it would be easier to read and modify. If you have any additional problems please feel free to contact me.

In addition this is what i've monkied my code into, it uses css2 code's to size the image width to 60px, height constrained proportion of that.

here is example:
http://diversionmary.com/dmblog/monkey.php

here is source to example:
http://diversionmary.com/dmblog/monkey.phps

and here is source:

<?
/*
* This grabs all thumbnails from a specified directory
* sorts them in reverse order and displays the last 7 files
* The files are displayed with their thumbnail href'd to the gallery page
* and the images are auto-sized down to 60pixels width, auto-height using
* css2
*/

// Open nested album absolute path

if($handle = opendir("/home/p/pthree/diversionmary.com/albums/drawings")){
// Loop through all files
while(false !== ($file = readdir($handle))){

// Ignore hidden files
if(preg_match ("/thumb/", $file)){

// Put dirs in $dirs[] and files in $files[]
if(is_dir($file)){
$dirs[] = $file;
}else{
$files[] = $file;
}
}
}

// Close nested album
closedir($handle);
// if $files[] exists, sort it and print last N elements in it, reverse order.
$i = 0;
if(is_array($files)){
rsort($files);
//specify how many items should be displayed
while ($i < 7) {

//specify where the nested album is displayed
$target_dir="http://diversionmary.com/gallery/drawings";

//specify where the item's thumbnail exists
$thumb_dir="http://diversionmary.com/albums/drawings";

//specify the object's identity for href, do not modify
$target_url=substr($files[$i], 0, 3);

//specify how the output will look. currently thumbnails will be 60px wide
//with 2 line breaks.
echo "<a href=".$target_dir."/".$target_url."><img
src=".$thumb_dir."/".$files[$i]." style="width: 60px"></a><br><br>";
echo "";
$i++;
}
}
}

?>

 
kyliedog
kyliedog's picture

Joined: 2003-03-02
Posts: 25
Posted: Sat, 2003-03-22 00:58

I used the new code and changed

while ($i < 7) {

To

while ($i < 5) {

as you suggested and its working beautifully! Thank you for taking the time to write something that we can all benefit from!

THANK YOU!!!!!!!!

 
pthree

Joined: 2003-03-19
Posts: 8
Posted: Sat, 2003-03-22 02:55

i have commented the source, monkey.phps but im not going to paste here as ive already cluttered the forum enough :smile:
http://diversionmary.com/dmblog/monkey.phps

 
sstteevvee

Joined: 2003-04-01
Posts: 14
Posted: Tue, 2003-04-01 01:14

Hi,

This is great, got it working nicely EXCEPt for the href goes to the thumbnail pic, rather than the full size pic. Is this the design or have i done something wrong?

Have used the code below, page is displayed <!-- BBCode Start --><A HREF="http://ukvsaus.com/modules/gallery/lastupdated.php" TARGET="_blank">here</A><!-- BBCode End -->

Any imput would be great,
Cheers,
Steve

 
pthree

Joined: 2003-03-19
Posts: 8
Posted: Tue, 2003-04-01 18:52

looks like you got it.

 
sstteevvee

Joined: 2003-04-01
Posts: 14
Posted: Tue, 2003-04-01 23:42

yeah made a few changes and it works on, now i have to put it into a phpnuke block!

new code is below if anyones interested (its the code above but hacked pretty badly. Works though :razz: )

Steve

<?
/*
* This grabs all thumbnails from a specified directory
* sorts them in reverse order and displays the last 7 files
* The files are displayed with their thumbnail href'd to the gallery page
* and the images are auto-sized down to 60pixels width, auto-height using
* css2
*/

//error_reporting(E_ALL);

// Open nested album absolute path

if($handle = opendir("/home/steve/ukvsaus.com/html/modules/albums/album02")){
// Loop through all files
while(false !== ($file = readdir($handle))){

// Ignore hidden files
if(eregi(".thumb.", $file)){
// Put dirs in $dirs[] and files in $files[]
if(is_dir($file)){
$dirs[] = $file;
}else{
$files[] = $file;
}
}
}

// Close nested album
closedir($handle);
// if $files[] exists, sort it and print last N elements in it, reverse order.
$i = 0;
if(is_array($files)){
rsort($files);
//specify how many items should be displayed number must be less than
//or equal to objects in gallery
while ($i < 10) {

//specify where the nested album is displayed
$target_dir="http://ukvsaus.com/modules/albums/album02";

//specify where the item's thumbnail exists
$thumb_dir="http://ukvsaus.com/modules/albums/album02";

//specify the object's identity for href, do not modify
$target_url=substr($files[$i], 0, -9);

//specify how the output will look. currently thumbnails will be 120px wide
//with 2 line breaks.
echo "<a href=".$target_dir."/".$target_url."jpg><img src=".$thumb_dir."/".$files[$i]." style="width: 120px"></a><br><br>";
echo "";
$i++;
}
}
}
?>

 
pthree

Joined: 2003-03-19
Posts: 8
Posted: Wed, 2003-04-02 17:15

yeah i ended up going with a 0, -9 substr for another person's site (he was matching movable type entries to like-named jpg's) the 0, 3 substr is really only useful if you are pointing to an "album" within gallery (as it forms its url with a 3 letter abbrev) glad you got what you needed working though. perhaps you can post a url to a finished page that is using that code as an example for other people?

 
sstteevvee

Joined: 2003-04-01
Posts: 14
Posted: Mon, 2003-04-14 06:49

no problems. made a few more changes as well and it seems to be working perfect for me!

code is as follows (ihave commented what changes i have made.) Scripts runs 10 or so individual times as each script is a phpnuke block. Can be seen on the left and right sides of the page <!-- BBCode Start --><A HREF="http://ukvsaus.com" TARGET="_blank">here</A><!-- BBCode End --> . Let me know what you think. steve at ukvsaus.com

Also, a brief disclaimer.....this may not look pretty in some peoples eyes but i'm only new at this....

Quote:
<?php
if (eregi("block-latestupdates",$PHP_SELF)) {
Header("Location: index.php");
die();
}
/*
* This grabs all thumbnails from a specified directory
* sorts them in reverse order and displays the last 7 files
* The files are displayed with their thumbnail href'd to the gallery page
* and the images are auto-sized down to 60pixels width, auto-height using
* css2
*/
//error_reporting(E_ALL);
// Open nested album absolute path - Change to your path
if($handle = opendir("/home/steve/ukvsaus.com/html/modules/albums/album11")){
// Loop through all files
while(false !== ($file = readdir($handle))){
// Ignore hidden files
if(eregi(".thumb.", $file)){
// Put dirs in $dirs[] and files in $files[]
if(is_dir($file)){
$dirs[] = $file;
}else{
$files[] = $file;
}
}
}
// Close nested album line 28
closedir($handle);
// if $files[] exists, sort it and print last N elements in it, reverse order.
//This is the main addition - changed rsort to arsort and did a count of the files in the dir.
$i = 0;
$plus1 = 1;
$filescount = count($files);
$numfiles = $filescount - $plus1;
if(is_array($files)){
arsort($files);
//specify how many items should be displayed number must be less than
//or equal to objects in gallery
//I only wanted 1 pic to display so you may need to alter this bit if not using as a nuke block
while ($i < $numfiles) {
//specify where the nested album is displayed- Change to your own
$target_dir="http://ukvsaus.com/modules/albums/album11";
//specify where the item's thumbnail exists - Change to your own
$thumb_dir="http://ukvsaus.com/modules/albums/album11";
//specify the object's identity for href, do not modify
$target_url=substr($files[$i], 0, -10);
//specify how the output will look currently thumbnails will be 120px wide
//with 2 line breaks.
$content="<a href=http://ukvsaus.com/modules.php?set_albumName=album11&amp;id=".$target_url."&amp;op=modload&amp;name=gallery&amp;file=index&amp;include=view_photo.php><img src=".$thumb_dir."/".$files[$i]." style="width: 120px"></a><br>";
echo "";
$i++;
}
}
}
?>

Cheers,
Steve :lol:

 
hezair

Joined: 2002-10-03
Posts: 3
Posted: Sun, 2003-06-08 05:11

1.

kyliedog wrote:
I tried out the php file you came up with and it seems to work pretty well. only one issue, it generates the 5 thumbnails but the first one isn't an image

i get this same thing, but it is because the album i want to use has a nested album within it. so i get links, with no thumbnail. i'd rather it ignored the links that go to sub albums... hmpf.

2. i was wondering about a way to integrate this in a moveabletype category template. where you'd have a MTCategory name and gallery album name the same, so you could include something like this in your Category Archive template: (assuming you use PHP for your MT archive pages)


$category = <$MTArchiveTitle$>   
...
if($handle = opendir('/pathto/gallery/albums/drawings'.$category)){ 
...
 $thumb_dir='http://website.com/gallery/albums/'.$category; 
(etc)

:( Sad News : This works only if you do not have spaces in your category names, because moveabletype and gallery 'dirify' in different ways.

Is the MT dirify method better? using _ underscores instead of - dashes... because if you want to use these same names in an XML file for example, or for other uses... the _ won't be misconstrued ...

Does anyone know if there is a way to change the way Gallery 'dirifies' the names?

 
phineasflapdoodle

Joined: 2003-05-08
Posts: 11
Posted: Sat, 2003-06-14 03:55

I think it would be much easier to "integrate" Gallery1.xx into a better blog open source product called Nucleus:

http://nucleuscms.org/

Has anyone tried that - I may be soon on a client's site.
Stephen

 
eggnumber41679

Joined: 2003-08-14
Posts: 4
Posted: Fri, 2003-08-22 18:09

I built off pthree's code a bit. This picks ten random images from all the albums in your /albums directory (including all subdirectories), displays the thumbnail, then links to the corresponding medium-sized photo in the gallery album.

Unfortunately, I don't have the coding prowess to enable exceptions (ie, private albums, etc), so if any of you have thoughts, they'd be much appreciated.

CF

<? 
$num = 10;

$DirectoriesToScan = array(realpath('./albums/'));
$DirectoriesScanned = array();
while (count($DirectoriesToScan) > 0) {
	foreach ($DirectoriesToScan as $DirectoryKey => $startingdir) {
		if ($dir = @opendir($startingdir)) {
			while (($file = readdir($dir)) !== false) {
				if (($file != '.') && ($file != '..')) {
					$RealPathName = realpath($startingdir.'/'.$file);
					if (is_dir($RealPathName)) {
						if (!in_array($RealPathName, $DirectoriesScanned) && !in_array($RealPathName, $DirectoriesToScan)) {
							$DirectoriesToScan[] = $RealPathName;
						}
					} 
					elseif (is_file($RealPathName) && eregi(".thumb.jpg", $file)) {
						$Directories[] = substr($RealPathName, 13);
						$FilesInDir[] = $file;
					}
				}
			}
		closedir($dir);
		}
	$DirectoriesScanned[] = $startingdir;
	unset($DirectoriesToScan[$DirectoryKey]);
	}
}

$FilesInDir = array_unique($FilesInDir);
sort($FilesInDir);
$random = array_rand($FilesInDir, $num+1);

$i = 1;

while ($i < $num) { 
	echo "<table border=0 cellspacing=1 cellpadding=3 bgcolor=333333><tr><td bgcolor=FFFFFF><a href=/gallery/".substr($Directories[$random[$i]],8,-10)."><img src=".$Directories[$random[$i]]." border=0></a></td></tr></table><br>"; 
	$i++; 
}
?>
 
jordy

Joined: 2003-09-22
Posts: 2
Posted: Mon, 2003-09-22 08:11

I use something slightly different. I don't want my main page to have to be written in php because it doesn't get indexed by search engines (and I'm far too lazy to set last-mod myself).

The code I have will only display non-hidden images. It's biggest flaw is that it doesn't handle sub-albums.

I just use the following code in a file called 'random_photo.php in the /gallery directory:


<?php
if (!empty($HTTP_GET_VARS["GALLERY_BASEDIR"]) ||
    !empty($HTTP_POST_VARS["GALLERY_BASEDIR"]) ||
    !empty($HTTP_COOKIE_VARS["GALLERY_BASEDIR"])) {
  print _("Security violation") ."\n";
  exit;
}

if (!isset($GALLERY_BASEDIR)) {
  $GALLERY_BASEDIR = './';
}

require($GALLERY_BASEDIR . 'init.php');

$albumDB = new AlbumDB(FALSE);
$numAlbums = $albumDB->numAlbums($gallery->user);

/* We keep looping until we get an unhidden photo and each time we switch albums just in case all photos in an album are hidden. */

while (1) {
  /* We use getVisibleAlbums because it doesn't force load each photo library like the alternatives do */
  $visible = $albumDB->getVisibleAlbums($gallery->user);
  $random = array_rand($visible, 1);
  $albumName = $visible[$random]->fields["name"];
  $album = $albumDB->getAlbumbyName($albumName);
  $numPhotos = $album->numPhotos(0);

  if ($numPhotos == 0) {
    continue;
  }

  $randPhoto = rand(1, $numPhotos);
  $photo = $album->getPhoto($randPhoto);

  if ($photo->isHidden()) {
    continue;
  }

  $thumbnail = $album->GetAlbumDirURL("thumb") . "/" .
               $photo->thumbnail->name . "." . $photo->thumbnail->type;

  header("Location: $thumbnail");
  break;
}

?>

And then in my blog I have something like the following:

<a href="/gallery/"><img src="/gallery/random_photo.php?0" border="0"></a>
<a href="/gallery/"><img src="/gallery/random_photo.php?1" border="0"></a>
<a href="/gallery/"><img src="/gallery/random_photo.php?2" border="0"></a>

(Note the ?0 is because browsers are far too smart for their own good, so making each a distinct url keeps it from displaying the same image three times).

You can see an example of this in action at http://www.zenzen.org/

 
jordy

Joined: 2003-09-22
Posts: 2
Posted: Tue, 2003-09-23 04:13

I made a modification because of browsers caching the location tag (so going to the same page over and over wouldn't cycle the images unleas reload was pressed). This reads the image straight out and passes it directly to the browser while setting the expires to force revalidation.

<?php
if (!empty($HTTP_GET_VARS["GALLERY_BASEDIR"]) ||
    !empty($HTTP_POST_VARS["GALLERY_BASEDIR"]) ||
    !empty($HTTP_COOKIE_VARS["GALLERY_BASEDIR"])) {
  print _("Security violation") ."\n";
  exit;
}

if (!isset($GALLERY_BASEDIR)) {
  $GALLERY_BASEDIR = './';
}

require($GALLERY_BASEDIR . 'init.php');

$albumDB = new AlbumDB(FALSE);
$numAlbums = $albumDB->numAlbums($gallery->user);

while (1) {
  $visible = $albumDB->getVisibleAlbums($gallery->user);
  $random = array_rand($visible, 1);
  $albumName = $visible[$random]->fields["name"];
  $album = $albumDB->getAlbumbyName($albumName);
  $numPhotos = $album->numPhotos(0);

  if ($numPhotos == 0) {
    continue;
  }

  $randPhoto = rand(1, $numPhotos);
  $photo = $album->getPhoto($randPhoto);

  if ($photo->isHidden()) {
    continue;
  }

  $thumbnail = $gallery->app->albumDir .  "/" . $albumName . "/" .
               $photo->thumbnail->name . "." . $photo->thumbnail->type;

  header("Content-Type: image/jpeg");
  header("Content-Length: " . (string)(filesize($thumbnail)));

  $fd = fopen($thumbnail, "r");
  fpassthru($fd);
  exit();
}

?>

As before, you have to reference each instance uniquely or the browser will just dup the same image out multiple times, so :

<img src="/gallery/random_photo.php?1">
<img src="/gallery/random_photo.php?2">
<img src="/gallery/random_photo.php?3">
 
peterh

Joined: 2003-07-01
Posts: 34
Posted: Wed, 2004-01-21 04:14

has anyone integrated gallery with moveabletype in such a way that an entire photo album can be displayed in a moveabletype entry?

it seems like the easiest way to do this (for my purposes anyway) would be to have moveabletype create a new album.header file in html_wrap for each entry. then i would modify view_album.php:

<?php includeHtmlWrap("album.header"); ?>

to

<?php includeHtmlWrap("album.header.$albumName"); ?>

and create one MT entry by the name of each gallery album. then i would create a category in my blog called photos and use the MT regex plugin to tell MT to replace links to entries in the "photos" category with a link to the respective gallery album..

thoughts?

p.