@@ -11,57 +11,57 @@ |
||
11 | 11 | |
12 | 12 | function crop($image, $mimeType, $imgWidth, $imgHeight, $newWidth, $newHeight){ |
13 | 13 | |
14 | - switch ($mimeType) { |
|
15 | - case "jpg": |
|
16 | - case "jpeg": |
|
17 | - $imageCreate = imagecreatefromjpeg($image); |
|
18 | - break; |
|
14 | + switch ($mimeType) { |
|
15 | + case "jpg": |
|
16 | + case "jpeg": |
|
17 | + $imageCreate = imagecreatefromjpeg($image); |
|
18 | + break; |
|
19 | 19 | |
20 | - case "png": |
|
21 | - $imageCreate = imagecreatefrompng($image); |
|
22 | - break; |
|
20 | + case "png": |
|
21 | + $imageCreate = imagecreatefrompng($image); |
|
22 | + break; |
|
23 | 23 | |
24 | - case "gif": |
|
25 | - $imageCreate = imagecreatefromgif($image); |
|
26 | - break; |
|
24 | + case "gif": |
|
25 | + $imageCreate = imagecreatefromgif($image); |
|
26 | + break; |
|
27 | 27 | |
28 | - default: |
|
29 | - throw new \Exception(" Only gif, jpg, jpeg and png files can be cropped "); |
|
30 | - break; |
|
31 | - } |
|
28 | + default: |
|
29 | + throw new \Exception(" Only gif, jpg, jpeg and png files can be cropped "); |
|
30 | + break; |
|
31 | + } |
|
32 | 32 | |
33 | - // The image offsets/coordination to crop the image. |
|
34 | - $widthTrim = ceil(($imgWidth - $newWidth) / 2); |
|
35 | - $heightTrim = ceil(($imgHeight - $newHeight) / 2); |
|
33 | + // The image offsets/coordination to crop the image. |
|
34 | + $widthTrim = ceil(($imgWidth - $newWidth) / 2); |
|
35 | + $heightTrim = ceil(($imgHeight - $newHeight) / 2); |
|
36 | 36 | |
37 | - // Can't crop to a bigger size, ex: |
|
38 | - // an image with 100X100 can not be cropped to 200X200. Image can only be cropped to smaller size. |
|
39 | - if ($widthTrim < 0 && $heightTrim < 0) { |
|
40 | - return ; |
|
41 | - } |
|
37 | + // Can't crop to a bigger size, ex: |
|
38 | + // an image with 100X100 can not be cropped to 200X200. Image can only be cropped to smaller size. |
|
39 | + if ($widthTrim < 0 && $heightTrim < 0) { |
|
40 | + return ; |
|
41 | + } |
|
42 | 42 | |
43 | - $temp = imagecreatetruecolor($newWidth, $newHeight); |
|
44 | - imageAlphaBlending($temp, false); |
|
45 | - imageSaveAlpha($temp, true); |
|
46 | - imagecopyresampled( |
|
47 | - $temp, |
|
48 | - $imageCreate, |
|
49 | - 0, |
|
50 | - 0, |
|
51 | - $widthTrim, |
|
52 | - $heightTrim, |
|
53 | - $newWidth, |
|
54 | - $newHeight, |
|
55 | - $newWidth, |
|
56 | - $newHeight |
|
57 | - ); |
|
43 | + $temp = imagecreatetruecolor($newWidth, $newHeight); |
|
44 | + imageAlphaBlending($temp, false); |
|
45 | + imageSaveAlpha($temp, true); |
|
46 | + imagecopyresampled( |
|
47 | + $temp, |
|
48 | + $imageCreate, |
|
49 | + 0, |
|
50 | + 0, |
|
51 | + $widthTrim, |
|
52 | + $heightTrim, |
|
53 | + $newWidth, |
|
54 | + $newHeight, |
|
55 | + $newWidth, |
|
56 | + $newHeight |
|
57 | + ); |
|
58 | 58 | |
59 | 59 | |
60 | - if (!$temp) { |
|
61 | - throw new \Exception("Failed to crop image. Please pass the right parameters"); |
|
62 | - } else { |
|
63 | - imagejpeg($temp, $image); |
|
64 | - } |
|
60 | + if (!$temp) { |
|
61 | + throw new \Exception("Failed to crop image. Please pass the right parameters"); |
|
62 | + } else { |
|
63 | + imagejpeg($temp, $image); |
|
64 | + } |
|
65 | 65 | |
66 | 66 | } |
67 | 67 |