Skip to content

Commit 2f4638d

Browse files
committed
Added crop option to image versions. Closes blueimp#2187.
1 parent ed13292 commit 2f4638d

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

server/php/UploadHandler.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/*
3-
* jQuery File Upload Plugin PHP Class 6.2
3+
* jQuery File Upload Plugin PHP Class 6.3
44
* https://github.com/blueimp/jQuery-File-Upload
55
*
66
* Copyright 2010, Sebastian Tschan
@@ -101,6 +101,9 @@ function __construct($options = null, $initialize = true, $error_messages = null
101101
),
102102
*/
103103
'thumbnail' => array(
104+
// Uncomment the following to force the max
105+
// dimensions and e.g. create square thumbnails:
106+
//'crop' => true,
104107
'max_width' => 80,
105108
'max_height' => 80
106109
)
@@ -280,19 +283,36 @@ protected function create_scaled_image($file_name, $version, $options) {
280283
if (!$img_width || !$img_height) {
281284
return false;
282285
}
286+
$max_width = $options['max_width'];
287+
$max_height = $options['max_height'];
283288
$scale = min(
284-
$options['max_width'] / $img_width,
285-
$options['max_height'] / $img_height
289+
$max_width / $img_width,
290+
$max_height / $img_height
286291
);
287292
if ($scale >= 1) {
288293
if ($file_path !== $new_file_path) {
289294
return copy($file_path, $new_file_path);
290295
}
291296
return true;
292297
}
293-
$new_width = $img_width * $scale;
294-
$new_height = $img_height * $scale;
295-
$new_img = @imagecreatetruecolor($new_width, $new_height);
298+
if (empty($options['crop'])) {
299+
$new_width = $img_width * $scale;
300+
$new_height = $img_height * $scale;
301+
$dst_x = 0;
302+
$dst_y = 0;
303+
$new_img = @imagecreatetruecolor($new_width, $new_height);
304+
} else {
305+
if (($img_width / $img_height) >= ($max_width / $max_height)) {
306+
$new_width = $img_width / ($img_height / $max_height);
307+
$new_height = $max_height;
308+
} else {
309+
$new_width = $max_width;
310+
$new_height = $img_height / ($img_width / $max_width);
311+
}
312+
$dst_x = 0 - ($new_width - $max_width) / 2;
313+
$dst_y = 0 - ($new_height - $max_height) / 2;
314+
$new_img = @imagecreatetruecolor($max_width, $max_height);
315+
}
296316
switch (strtolower(substr(strrchr($file_name, '.'), 1))) {
297317
case 'jpg':
298318
case 'jpeg':
@@ -322,7 +342,10 @@ protected function create_scaled_image($file_name, $version, $options) {
322342
$success = $src_img && @imagecopyresampled(
323343
$new_img,
324344
$src_img,
325-
0, 0, 0, 0,
345+
$dst_x,
346+
$dst_y,
347+
0,
348+
0,
326349
$new_width,
327350
$new_height,
328351
$img_width,

0 commit comments

Comments
 (0)