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

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

New class to manage printing of images. Took source code from class used on tilecity.com

File size: 4.5 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        return false != @exif_imagetype($filepath);
58    }
59   
60    /**
61     * Returns the value from getimagesize().
62     *
63     * @access  public
64     * @param   string  $id     Unique image identifier.
65     * @param   int     $key    Which element from the array returned by getimagesize:
66     *                   - Index 0 contains the width of the image in pixels.
67     *                   - Index 1 contains the height.
68     *                   - Index 2 is the type of the image.
69     *                   - Index 3 is height="yyy" width="xxx" string.
70     * @return  mixed   return value of getimagesize.
71     * @author  Quinn Comendant <quinn@strangecode.com>
72     * @version 1.0
73     * @since   26 Jan 2005 01:54:50
74     */
75    function size($id, $key)
76    {
77        $src = $this->oSrc($id);
78        $filepath = preg_match('!://!', $src) ? $src : getenv('DOCUMENT_ROOT') . $src;
79        $img_size = @getimagesize($filepath);
80        return isset($img_size[$key]) ? $img_size[$key] : false;
81    }
82   
83    /**
84     * Returns the URL to the source of image specified by id.
85     *
86     * @access  public
87     * @param   string  $id     Unique image identifier.
88     * @return  string  Absolute URL of image.
89     * @author  Quinn Comendant <quinn@strangecode.com>
90     * @version 1.1
91     * @since   26 Jan 2005 01:56:35
92     */
93    function oSrc($id)
94    {
95        $file_name = '';
96        if ($file_match = glob(sprintf("%s/{$this->_params['filename_pattern']}", $this->_params['base_path'], $id))) {
97            $file_name = basename(end($file_match));
98        } else if ('' != $this->_params['default_image_file']) {
99            $file_name = $this->_params['default_image_file'];
100        }
101        return sprintf('%s/%s', $this->_params['base_url'], $file_name);
102    }
103
104    /**
105     * Returns an HTML <img> tag with the src set to an image specified by id. 
106     * Automatically prints image width and height.
107     *
108     * @access  public
109     * @param   string  $id     Unique image identifier.
110     * @param   string  $alt    Text for image alt attribute.
111     * @param   string  $extra  Additional tag attributes.
112     * @return  HTML image tag.
113     * @author  Quinn Comendant <quinn@strangecode.com>
114     * @version 1.0
115     * @since   26 Jan 2005 01:57:33
116     */
117    function oImg($id, $alt='', $extra='')
118    {
119        $src = $this->oSrc($id);
120        $filepath = preg_match('!://!', $src) ? $src : getenv('DOCUMENT_ROOT') . $src;
121       
122        if (false === ($gis = @getimagesize($filepath)) || preg_match('/width|height/', $extra)) {
123            $image_size = '';
124        } else {
125            $image_size = $gis[3];
126        }
127       
128        return sprintf('<img src="%s" %s alt="%s" %s />',
129            $src,
130            $image_size,
131            oTxt($alt),
132            $extra
133        );
134    }
135} // End class
136
137?>
Note: See TracBrowser for help on using the repository browser.