source: trunk/lib/Image.inc.php @ 751

Last change on this file since 751 was 502, checked in by anonymous, 9 years ago

Many minor fixes during pulso development

File size: 5.6 KB
RevLine 
[95]1<?php
2/**
[362]3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
[396]5 * Copyright 2001-2012 Strangecode, LLC
[452]6 *
[362]7 * This file is part of The Strangecode Codebase.
8 *
9 * The Strangecode Codebase is free software: you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your option)
12 * any later version.
[452]13 *
[362]14 * The Strangecode Codebase is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * details.
[452]18 *
[362]19 * You should have received a copy of the GNU General Public License along with
20 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/**
[95]24 * Image.inc.php
[136]25 *
[95]26 * @author  Quinn Comendant <quinn@strangecode.com>
27 * @version 1.1
28 * @since   14 Apr 2006 20:07:29
29 */
[502]30class Image
31{
[95]32
33    // Object parameters.
[484]34    protected $_params = array();
[95]35
36    /**
37     * Constructor.
38     *
39     * @param   array    $params  Set parameters for this object.
40     * @author  Quinn Comendant <quinn@strangecode.com>
41     * @version 1.1
42     * @since   26 Jan 2005 01:54:50
43     */
[468]44    public function __construct($params=array())
[95]45    {
46        if (!is_array($params)) {
[479]47            trigger_error(sprintf('Parameters not specified properly.', null), E_USER_ERROR);
[95]48        }
49        $defaults = array(
50            // Pattern passed to glob() with $id to match image filenames.
51            'filename_pattern' => '%s*',
[452]52
[95]53            // The path to the image source directory. (Ex: /var/www/htdocs/images)
54            'base_path' => '',
[452]55
[95]56            // The URL to the image directory. (Ex: /images)
57            'base_url' => '',
[452]58
[95]59            // Image to use in the case of a missing image.
60            'default_image_file' => '',
61        );
62        $this->_params = array_merge($defaults, $params);
63    }
[452]64
[95]65    /**
66     * Tests if an image with specified id exists on the file system.
67     *
68     * @access  public
69     * @param   string  $id     Unique image identifier.
70     * @return  bool            Existence of file.
71     * @author  Quinn Comendant <quinn@strangecode.com>
72     * @version 1.0
73     * @since   26 Jan 2005 01:54:50
74      */
[468]75    public function exists($id)
[95]76    {
77        $src = $this->oSrc($id);
78        $filepath = preg_match('!://!', $src) ? $src : getenv('DOCUMENT_ROOT') . $src;
[334]79        // Use exif_imagetype to check not only file existence but that of a valid image.
[452]80        // The return value is the same value that getimagesize() returns in index 2 but exif_imagetype() is much faster.
[95]81        return false != @exif_imagetype($filepath);
82    }
[452]83
[95]84    /**
85     * Returns the value from getimagesize().
86     *
87     * @access  public
88     * @param   string  $id     Unique image identifier.
[452]89     * @param   int     $key    Which element from the array returned by getimagesize:
90     *                   - Index 0 contains the width of the image in pixels.
91     *                   - Index 1 contains the height.
92     *                   - Index 2 is the type of the image.
[95]93     *                   - Index 3 is height="yyy" width="xxx" string.
94     * @return  mixed   return value of getimagesize.
95     * @author  Quinn Comendant <quinn@strangecode.com>
96     * @version 1.0
97     * @since   26 Jan 2005 01:54:50
98     */
[468]99    public function size($id, $key)
[95]100    {
101        $src = $this->oSrc($id);
102        $filepath = preg_match('!://!', $src) ? $src : getenv('DOCUMENT_ROOT') . $src;
103        $img_size = @getimagesize($filepath);
104        return isset($img_size[$key]) ? $img_size[$key] : false;
105    }
[452]106
[95]107    /**
108     * Returns the URL to the source of image specified by id.
109     *
110     * @access  public
111     * @param   string  $id     Unique image identifier.
112     * @return  string  Absolute URL of image.
113     * @author  Quinn Comendant <quinn@strangecode.com>
114     * @version 1.1
115     * @since   26 Jan 2005 01:56:35
116     */
[468]117    public function oSrc($id)
[95]118    {
119        $file_name = '';
[121]120        // filename_pattern is a sprintf argument with %s replaced with the image id.
[95]121        if ($file_match = glob(sprintf("%s/{$this->_params['filename_pattern']}", $this->_params['base_path'], $id))) {
122            $file_name = basename(end($file_match));
123        } else if ('' != $this->_params['default_image_file']) {
124            $file_name = $this->_params['default_image_file'];
125        }
126        return sprintf('%s/%s', $this->_params['base_url'], $file_name);
127    }
128
129    /**
[452]130     * Returns an HTML <img> tag with the src set to an image specified by id.
[95]131     * Automatically prints image width and height.
132     *
133     * @access  public
134     * @param   string  $id     Unique image identifier.
135     * @param   string  $alt    Text for image alt attribute.
136     * @param   string  $extra  Additional tag attributes.
137     * @return  HTML image tag.
138     * @author  Quinn Comendant <quinn@strangecode.com>
139     * @version 1.0
140     * @since   26 Jan 2005 01:57:33
141     */
[468]142    public function oImg($id, $alt='', $extra='')
[95]143    {
144        $src = $this->oSrc($id);
145        $filepath = preg_match('!://!', $src) ? $src : getenv('DOCUMENT_ROOT') . $src;
[452]146
[95]147        if (false === ($gis = @getimagesize($filepath)) || preg_match('/width|height/', $extra)) {
148            $image_size = '';
149        } else {
150            $image_size = $gis[3];
151        }
[452]152
[95]153        return sprintf('<img src="%s" %s alt="%s" %s />',
154            $src,
155            $image_size,
156            oTxt($alt),
157            $extra
158        );
159    }
160} // End class
Note: See TracBrowser for help on using the repository browser.