source: tags/2.1.5/lib/Image.inc.php

Last change on this file was 377, checked in by quinn, 14 years ago

Releasing trunk as stable version 2.1.5

File size: 5.5 KB
Line 
1<?php
2/**
3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
5 * Copyright 2001-2010 Strangecode, LLC
6 *
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.
13 *
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.
18 *
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/**
24 * Image.inc.php
25 *
26 * @author  Quinn Comendant <quinn@strangecode.com>
27 * @version 1.1
28 * @since   14 Apr 2006 20:07:29
29 */
30class Image {
31
32    // Object parameters.
33    var $_params = array();
34
35    /**
36     * Constructor.
37     *
38     * @param   array    $params  Set parameters for this object.
39     * @author  Quinn Comendant <quinn@strangecode.com>
40     * @version 1.1
41     * @since   26 Jan 2005 01:54:50
42     */
43    function Image($params=array())
44    {
45        if (!is_array($params)) {
46            trigger_error(sprintf('Parameters not specified properly.', null), E_USER_ERROR);
47        }
48        $defaults = array(
49            // Pattern passed to glob() with $id to match image filenames.
50            'filename_pattern' => '%s*',
51           
52            // The path to the image source directory. (Ex: /var/www/htdocs/images)
53            'base_path' => '',
54           
55            // The URL to the image directory. (Ex: /images)
56            'base_url' => '',
57           
58            // Image to use in the case of a missing image.
59            'default_image_file' => '',
60        );
61        $this->_params = array_merge($defaults, $params);
62    }
63   
64    /**
65     * Tests if an image with specified id exists on the file system.
66     *
67     * @access  public
68     * @param   string  $id     Unique image identifier.
69     * @return  bool            Existence of file.
70     * @author  Quinn Comendant <quinn@strangecode.com>
71     * @version 1.0
72     * @since   26 Jan 2005 01:54:50
73      */
74    function exists($id)
75    {
76        $src = $this->oSrc($id);
77        $filepath = preg_match('!://!', $src) ? $src : getenv('DOCUMENT_ROOT') . $src;
78        // Use exif_imagetype to check not only file existence but that of a valid image.
79        return false != @exif_imagetype($filepath);
80    }
81   
82    /**
83     * Returns the value from getimagesize().
84     *
85     * @access  public
86     * @param   string  $id     Unique image identifier.
87     * @param   int     $key    Which element from the array returned by getimagesize:
88     *                   - Index 0 contains the width of the image in pixels.
89     *                   - Index 1 contains the height.
90     *                   - Index 2 is the type of the image.
91     *                   - Index 3 is height="yyy" width="xxx" string.
92     * @return  mixed   return value of getimagesize.
93     * @author  Quinn Comendant <quinn@strangecode.com>
94     * @version 1.0
95     * @since   26 Jan 2005 01:54:50
96     */
97    function size($id, $key)
98    {
99        $src = $this->oSrc($id);
100        $filepath = preg_match('!://!', $src) ? $src : getenv('DOCUMENT_ROOT') . $src;
101        $img_size = @getimagesize($filepath);
102        return isset($img_size[$key]) ? $img_size[$key] : false;
103    }
104   
105    /**
106     * Returns the URL to the source of image specified by id.
107     *
108     * @access  public
109     * @param   string  $id     Unique image identifier.
110     * @return  string  Absolute URL of image.
111     * @author  Quinn Comendant <quinn@strangecode.com>
112     * @version 1.1
113     * @since   26 Jan 2005 01:56:35
114     */
115    function oSrc($id)
116    {
117        $file_name = '';
118        // filename_pattern is a sprintf argument with %s replaced with the image id.
119        if ($file_match = glob(sprintf("%s/{$this->_params['filename_pattern']}", $this->_params['base_path'], $id))) {
120            $file_name = basename(end($file_match));
121        } else if ('' != $this->_params['default_image_file']) {
122            $file_name = $this->_params['default_image_file'];
123        }
124        return sprintf('%s/%s', $this->_params['base_url'], $file_name);
125    }
126
127    /**
128     * Returns an HTML <img> tag with the src set to an image specified by id. 
129     * Automatically prints image width and height.
130     *
131     * @access  public
132     * @param   string  $id     Unique image identifier.
133     * @param   string  $alt    Text for image alt attribute.
134     * @param   string  $extra  Additional tag attributes.
135     * @return  HTML image tag.
136     * @author  Quinn Comendant <quinn@strangecode.com>
137     * @version 1.0
138     * @since   26 Jan 2005 01:57:33
139     */
140    function oImg($id, $alt='', $extra='')
141    {
142        $src = $this->oSrc($id);
143        $filepath = preg_match('!://!', $src) ? $src : getenv('DOCUMENT_ROOT') . $src;
144       
145        if (false === ($gis = @getimagesize($filepath)) || preg_match('/width|height/', $extra)) {
146            $image_size = '';
147        } else {
148            $image_size = $gis[3];
149        }
150       
151        return sprintf('<img src="%s" %s alt="%s" %s />',
152            $src,
153            $image_size,
154            oTxt($alt),
155            $extra
156        );
157    }
158} // End class
159
160?>
Note: See TracBrowser for help on using the repository browser.