* Copyright 2001-2010 Strangecode, LLC * * This file is part of The Strangecode Codebase. * * The Strangecode Codebase is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your option) * any later version. * * The Strangecode Codebase is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * The Strangecode Codebase. If not, see . */ /** * Image.inc.php * * @author Quinn Comendant * @version 1.1 * @since 14 Apr 2006 20:07:29 */ class Image { // Object parameters. var $_params = array(); /** * Constructor. * * @param array $params Set parameters for this object. * @author Quinn Comendant * @version 1.1 * @since 26 Jan 2005 01:54:50 */ function Image($params=array()) { if (!is_array($params)) { trigger_error(sprintf('Parameters not specified properly.', null), E_USER_ERROR); } $defaults = array( // Pattern passed to glob() with $id to match image filenames. 'filename_pattern' => '%s*', // The path to the image source directory. (Ex: /var/www/htdocs/images) 'base_path' => '', // The URL to the image directory. (Ex: /images) 'base_url' => '', // Image to use in the case of a missing image. 'default_image_file' => '', ); $this->_params = array_merge($defaults, $params); } /** * Tests if an image with specified id exists on the file system. * * @access public * @param string $id Unique image identifier. * @return bool Existence of file. * @author Quinn Comendant * @version 1.0 * @since 26 Jan 2005 01:54:50 */ function exists($id) { $src = $this->oSrc($id); $filepath = preg_match('!://!', $src) ? $src : getenv('DOCUMENT_ROOT') . $src; // Use exif_imagetype to check not only file existence but that of a valid image. return false != @exif_imagetype($filepath); } /** * Returns the value from getimagesize(). * * @access public * @param string $id Unique image identifier. * @param int $key Which element from the array returned by getimagesize: * - Index 0 contains the width of the image in pixels. * - Index 1 contains the height. * - Index 2 is the type of the image. * - Index 3 is height="yyy" width="xxx" string. * @return mixed return value of getimagesize. * @author Quinn Comendant * @version 1.0 * @since 26 Jan 2005 01:54:50 */ function size($id, $key) { $src = $this->oSrc($id); $filepath = preg_match('!://!', $src) ? $src : getenv('DOCUMENT_ROOT') . $src; $img_size = @getimagesize($filepath); return isset($img_size[$key]) ? $img_size[$key] : false; } /** * Returns the URL to the source of image specified by id. * * @access public * @param string $id Unique image identifier. * @return string Absolute URL of image. * @author Quinn Comendant * @version 1.1 * @since 26 Jan 2005 01:56:35 */ function oSrc($id) { $file_name = ''; // filename_pattern is a sprintf argument with %s replaced with the image id. if ($file_match = glob(sprintf("%s/{$this->_params['filename_pattern']}", $this->_params['base_path'], $id))) { $file_name = basename(end($file_match)); } else if ('' != $this->_params['default_image_file']) { $file_name = $this->_params['default_image_file']; } return sprintf('%s/%s', $this->_params['base_url'], $file_name); } /** * Returns an HTML tag with the src set to an image specified by id. * Automatically prints image width and height. * * @access public * @param string $id Unique image identifier. * @param string $alt Text for image alt attribute. * @param string $extra Additional tag attributes. * @return HTML image tag. * @author Quinn Comendant * @version 1.0 * @since 26 Jan 2005 01:57:33 */ function oImg($id, $alt='', $extra='') { $src = $this->oSrc($id); $filepath = preg_match('!://!', $src) ? $src : getenv('DOCUMENT_ROOT') . $src; if (false === ($gis = @getimagesize($filepath)) || preg_match('/width|height/', $extra)) { $image_size = ''; } else { $image_size = $gis[3]; } return sprintf('%s', $src, $image_size, oTxt($alt), $extra ); } } // End class ?>