Skip to main content
Participating Frequently
August 1, 2009
Question

Photo gallery with buttons and php.

  • August 1, 2009
  • 1 reply
  • 560 views

I am relatively new to actions script, very new to as3, and brand new to php. I have tried very hard to figure all of this out via tutorials, studying the code, etc but now I'm running out of time to get this done so I need some help.

I am trying to make a photo gallery that functions as follows:

-the php gathers all of the filenames from a folder and returns it to flash

-an array of buttons is loaded onto the stage (one for each photo)

-flash then loads the first image into a movie clip.

-when the user clicks on button, image loads in the movie clip.

-i also plan to incorporate what might happen if the number of photos exceeds the number of buttons that can fit nicely on the stage, like an arrow to go to another set of buttons. I haven't tried coding this yet since I was just trying to get the thing to work in general first. If anyone has ideas about this, let me know.

Please don't make fun of me too much. I know it's probably a mess.

Thank you so much in advance for your help.

Here's the php:

<!--

Author:      Adam Ehrheart

Site:      http://adamehrheart.com

Blog:      http://flashcamp.net

Date:      4.21.08

-->

<?php

#   Use "." if the get_files.php file resides in the same directory as files being read

#   Otherwise you can change the path to whatever you like

#   eg:

#   Same Directory:

#   $path = ".";

#

#   Other Directory

#   $path = "products/images/"

$path = "Photography/";

#   Choosing what directory to read

if ($handle = opendir($path)) {

  

   #   Temporary array to hold image files

   $imageFiles = array();

  

   #   Creating loop and assigning current file to $file temp variable

   while (false !== ($file = readdir($handle)))

   {

      #   Checking wheter or not the file is invisible and starts with a "."

      $fileCheck = substr($file, 0, 1);

      #   Checking to make sure the files is either a (jpg, JPG, png, PNG)

      $fileType = substr($file, -3);

     

      #   Making sure file is not invisible

      if($fileCheck != ".")

      {

        

         #   Making sure file is readable and dynamically loadable by Flash

         if($fileType == "jpg" || $fileType == "JPG" || $fileType == "png" || $fileType == "PNG")

         {

            #   Adding File to the image array

            if($path != "."){

            array_push($imageFiles, $path . $file);

            }else{

               array_push($imageFiles, $file);

            }

     

         }

      #   Sorting the files alphabetically

      sort($imageFiles);

      }

   }

   #    Creating XML File output to be read by Flash

   echo "<?xml version=\"1.0\"?>\n";

  

   #   Root Node

   echo "<image_list>";

  

   #   Creating child nodes for each image

   foreach($imageFiles as $value)

   {

      #   Pulling the Width and Height values for each file and adding them as attributes for the image node

      list($width, $height) = getimagesize($value);

      #   Creating the image node

      echo "<image width=\"$width\"" . " height=\"$height\">" . $value . "</image>";  

   }

   echo "</image_list>";

  

   #   Closing the readdir function

   closedir($handle);

}

And here's the as3:

//php photo section

import flash.events.Event;

import flash.net.*;

//load the php file

var myRequest:URLRequest = new URLRequest("Photography.php");

var myLoader:URLLoader = new URLLoader();

//define images variable as an xml file

var images:XML = new XML();

images.ignoreWhite = true;

images.addEventListener ('load', myLoader);

//define the images variable as an xml as the php file result

myRequest.data = images;

//outputting the filenames

function onLoaded(evt:Event):void {

  trace("here we get the data back: "+myLoader.data);

}

//when the data is loaded, begin myRequest

myLoader.addEventListener(Event.COMPLETE, onLoaded);

myLoader.load(myRequest);

//array to call the images

var imageArray:Array //= NewArray();

var listLength:Number;

var il:XMLList = images.data  //xml.images;

listLength=il.length();

var i:Number

var photo_btn:Array = new Array();

for (i = 0; i < listLength; i++); {

imageArray = il.pic //xml.images.pic;

{

if (photo_btn.mouseDown == true) {

img_loader.load(imageArray)

  }

}

{

if (i == 0)  {

photo_btn.y = 422.7;

photo_btn.x = 411.5

}

else if (i > 0 && i < 24); {

photo_btn.y = 422.7;

photo_btn.x = (photo_btn[i-1].x + 18.6);

}

{

if (i > 24 && i < listLength); {

photo_btn.y = 442.7;

photo_btn.x = (photo_btn[i-1].x + 18.6);

}

}

}

img_loader.load(imageArray[0]);

}

This topic has been closed for replies.

1 reply

Inspiring
August 1, 2009

As for AS3 part of it, I am not sure your code really works. There are syntax and logical errors there.

I think you need to take it step by step and accomplish several task in the following sequences:

1. Write code that loads XML correctly;

2. Write code that enables buttons;

3. Write code that will load images on button clicks.

The code below shows in principal what needs to be done in order to load XML and make the data in this XML available for further consumption. Also, by accomplishing this step you will iron out all the PHP vs Flash wrinkles including your XML.

Please note, I don't know your XML structure so all the parsing issues you need to resolve yourself.

Once you get handle on it - we, hopefully, will talk about steps 2 and 3.

import flash.display.Loader;
import flash.events.*;
import flash.net.*;

var images:XML;
var myRequest:URLRequest;
var myLoader:URLLoader;
// list of image urls that will come from loaded XML
var imageList:XMLList;

myRequest = new URLRequest("Photography.php");
myLoader = new URLLoader();
myLoader.addEventListener(Event.COMPLETE, onFileLoaded);
// suggested handler for unexpected errors - avoids some headaches
myLoader.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
myLoader.load(myRequest);

// Note: all the listeners are removed
// it is always wise to remove listeners that are needed any longer
// to make objects eligible for arbage collection
function onLoadError(e:IOErrorEvent):void 
{
     trace(e.toString());
     myLoader.removeEventListener(Event.COMPLETE, onFileLoaded);
     myLoader.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError);
}
          
function onFileLoaded(e:Event):void 
{
     myLoader.removeEventListener(Event.COMPLETE, onFileLoaded);
     myLoader.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError);
     images = new XML(myLoader.data);
     // only now xml is ready and you can start loading images
     imageList= images.pic;
}
Participating Frequently
August 1, 2009

Thank you very very very much for the reply. I've been working on this for awhile and getting more and more frustrated the more I try. I am going to attempt to start over with the code. I'll study the code you gave me and try to take it in the steps you suggested. I'll probably need additional help, but I'll try to give it my best attempt and then I'll ask for more help as needed.

Thanks a lot.

Inspiring
August 1, 2009

You are welcome.