Location: PHPKode > scripts > Snaps Album > snaps-album/SnapsAlbum.php
<?php

// Photo album class, that creates a photo album object from a folder of jpg images.

class SnapsAlbum {
	private $path_to_folder = "photos/";
	private $Folder;
	private $title;
	private $snaps_images;	
	
	function __construct($snapsFolder, $snaps_path) {
		$this->path_to_folder = $snaps_path;
		$this->Folder = $snapsFolder;
		$this->title = $this->Folder;
		
		// Load the images within the folder into an multi-dimensional array, containing "filename, width, height";
		$this->snaps_loadimages();
		
		// check to see if the thumbnails have been created for all the photos. If they haven't create them.
		$dir = $this->path_to_folder . $this->Folder;
		if(count(glob($dir."/thumbnails")) > 0) {
			// Do not create the thumbnails, unless there isn't enough thumbnails within the thumbnails folder.
			if (count(glob($dir . '/thumbnails/*.jp{g,eg}', GLOB_BRACE)) < $this->snaps_totalNum()) {
				$this->snaps_thumbnails();
			}
		} else {
			$this->snaps_thumbnails();
		}	
			
	}
	
	private function snaps_makethumb($width, $height, $image, $originalx, $originaly, $filename) { // resize original image.
		$new_image = imagecreatetruecolor($width, $height);
      	imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $originalx, $originaly);
		$save_to = $this->path_to_folder . $this->Folder . '/thumbnails/' . $filename;
		imagejpeg($new_image, $save_to, 50);
	}

	private function snaps_thumbnails() {
		// resize every image in the folder to fit within 125px square, create a thumbnails folder and place the images within it.
		if (!file_exists($this->path_to_folder . $this->Folder . '/thumbnails')) {
			mkdir($this->path_to_folder . $this->Folder . '/thumbnails', 0755);
		}
		
		foreach ($this->snaps_images as $key => $sizes) {
			$filename = $key;
			$image = imagecreatefromjpeg($this->path_to_folder . $this->Folder . '/' .$filename);
			if (is_array($sizes)) {
				$width = $sizes['width'];
				$height = $sizes['height'];
				
				if ($height < $width) {
					$resize_ratio = 125 / $width;
					$newHeight = $height * $resize_ratio;
					$newWidth = 125;
				} else {
					$resize_ratio = 125 / $height;
					$newWidth = $width * $resize_ratio;
					$newHeight = 125;
				}
				$this->snaps_makethumb($newWidth, $newHeight, $image, $width, $height, $filename);
			}
		}
		
	}
	
	private function snaps_loadimages() {
		$directory = $this->path_to_folder . $this->Folder;
		$d = dir($directory) or die("Wrong path: $directory");
		while (false !== ($entry = $d->read())) {
			if ($entry != '.' && $entry != '..' && !is_dir($dir.$entry)) {
				if (preg_match("/(.*\.jpe?g|.*\.JPE?G)/", $entry)) {
					$Images[] = $entry;
				}
			}
		}
		$d->close();
		
		foreach ($Images as $img) {
		// getimagesize outputs an array of image info, INDEX [0]-width [1]-height [2]-IMAGETYPE_XXX [3]-string 'height="yyy" width="xxx"'		
			$sizes = getimagesize($directory. '/' .$img); 				
			$this->snaps_images[$img] = array('width' => $sizes[0],
										  'height' => $sizes[1],
										  'dimensions' => $sizes[3]);
		}
	}
	
	// Thanks to michael from http://uk3.php.net/key for this function.
	private function KeyName(array $a, $pos) { // KeyName($a, -1) will return the last key
		$temp = array_slice($a, $pos, 1, true);
    	return key($temp);
	}
	
	function get_snapsTitle() { // returns the album title.
		return $this->title;
	}
	
	function set_snapsTitle($new_title) { // sets a new title for this album.
		$this->title = $new_title;
	}
	
	function get_snapsFolder() { // returns the directory name of this album's folder.
		return $this->Folder;
	}
	
	function get_snaps_images() { // returns an array of images in the following format. 'image_filename' => array('width' => 'XXX;, 'height' => 'YYY', 'dimensions' => 'height="yyy" width="xxx"');
		return $this->snaps_images;
	}
	
	function get_snaps_photo_dimensions($imgNum) { 
		$req_photo = $this->get_snaps_photo_filename($imgNum);
		$dimensions = $this->snaps_images[$req_photo]['dimensions'];
		return $dimensions;
	}
	
	function get_snaps_photo_filename($imgNum) { 
	// by giving this function a photos position within the snaps_images array, it returns the photos filename.
		$curr_filename = $this->KeyName($this->snaps_images, $imgNum);
		return $curr_filename;
	}
	
	function snaps_totalNum() {
	// how many photos are in this folder, not including the thumbnails.
		$dir = $this->path_to_folder . $this->Folder;		
    	return count(glob($dir."/*.jp{g,eg}", GLOB_BRACE)); 
	}
	
	function snaps_getCover() { // returns the filename of a random image from this album.
		if (is_array($this->snaps_images)) {
			$num_images = sizeof($this->snaps_images);
			$rand_cover = rand(0, ($num_images - 1));
		}
		$filename = $this->KeyName($this->snaps_images, $rand_cover);
		
		return $filename;		
	}

}
?>
Return current item: Snaps Album