-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathresizer.php
354 lines (309 loc) · 9.74 KB
/
resizer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
/**
* Provides a very simple way to resize an image.
*
* Credits to Jarrod Oberto.
* Jarrod wrote a tutorial on NetTuts.
* http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/
*
* I only turned it into a Laravel bundle.
*
* @package Resizer
* @version 1.0
* @author Maikel D (original author Jarrod Oberto)
* @link
* @example
* Resizer::open( mixed $file )
* ->resize( int $width , int $height , string 'exact, portrait, landscape, auto or crop' )
* ->save( string 'path/to/file.jpg' , int $quality );
*
* // Resize and save an image.
* Resizer::open( Input::file('field_name') )
* ->resize( 800 , 600 , 'crop' )
* ->save( 'path/to/file.jpg' , 100 );
*
* // Recompress an image.
* Resizer::open( 'path/to/image.jpg' )
* ->save( 'path/to/new_image.jpg' , 60 );
*/
class Resizer {
/**
* Store the image resource which we'll modify
* @var Resource
*/
private $image;
/**
* Original width of the image we're modifying
* @var int
*/
private $width;
/**
* Original height of the image we're modifying
* @var int
*/
private $height;
/**
* Store the resource of the resized image
* @var Resource
*/
private $image_resized;
/**
* Instantiates the Resizer and receives the path to an image we're working with
* @param mixed $file The file array provided by Laravel's Input::file('field_name') or a path to a file
*/
function __construct( $file )
{
// Open up the file
$this->image = $this->open_image( $file );
// Get width and height of our image
$this->width = imagesx( $this->image );
$this->height = imagesy( $this->image );
}
/**
* Static call, Laravel style.
* Returns a new Resizer object, allowing for chainable calls
* @param mixed $file The file array provided by Laravel's Input::file('field_name') or a path to a file
* @return Resizer
*/
public static function open( $file )
{
return new Resizer( $file );
}
/**
* Resizes and/or crops an image
* @param int $new_width The width of the image
* @param int $new_height The height of the image
* @param string $option Either exact, portrait, landscape, auto or crop.
* @return [type]
*/
public function resize( $new_width , $new_height , $option = 'auto' )
{
// Get optimal width and height - based on $option
$option_array = $this->get_dimensions( $new_width , $new_height , $option );
$optimal_width = $option_array['optimal_width'];
$optimal_height = $option_array['optimal_height'];
// Resample - create image canvas of x, y size
$this->image_resized = imagecreatetruecolor( $optimal_width , $optimal_height );
imagecopyresampled( $this->image_resized , $this->image , 0 , 0 , 0 , 0 , $optimal_width , $optimal_height , $this->width , $this->height );
// if option is 'crop', then crop too
if ( $option == 'crop' ) {
$this->crop( $optimal_width , $optimal_height , $new_width , $new_height );
}
// Return $this to allow calls to be chained
return $this;
}
/**
* Save the image based on its file type.
* @param string $save_path Where to save the image
* @param int $image_quality The output quality of the image
* @return boolean
*/
public function save( $save_path , $image_quality = 95 )
{
// If the image wasn't resized, fetch original image.
if ( !$this->image_resized ) {
$this->image_resized = $this->image;
}
// Get extension of the output file
$extension = strtolower( File::extension($save_path) );
// Create and save an image based on it's extension
switch( $extension )
{
case 'jpg':
case 'jpeg':
if ( imagetypes() & IMG_JPG ) {
imagejpeg( $this->image_resized , $save_path , $image_quality );
}
break;
case 'gif':
if ( imagetypes() & IMG_GIF ) {
imagegif( $this->image_resized , $save_path );
}
break;
case 'png':
// Scale quality from 0-100 to 0-9
$scale_quality = round( ($image_quality/100) * 9 );
// Invert quality setting as 0 is best, not 9
$invert_scale_quality = 9 - $scale_quality;
if ( imagetypes() & IMG_PNG ) {
imagepng( $this->image_resized , $save_path , $invert_scale_quality );
}
break;
default:
return false;
break;
}
// Remove the resource for the resized image
imagedestroy( $this->image_resized );
return true;
}
/**
* Open a file, detect its mime-type and create an image resrource from it.
* @param array $file Attributes of file from the $_FILES array
* @return mixed
*/
private function open_image( $file )
{
// If $file isn't an array, we'll turn it into one
if ( !is_array($file) ) {
$file = array(
'type' => File::mime( strtolower(File::extension($file)) ),
'tmp_name' => $file
);
}
$mime = $file['type'];
$file_path = $file['tmp_name'];
switch ( $mime )
{
case 'image/pjpeg': // IE6
case File::mime('jpg'): $img = @imagecreatefromjpeg( $file_path ); break;
case File::mime('gif'): $img = @imagecreatefromgif( $file_path ); break;
case File::mime('png'): $img = @imagecreatefrompng( $file_path ); break;
default: $img = false; break;
}
return $img;
}
/**
* Return the image dimentions based on the option that was chosen.
* @param int $new_width The width of the image
* @param int $new_height The height of the image
* @param string $option Either exact, portrait, landscape, auto or crop.
* @return array
*/
private function get_dimensions( $new_width , $new_height , $option )
{
switch ( $option )
{
case 'exact':
$optimal_width = $new_width;
$optimal_height = $new_height;
break;
case 'portrait':
$optimal_width = $this->get_size_by_fixed_height( $new_height );
$optimal_height = $new_height;
break;
case 'landscape':
$optimal_width = $new_width;
$optimal_height = $this->get_size_by_fixed_width( $new_width );
break;
case 'auto':
$option_array = $this->get_size_by_auto( $new_width , $new_height );
$optimal_width = $option_array['optimal_width'];
$optimal_height = $option_array['optimal_height'];
break;
case 'crop':
$option_array = $this->get_optimal_crop( $new_width , $new_height );
$optimal_width = $option_array['optimal_width'];
$optimal_height = $option_array['optimal_height'];
break;
}
return array(
'optimal_width' => $optimal_width,
'optimal_height' => $optimal_height
);
}
/**
* Returns the width based on the image height
* @param int $new_height The height of the image
* @return int
*/
private function get_size_by_fixed_height( $new_height )
{
$ratio = $this->width / $this->height;
$new_width = $new_height * $ratio;
return $new_width;
}
/**
* Returns the height based on the image width
* @param int $new_width The width of the image
* @return int
*/
private function get_size_by_fixed_width( $new_width )
{
$ratio = $this->height / $this->width;
$new_height = $new_width * $ratio;
return $new_height;
}
/**
* Checks to see if an image is portrait or landscape and resizes accordingly.
* @param int $new_width The width of the image
* @param int $new_height The height of the image
* @return array
*/
private function get_size_by_auto( $new_width , $new_height )
{
// Image to be resized is wider (landscape)
if ( $this->height < $this->width )
{
$optimal_width = $new_width;
$optimal_height= $this->get_size_by_fixed_width( $new_width );
}
// Image to be resized is taller (portrait)
elseif ( $this->height > $this->width )
{
$optimal_width = $this->get_size_by_fixed_height( $new_height );
$optimal_height= $new_height;
}
// Image to be resizerd is a square
else
{
if ( $new_height < $new_width ) {
$optimal_width = $new_width;
$optimal_height= $this->get_size_by_fixed_width( $new_width );
} else if ( $new_height > $new_width ) {
$optimal_width = $this->get_size_by_fixed_height( $new_height );
$optimal_height= $new_height;
} else {
// Sqaure being resized to a square
$optimal_width = $new_width;
$optimal_height= $new_height;
}
}
return array(
'optimal_width' => $optimal_width,
'optimal_height' => $optimal_height
);
}
/**
* Attempts to find the best way to crop. Whether crop is based on the
* image being portrait or landscape.
* @param int $new_width The width of the image
* @param int $new_height The height of the image
* @return array
*/
private function get_optimal_crop( $new_width , $new_height )
{
$height_ratio = $this->height / $new_height;
$width_ratio = $this->width / $new_width;
if ( $height_ratio < $width_ratio ) {
$optimal_ratio = $height_ratio;
} else {
$optimal_ratio = $width_ratio;
}
$optimal_height = $this->height / $optimal_ratio;
$optimal_width = $this->width / $optimal_ratio;
return array(
'optimal_width' => $optimal_width,
'optimal_height' => $optimal_height
);
}
/**
* Crops an image from its center
* @param int $optimal_width The width of the image
* @param int $optimal_height The height of the image
* @param int $new_width The new width
* @param int $new_height The new height
* @return true
*/
private function crop( $optimal_width , $optimal_height , $new_width , $new_height )
{
// Find center - this will be used for the crop
$crop_start_x = ( $optimal_width / 2 ) - ( $new_width / 2 );
$crop_start_y = ( $optimal_height / 2 ) - ( $new_height / 2 );
$crop = $this->image_resized;
// Now crop from center to exact requested size
$this->image_resized = imagecreatetruecolor( $new_width , $new_height );
imagecopyresampled( $this->image_resized , $crop , 0 , 0 , $crop_start_x , $crop_start_y , $new_width , $new_height , $new_width , $new_height );
return true;
}
}