comparison e2gallerypro/e2upload/Backend/Image.php @ 3:3f6b44aa6b35 judyates

[svn r4] added ability to buy stuff, from a Prints page, but it doesn't work well with the css, and it also has not been fitted into the perl make system.
author rlm
date Mon, 22 Feb 2010 08:02:39 -0500
parents
children
comparison
equal deleted inserted replaced
2:670229c4eb4b 3:3f6b44aa6b35
1 <?php
2 /**
3 * Styx::Image - Provides an Interface to the GD-Library for image manipulation
4 *
5 * @package Styx
6 * @subpackage Utility
7 *
8 * @license MIT-style License
9 * @author Christoph Pojer <christoph.pojer@gmail.com>
10 *
11 * @link http://www.bin-co.com/php/scripts/classes/gd_image/ Based on work by "Binny V A"
12 */
13
14 class Image {
15 /**
16 * The path to the image file
17 *
18 * @var string
19 */
20 private $file;
21 /**
22 * The image resource
23 *
24 * @var resource
25 */
26 private $image;
27 /**
28 * Metadata regarding the image
29 *
30 * @var array
31 */
32 private $meta;
33
34 /**
35 * @param string $file The path to the image file
36 */
37 public function __construct($file){
38 $file = realpath($file);
39 if(!file_exists($file))
40 return;
41
42 $this->file = $file;
43 $img = getimagesize($file);
44
45 $this->meta = array(
46 'width' => $img[0],
47 'height' => $img[1],
48 'mime' => $img['mime'],
49 'ext' => end(explode('/', $img['mime'])),
50 );
51 if($this->meta['ext']=='jpg')
52 $this->meta['ext'] = 'jpeg';
53
54 if(!in_array($this->meta['ext'], array('gif', 'png', 'jpeg')))
55 return;
56
57 if(in_array($this->meta['ext'], array('gif', 'png'))){
58 $this->image = $this->create();
59
60 $fn = 'imagecreatefrom'.$this->meta['ext'];
61 $original = $fn($file);
62 imagecopyresampled($this->image, $original, 0, 0, 0, 0, $this->meta['width'], $this->meta['height'], $this->meta['width'], $this->meta['height']);
63 }else{
64 $this->image = imagecreatefromjpeg($file);
65 }
66 }
67
68 public function __destruct(){
69 if(!empty($this->image)) imagedestroy($this->image);
70 }
71
72 /**
73 * Returns the size of the image
74 *
75 * @return array
76 */
77 public function getSize(){
78 return array(
79 'width' => $this->meta['width'],
80 'height' => $this->meta['height'],
81 );
82 }
83
84 /**
85 * Creates a new, empty image with the desired size
86 *
87 * @param int $x
88 * @param int $y
89 * @param string $ext
90 * @return resource
91 */
92 private function create($x = null, $y = null, $ext = null){
93 if(!$x) $x = $this->meta['width'];
94 if(!$y) $y = $this->meta['height'];
95
96 $image = imagecreatetruecolor($x, $y);
97 if(!$ext) $ext = $this->meta['ext'];
98 if($ext=='png'){
99 imagealphablending($image, false);
100 imagefilledrectangle($image, 0, 0, $x, $y, imagecolorallocatealpha($image, 0, 0, 0, 127));
101 }
102
103 return $image;
104 }
105
106 /**
107 * Replaces the image resource with the given parameter
108 *
109 * @param resource $new
110 */
111 private function set($new){
112 imagedestroy($this->image);
113 $this->image = $new;
114
115 $this->meta['width'] = imagesx($this->image);
116 $this->meta['height'] = imagesy($this->image);
117 }
118
119 /**
120 * Returns the path to the image file
121 *
122 * @return string
123 */
124 public function getPathname(){
125 return $this->file;
126 }
127
128 /**
129 * Rotates the image by the given angle
130 *
131 * @param int $angle
132 * @param array $bgcolor An indexed array with red/green/blue/alpha values
133 * @return Image
134 */
135 public function rotate($angle, $bgcolor = null){
136 if(empty($this->image) || !$angle || $angle>=360) return $this;
137
138 $this->set(imagerotate($this->image, $angle, is_array($bgcolor) ? imagecolorallocatealpha($this->image, $bgcolor[0], $bgcolor[1], $bgcolor[2], !empty($bgcolor[3]) ? $bgcolor[3] : null) : $bgcolor));
139
140 return $this;
141 }
142
143 /**
144 * Resizes the image to the given size, automatically calculates
145 * the new ratio if parameter {@link $ratio} is set to true
146 *
147 * @param int $x
148 * @param int $y
149 * @param bool $ratio
150 * @return Image
151 */
152 public function resize($x = null, $y = null, $ratio = true){
153 if(empty($this->image) || (!$x && !$y)) return $this;
154
155 if(!$y) $y = $ratio ? $this->meta['height']*$x/$this->meta['width'] : $this->meta['height'];
156 if(!$x) $x = $ratio ? $this->meta['width']*$y/$this->meta['height'] : $this->meta['width'];
157
158 $new = $this->create($x, $y);
159 imagecopyresampled($new, $this->image, 0, 0, 0, 0, $x, $y, $this->meta['width'], $this->meta['height']);
160 $this->set($new);
161
162 return $this;
163 }
164
165 /**
166 * Crops the image. The values are given like margin/padding values in css
167 *
168 * <b>Example</b>
169 * <ul>
170 * <li>crop(10) - Crops by 10px on all sides</li>
171 * <li>crop(10, 5) - Crops by 10px on top and bottom and by 5px on left and right sides</li>
172 * <li>crop(10, 5, 5) - Crops by 10px on top and by 5px on left, right and bottom sides</li>
173 * <li>crop(10, 5, 3, 2) - Crops by 10px on top, 5px by right, 3px by bottom and 2px by left sides</li>
174 * </ul>
175 *
176 * @param int $top
177 * @param int $right
178 * @param int $bottom
179 * @param int $left
180 * @return Image
181 */
182 public function crop($top, $right = null, $bottom = null, $left = null){
183 if(empty($this->image)) return $this;
184
185 if(!is_numeric($right) && !is_numeric($bottom) && !is_numeric($left))
186 $right = $bottom = $left = $top;
187
188 if(!is_numeric($bottom) && !is_numeric($left)){
189 $bottom = $top;
190 $left = $right;
191 }
192
193 if(!is_numeric($left))
194 $left = $right;
195
196 $x = $this->meta['width']-$left-$right;
197 $y = $this->meta['height']-$top-$bottom;
198
199 if($x<0 || $y<0) return $this;
200
201 $new = $this->create($x, $y);
202 imagecopy($new, $this->image, 0, 0, $left, $top, $x, $y);
203 $this->set($new);
204
205 return $this;
206 }
207
208 /**
209 * Flips the image horizontally or vertically. To Flip both just use ->rotate(180)
210 *
211 * @see Image::rotate()
212 * @param string $type Either horizontal or vertical
213 * @return Image
214 */
215 public function flip($type){
216 if(empty($this->image) || !in_array($type, array('horizontal', 'vertical'))) return $this;
217
218 $new = $this->create();
219
220 if($type=='horizontal')
221 for($x=0;$x<$this->meta['width'];$x++)
222 imagecopy($new, $this->image, $this->meta['width']-$x-1, 0, $x, 0, 1, $this->meta['height']);
223 elseif($type=='vertical')
224 for($y=0;$y<$this->meta['height'];$y++)
225 imagecopy($new, $this->image, 0, $this->meta['height']-$y-1, 0, $y, $this->meta['width'], 1);
226
227 $this->set($new);
228
229 return $this;
230 }
231
232 /**
233 * Stores the image in the desired directory or outputs it
234 *
235 * @param string $ext
236 * @param string $file
237 */
238 private function process($ext = null, $file = null){
239 if(!$ext) $ext = $this->meta['ext'];
240
241 if($ext=='png') imagesavealpha($this->image, true);
242 $fn = 'image'.$ext;
243 $fn($this->image, $file);
244
245 // If there is a new filename change the internal name too
246 if($file) $this->file = $file;
247 }
248
249 /**
250 * Saves the image to the given path
251 *
252 * @param string $file Leave empty to replace the original file
253 * @return Image
254 */
255 public function save($file = null){
256 if(empty($this->image)) return $this;
257
258 if(!$file) $file = $this->file;
259
260 $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
261 if(!$ext){
262 $file .= '.'.$this->meta['ext'];
263 $ext = $this->meta['ext'];
264 }
265
266 if($ext=='jpg') $ext = 'jpeg';
267
268 if(!in_array($ext, array('png', 'jpeg', 'gif')))
269 return $this;
270
271 $this->process($ext, $file);
272
273 return $this;
274 }
275
276 /**
277 * Outputs the manipulated image
278 *
279 * @return Image
280 */
281 public function show(){
282 if(empty($this->image)) return $this;
283
284 header('Content-type: '.$this->meta['mime']);
285 $this->process();
286
287 return $this;
288 }
289
290 }