gallery.php

<?php if (! defined ('BASEPATH')) die('No direct script access allowed.');

class Gallery extends Controller {
	
	function Gallery() {
		parent::Controller();
		// Set copyright year
		// * Change this to the year you want the copyright date to start on
		$this->year = '2007';
		// Location of the galleries
		// * This location should be outside the web-accessible document root
		$basepath = '/home/example.com/private/showcase';
		// Set the file locations
		// * The gallery directory should contain two directories:
		// - fullsize
		// - thumbnail
		// * The dropbox is a FTP location to drop new files, but the functions
		// * to process those files are not included with this example.
		// * (Read the documentation for Image_lib!)
		$this->gallery = "$basepath/gallery";
		$this->dropbox = "$basepath/dropbox";
		// Allowed image types
		// * Array for comparing the return of exif_imagetype() to the extension
		$this->allowed = array(
			IMAGETYPE_GIF     => 'gif',
			IMAGETYPE_JPEG    => 'jpg',
			IMAGETYPE_PNG     => 'png',
			IMAGETYPE_TIFF_II => 'tiff',
			IMAGETYPE_TIFF_MM => 'tiff',
			IMAGETYPE_BMP     => 'bmp');
		// Load required resources
		// * Most of these are non-standard Models and Helpers, and are unrelated
		$this->load->helper ('media');
		$this->load->helper ('typography');
		$this->load->model  ('gallerydb');
	}
	
	function index() {
		$year = date ('Y');
		$year = $this->year != $year ? $this->year .'-'. $year : $this->year;
		// Create a copyright date
		// * This sets a year that you can use in your template to show the
		// * copyright date. It will be in the format YYYY[-YYYY]
		$view['copydate'] = $year;
		$view['images']   = $this->gallerydb->list_all (false, true);
		
		$this->load->view ('template', $view);
	}
	
	function show ($type, $id) {
		// Select the type of image we are fetching
		// * The following URL would select the fullsize image with the id 34:
		// - http://example.com/gallery/show/fullsize/34
		$type  = ($type == 'thumbnail') ? 'thumbnail' : 'fullsize';
		// Select the directory to fetch from, based on the type
		// * The this would read the following file:
		// - /home/example.com/private/showcase
		$image = "$this->gallery/$type/$id.jpg";
		
		// Check to make sure we are working with an allowed image type
		// * Note that I convert all of my images to JPG (as seen above),
		// * so this step is entirely unnecessary, and is only here for
		// * documentation purposes
		$mime  = @exif_imagetype($image);
		if (! isset ($this->allowed[$mime])) {
			$image = "./media/img/gallery_{$type}_na.jpg";
			$mime  = @exif_imagetype ($image);
		}
		// Get the mime type for Content-Type
		$mime = image_type_to_mime_type ($mime);
		// Get the file size for Content-Length
		$size = @filesize ($image);
		// Get the file mod time for Last-Modifed
		$modf = @filemtime ($image);
		$mgmt = gmdate ('D, d M Y H:i:s', $modf);
		// Set the expiration time for Expires
		$egmt = gmdate ('D, d M Y H:i:s', (time()+7200));
		
		// Set the headers
		header ("Cache-Control: public, must-revalidate");
		header ("Last-Modified: $mgmt GMT");
		header ("Expires: $egmt GMT");
		header ("Content-Type: $mime");
		header ("Content-Length: $size");
		
		// Clear the buffer, to make sure we have clean output
		ob_end_clean();
		// Open the image in read-only, binary mode
		if ($file = fopen ($image, 'rb')) {
			// Prevent the user from aborting the script
			// * Don't worry, we handle aborting in the output loop.
			// * This is necessary to make sure that the file is closed properly
			ignore_user_abort(true);
			// Read the file
			while (! feof ($file)) {
				// Read the output in 32 byte chunks
				// * It is recommended that you not lower this, benchmarks
				// * have proven that using anything less than a 32 byte
				// * will work poorly with IE (w00t!) and don't allow full
				// * saturation of the bandwidth.
				print (fread ($file, 1024*32));
				// Good connection status, output data chunk
				// * Remember that ob_flush() and flush() must ALWAYS be used
				if (connection_status() == 0) { ob_flush(); flush(); }
				// User has closed the connection, close the file and stop
				else { fclose($file); return; }
			}
			// Finished reading, close the file
			fclose($file);
		}
		// Stop
		return;
	}
	
}

?>