view e2gallerypro/e2upload/Backend/Image.php @ 28:118cb614cf18 judyates tip

judy bio.
author Robert McIntyre <rlm@mit.edu>
date Tue, 29 Dec 2015 16:26:18 -0800
parents 3f6b44aa6b35
children
line wrap: on
line source
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 */
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;
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;
42 $this->file = $file;
43 $img = getimagesize($file);
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';
54 if(!in_array($this->meta['ext'], array('gif', 'png', 'jpeg')))
55 return;
57 if(in_array($this->meta['ext'], array('gif', 'png'))){
58 $this->image = $this->create();
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 }
68 public function __destruct(){
69 if(!empty($this->image)) imagedestroy($this->image);
70 }
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 }
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'];
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 }
103 return $image;
104 }
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;
115 $this->meta['width'] = imagesx($this->image);
116 $this->meta['height'] = imagesy($this->image);
117 }
119 /**
120 * Returns the path to the image file
121 *
122 * @return string
123 */
124 public function getPathname(){
125 return $this->file;
126 }
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;
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));
140 return $this;
141 }
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;
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'];
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);
162 return $this;
163 }
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;
185 if(!is_numeric($right) && !is_numeric($bottom) && !is_numeric($left))
186 $right = $bottom = $left = $top;
188 if(!is_numeric($bottom) && !is_numeric($left)){
189 $bottom = $top;
190 $left = $right;
191 }
193 if(!is_numeric($left))
194 $left = $right;
196 $x = $this->meta['width']-$left-$right;
197 $y = $this->meta['height']-$top-$bottom;
199 if($x<0 || $y<0) return $this;
201 $new = $this->create($x, $y);
202 imagecopy($new, $this->image, 0, 0, $left, $top, $x, $y);
203 $this->set($new);
205 return $this;
206 }
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;
218 $new = $this->create();
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);
227 $this->set($new);
229 return $this;
230 }
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'];
241 if($ext=='png') imagesavealpha($this->image, true);
242 $fn = 'image'.$ext;
243 $fn($this->image, $file);
245 // If there is a new filename change the internal name too
246 if($file) $this->file = $file;
247 }
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;
258 if(!$file) $file = $this->file;
260 $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
261 if(!$ext){
262 $file .= '.'.$this->meta['ext'];
263 $ext = $this->meta['ext'];
264 }
266 if($ext=='jpg') $ext = 'jpeg';
268 if(!in_array($ext, array('png', 'jpeg', 'gif')))
269 return $this;
271 $this->process($ext, $file);
273 return $this;
274 }
276 /**
277 * Outputs the manipulated image
278 *
279 * @return Image
280 */
281 public function show(){
282 if(empty($this->image)) return $this;
284 header('Content-type: '.$this->meta['mime']);
285 $this->process();
287 return $this;
288 }