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

Last change on this file since 121 was 121, checked in by scdev, 18 years ago

Q - Added "sc-" to all css class selectors; Finished reworking Upload and ImageThumb? (now with GD support!); More PHP 5 upgrades.

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