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-2012 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 | */ |
---|
30 | class 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 | // The return value is the same value that getimagesize() returns in index 2 but exif_imagetype() is much faster. |
---|
80 | return false != @exif_imagetype($filepath); |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | * Returns the value from getimagesize(). |
---|
85 | * |
---|
86 | * @access public |
---|
87 | * @param string $id Unique image identifier. |
---|
88 | * @param int $key Which element from the array returned by getimagesize: |
---|
89 | * - Index 0 contains the width of the image in pixels. |
---|
90 | * - Index 1 contains the height. |
---|
91 | * - Index 2 is the type of the image. |
---|
92 | * - Index 3 is height="yyy" width="xxx" string. |
---|
93 | * @return mixed return value of getimagesize. |
---|
94 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
95 | * @version 1.0 |
---|
96 | * @since 26 Jan 2005 01:54:50 |
---|
97 | */ |
---|
98 | function size($id, $key) |
---|
99 | { |
---|
100 | $src = $this->oSrc($id); |
---|
101 | $filepath = preg_match('!://!', $src) ? $src : getenv('DOCUMENT_ROOT') . $src; |
---|
102 | $img_size = @getimagesize($filepath); |
---|
103 | return isset($img_size[$key]) ? $img_size[$key] : false; |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * Returns the URL to the source of image specified by id. |
---|
108 | * |
---|
109 | * @access public |
---|
110 | * @param string $id Unique image identifier. |
---|
111 | * @return string Absolute URL of image. |
---|
112 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
113 | * @version 1.1 |
---|
114 | * @since 26 Jan 2005 01:56:35 |
---|
115 | */ |
---|
116 | function oSrc($id) |
---|
117 | { |
---|
118 | $file_name = ''; |
---|
119 | // filename_pattern is a sprintf argument with %s replaced with the image id. |
---|
120 | if ($file_match = glob(sprintf("%s/{$this->_params['filename_pattern']}", $this->_params['base_path'], $id))) { |
---|
121 | $file_name = basename(end($file_match)); |
---|
122 | } else if ('' != $this->_params['default_image_file']) { |
---|
123 | $file_name = $this->_params['default_image_file']; |
---|
124 | } |
---|
125 | return sprintf('%s/%s', $this->_params['base_url'], $file_name); |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * Returns an HTML <img> tag with the src set to an image specified by id. |
---|
130 | * Automatically prints image width and height. |
---|
131 | * |
---|
132 | * @access public |
---|
133 | * @param string $id Unique image identifier. |
---|
134 | * @param string $alt Text for image alt attribute. |
---|
135 | * @param string $extra Additional tag attributes. |
---|
136 | * @return HTML image tag. |
---|
137 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
138 | * @version 1.0 |
---|
139 | * @since 26 Jan 2005 01:57:33 |
---|
140 | */ |
---|
141 | function oImg($id, $alt='', $extra='') |
---|
142 | { |
---|
143 | $src = $this->oSrc($id); |
---|
144 | $filepath = preg_match('!://!', $src) ? $src : getenv('DOCUMENT_ROOT') . $src; |
---|
145 | |
---|
146 | if (false === ($gis = @getimagesize($filepath)) || preg_match('/width|height/', $extra)) { |
---|
147 | $image_size = ''; |
---|
148 | } else { |
---|
149 | $image_size = $gis[3]; |
---|
150 | } |
---|
151 | |
---|
152 | return sprintf('<img src="%s" %s alt="%s" %s />', |
---|
153 | $src, |
---|
154 | $image_size, |
---|
155 | oTxt($alt), |
---|
156 | $extra |
---|
157 | ); |
---|
158 | } |
---|
159 | } // End class |
---|
160 | |
---|
161 | ?> |
---|