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

Last change on this file since 334 was 334, checked in by quinn, 16 years ago

Fixed lots of misplings. I'm so embarrassed! ;P

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