This is a fix to the Image gallery tutorial - makes it a fully dynamic gallery

After searching and reading a couple of hundred post on the **truly GREAT **Kirupa image gallery tutorial - and wondering why nobody has written a new tutorial or at least updated the “old” one to avoid overloading the net with flash image gallery questions.

Im trying to create a new thread that will serve as a basic startingpoint for all Kirupa gallery questions. Im no überscripter nor do I know a lot about flash, but with a little common sense and perhaps some help from all of you… oh well - here goes:

Quick Checklist:

[list]
[] If your JPG’s wont load - Make sure you don’t save them as progressive JPG’s, as they wont load in Flash.
[
]If your images dont align to the photo container movieclip it is probably because your registration point of the photo MC is set to center instead of topleft.
[/list]Below is the code I have pieced together from different posts on the subject (I would like to credit all the authors, but that would require me reading all the posts again, as I have forgotten where I got them.

It produces a gallery that loads files dynamicaly thru a phpscript and checks for maximum image height/width and scales the images to fit within the *photo *MC. It also sets the center of the displayed images to a given coordinate (x,y).

All in all this is a install and forget image gallery, where the only maintaining required is uploading new pictures to the image folder.

On the server the file/directory structure is:

[list]
[]gallery swf
[
]filearray.php
[]images/
[list]
[
]file_x.jpg
[]file_y.jpg
[
]etc…
[/list]
[/list]This is the filearray.php - modify this to check for files in the path set in the actionscript line: *this.pathToPics = “images/”;


  <?php
  if ($dir = opendir("images")) {
    while (($file = readdir($dir)) !== false) {
  		$cont++;
  	if ($file == "." || $file == "..") { } else {
  			$string.= ($file);
  		$string.= "&";
  		}
  	  }
  	closedir($dir);
  }
  print($string);
  ?> 
  

This is the modified actionscript from the original Kirupa gallery script:


  // This loads the array created by filearray.php and puts it into pArray 
  lv = new LoadVars();
  lv.load("filearray.php");
  lv.onData = function(text){
  pArray = text.split("&");
  for (i=0; i<pArray.length-1; i++) {
  trace(pArray*);
  }
  }
  
  // variables ------------------------------------------
  // put the path to your pics here, include the slashes (ie. "pics/")
  // leave it blank if they're in the same directory
  this.pathToPics = "images/";
  // fill this array with your pics (set from filearray.php)
  this.pArray = pArray;    
      
  this.fadeSpeed = 20;
  this.pIndex = 0;
  
  // MovieClip methods ----------------------------------
  // d=direction; should 1 or -1 but can be any number
  //loads an image automatically when you run animation
  loadMovie(this.pathToPics+this.pArray[0], _root.photo);
  
  // Center the photo at (x,y) the coordinates are set in line 69
  MovieClip.prototype.centered = function(x, y) {
  this._x = x-this._width/2;
  this._y = y-this._height/2;
  };
  // Makes sure that the image fits within (wMax, hMax)
  MovieClip.prototype.resize = function(wMax, hMax) {
  while (this._width>wMax || this._height>hMax) {
  this._xscale = this._yscale -= 1;
  }
  };
  // This line sets max width and max height (wMax, hMax)
  photo.resize(397, 297);
  
  MovieClip.prototype.changePhoto = function(d) {
      // make sure pIndex falls within pArray.length
      this.pIndex = (this.pIndex+d)%this.pArray.length;
      if (this.pIndex<0) {
          this.pIndex += this.pArray.length;
      }
      this.onEnterFrame = fadeOut;
  };
  MovieClip.prototype.fadeOut = function() {
      if (this.photo._alpha>this.fadeSpeed) {
          this.photo._alpha -= this.fadeSpeed;
      } else {
          this.loadPhoto();
      }
  };
  MovieClip.prototype.loadPhoto = function() {
      // specify the movieclip to load images into
      var p = _root.photo;
      //------------------------------------------
      p._alpha = 0;
      p.loadMovie(this.pathToPics+this.pArray[this.pIndex]);
      this.onEnterFrame = loadMeter;
  };
  MovieClip.prototype.loadMeter = function() {
      var i, l, t;
      l = this.photo.getBytesLoaded();
      t = this.photo.getBytesTotal();
      if (t>0 && t == l) {
          this.onEnterFrame = fadeIn;
          // This line sets the (x,y) center of the image on the stage
          this.photo.centered(249, 213);
      } else {
          trace(l/t);
      }
  };
  MovieClip.prototype.fadeIn = function() {
      if (this.photo._alpha<100-this.fadeSpeed) {
          this.photo._alpha += this.fadeSpeed;
      } else {
          this.photo._alpha = 100;
          this.onEnterFrame = null;
      }
  };
  

This should do it - the only thing I (think) I need some help with is changing the script to actually load the FIRST image, when the gallery loads. For some reason beyond my knowledge you have to click the next button for a image to load… What part of the script needs to be changed to make sure that a image is loaded on startup???

** I hope someone will take the time to help develope this gallery tutorial / help file… It would be great if you added som more steps to the quick checklist… and of course fixed the load image on start problem…** :wink:

nice work scotty :smiley: much appreciated. Now i gotta scale the images. any suggestions on that?

i am using a windows 2k3 server with php and it works fine. all of the scripts do, and now scotty is god and got it load first. Now, i fixed the image size php script and it works perfectly for a sub folder.

http://steveandrew.missionsix.net/filearray.php

i dont really think that is what i need though. I need some way of sizing the images into a container without affecting the images themselfs. ive seen it done, and i’m sure i can figure it out. but any suggestions would be greatly appreciated

can someone post the action sctipt in its entirorty

can do bucko.


lv = new LoadVars();
lv.onLoad = function(success) {
if (success) {
pArray = this.imgList.split(", ");
changePhoto(0);
} else {
info.text = "could not load file";
}
};
lv.load("filearray.php"); 
 
 
// variables ------------------------------------------
// put the path to your pics here, include the slashes (ie. "pics/")
// leave it blank if they're in the same directory
this.pathToPics = "images/";
// fill this array with your pics (set from filearray.php)
this.pArray = pArray; 
 
this.fadeSpeed = 20;
this.pIndex = 0;
 
// MovieClip methods ----------------------------------
// d=direction; should 1 or -1 but can be any number
//loads an image automatically when you run animation
loadMovie(this.pathToPics+this.pArray[0], _root.photo);
 
// Center the photo at (x,y) the coordinates are set in line 69
MovieClip.prototype.centered = function(x, y) {
this._x = x-this._width/2;
this._y = y-this._height/2;
};
// Makes sure that the image fits within (wMax, hMax)
MovieClip.prototype.resize = function(wMax, hMax) {
while (this._width>wMax || this._height>hMax) {
this._xscale = this._yscale -= 1;
}
};
// This line sets max width and max height (wMax, hMax)
photo.resize(397, 297);
 
MovieClip.prototype.changePhoto = function(d) {
// make sure pIndex falls within pArray.length
this.pIndex = (this.pIndex+d)%this.pArray.length;
if (this.pIndex<0) {
this.pIndex += this.pArray.length;
}
this.onEnterFrame = fadeOut;
};
MovieClip.prototype.fadeOut = function() {
if (this.photo._alpha>this.fadeSpeed) {
this.photo._alpha -= this.fadeSpeed;
} else {
this.loadPhoto();
}
};
MovieClip.prototype.loadPhoto = function() {
// specify the movieclip to load images into
var p = _root.photo;
//------------------------------------------
p._alpha = 0;
p.loadMovie(this.pathToPics+this.pArray[this.pIndex]);
this.onEnterFrame = loadMeter;
};
MovieClip.prototype.loadMeter = function() {
var i, l, t;
l = this.photo.getBytesLoaded();
t = this.photo.getBytesTotal();
if (t>0 && t == l) {
this.onEnterFrame = fadeIn;
// This line sets the (x,y) center of the image on the stage
this.photo.centered(249, 213);
} else {
trace(l/t);
}
};
MovieClip.prototype.fadeIn = function() {
if (this.photo._alpha<100-this.fadeSpeed) {
this.photo._alpha += this.fadeSpeed;
} else {
this.photo._alpha = 100;
this.onEnterFrame = null;
}
};

i would also like to point out that you tried to scale the image before the image was even generated. which cuased this part of the script to be null


MovieClip.prototype.resize = function(wMax, hMax) {
while (this._width>wMax || this._height>hMax) {
this._xscale = this._yscale -= 1;
}
};
// This line sets max width and max height (wMax, hMax)
photo.resize(397, 297);

i’ve messed around with it, and tried to get it to work. but when i placed it after hte image has loaded, everything is scaled based on the previous scale, so the images get smaller and smaller until it reaches the first image, where the scalling isnt affected anymore becuase every image has been scaled and the final scale is the total of all the scaling.

An attempt:

this.pathToPics = "images/";
this.fadeSpeed = 20;
this.pIndex = d=0;
hMax = 400;
//maximum height is 400
wMax = 300;
//maximum width is 300
MovieClip.prototype.changePhoto = function(d) {
	this.pIndex = (this.pIndex+d)%this.pArray.length;
	if (this.pIndex<0) {
		this.pIndex += this.pArray.length;
	}
	this.onEnterFrame = fadeOut;
};
MovieClip.prototype.fadeOut = function() {
	if (this.photo._alpha>this.fadeSpeed) {
		this.photo._alpha -= this.fadeSpeed;
	} else {
		this.loadPhoto();
	}
};
MovieClip.prototype.loadPhoto = function() {
	this.photo._alpha = 100;
	this.photo.loadMovie(this.pathToPics+this.pArray[this.pIndex]);
	this.onEnterFrame = loadMeter;
};
MovieClip.prototype.loadMeter = function() {
	var i, l, t;
	l = this.photo.getBytesLoaded();
	t = this.photo.getBytesTotal();
	if (t>0 && t == l) {
		this.onEnterFrame = fadeIn;
		//the 249 and 213 are from the old code change them to your needs
		this.photo.centered(249, 213);
	} else {
		//trace(l/t);
	}
};
MovieClip.prototype.centered = function(x, y) {
	if (this._width>=this._height) {
		this._width = wMax;
		this._yscale = this._xscale;
	} else if (this._width<this._height) {
		this._height = hMax;
		this._xscale = this._yscale;
	}
	this._x = x-this._width/2;
	this._y = y-this._height/2;
};
MovieClip.prototype.fadeIn = function() {
	if (this.photo._alpha<100-this.fadeSpeed) {
		this.photo._alpha += this.fadeSpeed;
	} else {
		this.photo._alpha = 100;
		this.onEnterFrame = null;
	}
};
but1.onRelease = function() {
	changePhoto(1);
};
lv = new LoadVars();
lv.onLoad = function(success) {
	if (success) {
		pArray = this.imgList.split(", ");
		changePhoto(0);
	} else {
		//info.text = "could not load file";
	}
};
lv.load("filearray.php");

Give it a try to see if this is what you want:)

scotty(-:

well it works somewhat. The images are scaled which is cool and all, but some of the images are not. Some of the images, ones that are alot smaller than the max, were stretched to fix into the max values. I think im going in way over my head on this one, but i believe it can be done.

i feel special :smiley: i fixed it. Okay, for these lines:


if (this._width>=this._height) {
		this._width = wMax;
		this._yscale = this._xscale;
	} else if (this._width<this._height) {
		this._height = hMax;
		this._xscale = this._yscale;
	}

you were basically saying if with > height then do this, which would be fine, except for one small problem. If an image is smaller than the 300 width, it gets upscaled to 300 width and then looks really distored. Therefore, i changed it to this:


if (this._width>wMax) {
this._width = wMax;
this._yscale = this._xscale;
} else if (h<this._height) {
this._height = hMax;
this._xscale = this._yscale;
}

Now, the images are correctly scaled if they are larger. if they are not, it is not a problem because the images will fit within the container ;).

Good one=)
I knew it only worked properly when the image was larger, but coudn’t come up with the solution, nice work:thumb:

scotty(-:

Ok, I have to admit, I am a bit confused on implementation of the code in this thread.

I have both V1, V2, and V3 of the Reizing Gallery of Scotty’s from the other thread.

If I am reading this thread right, all I need to do is replace the main timeline code of V1 with the code here, add the filearray.php file into the same directory, add a folder called images and put a few jpg pics in there, and then run the swf.

Well, that is what I have done anyway, and when I run the swf, it brings up the preloader box and thats it. It doesn’t show any buttons, thmbnails, etc…

I would really prefer to make it work with the V3 and not V1, but figured I would have to go thru and compare code differences afterwards to get that to happen.

Do I need to leave some of the code from the V1 and insert this into it, rather than replace it all? Is that the problem?

I guess someone needs to help in exactly what I am suppose to do to make this thing work.

Thanks.

I’ve been playing with this script. All seems to work, to avoid any confusion, i’ll post the code i’ve used first. None is edited…

The PHP file:

<?php 
$dir = opendir("images"); 
while ($file = readdir($dir)) { 
if ($file=="." or $file=="..") continue; 
$string.= $file; 
$string.= ", "; 
} 
echo "imgList=".$string; 
?>
this.pathToPics = "images/";
this.fadeSpeed = 20;
this.pIndex = d=0;
hMax = 480;
//maximum height is 400
wMax = 640;
//maximum width is 300
MovieClip.prototype.changePhoto = function(d) {
	this.pIndex = (this.pIndex+d)%this.pArray.length;
	if (this.pIndex<0) {
		this.pIndex += this.pArray.length;
	}
	this.onEnterFrame = fadeOut;
};
MovieClip.prototype.fadeOut = function() {
	if (this.photo._alpha>this.fadeSpeed) {
		this.photo._alpha -= this.fadeSpeed;
	} else {
		this.loadPhoto();
	}
};
MovieClip.prototype.loadPhoto = function() {
	this.photo._alpha = 100;
	this.photo.loadMovie(this.pathToPics+this.pArray[this.pIndex]);
	this.onEnterFrame = loadMeter;
};
MovieClip.prototype.loadMeter = function() {
	var i, l, t;
	l = this.photo.getBytesLoaded();
	t = this.photo.getBytesTotal();
	if (t>0 && t == l) {
		this.onEnterFrame = fadeIn;
		//the 249 and 213 are from the old code change them to your needs
		this.photo.centered(320, 240);
	} else {
		//trace(l/t);
	}
};
MovieClip.prototype.centered = function(x, y) {
	if (this._width>=wMax) {
		this._width = wMax;
		this._yscale = this._xscale;
	} else if (hMax<this._height) {
		this._height = hMax;
		this._xscale = this._yscale;
	}
	this._x = x-this._width/2;
	this._y = y-this._height/2;
};
MovieClip.prototype.fadeIn = function() {
	if (this.photo._alpha<100-this.fadeSpeed) {
		this.photo._alpha += this.fadeSpeed;
	} else {
		this.photo._alpha = 100;
		this.onEnterFrame = null;
	}
};
but1.onRelease = function() {
	changePhoto(1);
};
lv = new LoadVars();
lv.onLoad = function(success) {
	if (success) {
		pArray = this.imgList.split(", ");
		changePhoto(0);
	} else {
		//info.text = "could not load file";
	}
};
lv.load("filearray.php");

Now heres what I’d like to know:

1- In the current script, once it reaches the end of all the images within the folder, and the “next” button is clicked. It shows a blank slide, if the next button is clicked again, you’ll see the very first image all over. Could someone please help me get rid of the blank slide problem?

2- Instead of using buttons, I’d like to run the show (looping) automatically. Could someone please teach me how?

Thanks

Add this to your code

setInterval(this, "changePhoto", 4000, 1);

The ‘4000’ is the time in msecs. I tested the code here and ir didn’t give me a blank slide…

scotty(-:

Could someone be kind enough to post the .FLA that we’re supposed to edit all this ActionScript in ? I don’t know if it’s the original from the Tutorial, V1 or V3 … ???

Thanks!

This thread is based on the photogallery tutorial here on kirupa.
But, and that’s why I gave you the link to this thread, it’s not that hard to implement in the resize gallery:)

scotty(-:

LOL! Says the god of actionscripting…!!
Ok, gonna go try it! :cons:

ooh! one image is good!!! now … how did we make those next/back buttons…
hmmmm

ex: http://www.joel.bz/test/

ooooh!!! I did it!!! I did it!!! I put little buttons of my own!!!
YAY!!! http://www.joel.bz/test/ look! HAHAHAHAHAHAHAHA

i doubt i have been so happy since i learned how to turn on a pc!
:rabbit:

LOL, then you must be very happy;)

scotty(-:

well, i thought i had it, but i still have no idea why my pics aren’t loading. its sad, i need help please. i also dont know much about action scripting so that could be a reason why too…and i have no idea whats wrong with it my code

this.pathToPics = "[http://www.angelfire.com/musicals/distant/images/](http://www.angelfire.com/musicals/distant/images/)";
this.pArray = ["group0.jpg", "group1.jpg", "group2.jpg", "group3.jpg", "adam.jpg", "corey.jpg", "shaun.jpg", "travis.jpg"];
this.fadeSpeed = 20;
this.pIndex = d=0;
loadMovie(this.pathToPics+this.pArray[0], this.photo);
//Center the photo at (x,y) the coordinates are set in line 69 
MovieClip.prototype.centered = function(x, y) {
 this._x = x-this._width/2;
 this._y = y-this._height/2;
};
MovieClip.prototype.changePhoto = function(d) {
 this.pIndex = (this.pIndex+d)%this.pArray.length;
 trace(this.pArray);
 if (this.pIndex<0) {
  this.pIndex += this.pArray.length;
 }
 this.onEnterFrame = fadeOut;
};
MovieClip.prototype.fadeOut = function() {
 if (this.photo._alpha>this.fadeSpeed) {
  this.photo._alpha -= this.fadeSpeed;
 } else {
  this.loadPhoto();
 }
};
MovieClip.prototype.loadPhoto = function() {
 var p = this.photo;
 p._alpha = 0;
 p.loadMovie(this.pathToPics+this.pArray[this.pIndex]);
 this.onEnterFrame = loadMeter;
};
MovieClip.prototype.loadMeter = function() {
 var i, l, t;
 l = this.photo.getBytesLoaded();
 t = this.photo.getBytesTotal();
 if (t>0 && t == l) {
  this.onEnterFrame = fadeIn;
  this.photo.centered (105, 145);
 } else {
  //trace(l/t);
 }
};
MovieClip.prototype.fadeIn = function() {
 if (this.photo._alpha<100-this.fadeSpeed) {
  this.photo._alpha += this.fadeSpeed;
 } else {
  this.photo._alpha = 100;
  this.onEnterFrame = null;
 }
};
but1.onRelease = function() {
 changePhoto(1);
};
lv = new LoadVars();
lv.onLoad = function(success) {
 
 if (success) {
  pArray = this.imgList.split(", ");
  info.text = pArray;
 } else {
  info.text = "could not load file";
 }
};
lv.load("filearray.php");

Why you define/code pArray and load it with the PHP-file?
Are you sure the path is correct?
Are the jpg’s non-progressive?
Does your server support PHP?

Just a few questions;)

scotty(-: