1 | <?php |
---|
2 | |
---|
3 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
4 | |
---|
5 | function image_resize_ext_im($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality) |
---|
6 | { |
---|
7 | return pwg_image_resize_ext_im($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality, false); |
---|
8 | } |
---|
9 | |
---|
10 | function thumb_resize_ext_im($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality) |
---|
11 | { |
---|
12 | return pwg_image_resize_ext_im($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality, true); |
---|
13 | } |
---|
14 | |
---|
15 | function pwg_image_resize_ext_im($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality, $thumb) |
---|
16 | { |
---|
17 | global $conf; |
---|
18 | |
---|
19 | if ($result !== false) |
---|
20 | { |
---|
21 | //someone hooked us - so we skip |
---|
22 | return $result; |
---|
23 | } |
---|
24 | |
---|
25 | // extension of the picture filename |
---|
26 | $extension = strtolower(get_extension($source_filepath)); |
---|
27 | if (!in_array($extension, array('jpg', 'jpeg', 'png'))) |
---|
28 | { |
---|
29 | die('[Imagick] unsupported file extension'); |
---|
30 | } |
---|
31 | |
---|
32 | $image = new Imagick($source_filepath); |
---|
33 | |
---|
34 | $rotation = get_rotation_angle($source_filepath); |
---|
35 | |
---|
36 | // width/height |
---|
37 | $source_width = $image->getImageWidth(); |
---|
38 | $source_height = $image->getImageHeight(); |
---|
39 | |
---|
40 | if ($thumb and isset($conf['upload_form_thumb_square']) and $conf['upload_form_thumb_square']) |
---|
41 | { |
---|
42 | $coord = process_ratio($source_width, $source_height, $rotation); |
---|
43 | $image->cropImage($source_width, $source_height, $coord['x'], $coord['y']); |
---|
44 | } |
---|
45 | |
---|
46 | $resize_dimensions = get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation); |
---|
47 | |
---|
48 | // testing on height is useless in theory: if width is unchanged, there |
---|
49 | // should be no resize, because width/height ratio is not modified. |
---|
50 | if ($resize_dimensions['width'] == $source_width and $resize_dimensions['height'] == $source_height) |
---|
51 | { |
---|
52 | // the image doesn't need any resize! We just copy it to the destination |
---|
53 | copy($source_filepath, $destination_filepath); |
---|
54 | return true; |
---|
55 | } |
---|
56 | |
---|
57 | $image->setImageCompressionQuality($quality); |
---|
58 | $image->setInterlaceScheme(Imagick::INTERLACE_LINE); |
---|
59 | |
---|
60 | $image->resizeImage($resize_dimensions['width'], $resize_dimensions['height'], Imagick::FILTER_LANCZOS, 0.9); |
---|
61 | |
---|
62 | if (isset($rotation)) |
---|
63 | { |
---|
64 | $image->rotateImage(new ImagickPixel(), -$rotation); |
---|
65 | $image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT); |
---|
66 | } |
---|
67 | |
---|
68 | $image->writeImage($destination_filepath); |
---|
69 | $image->destroy(); |
---|
70 | |
---|
71 | // everything should be OK if we are here! |
---|
72 | return true; |
---|
73 | } |
---|
74 | |
---|
75 | function get_rotation_angle($source_filepath) |
---|
76 | { |
---|
77 | global $conf; |
---|
78 | |
---|
79 | if (!function_exists('exif_read_data')) |
---|
80 | { |
---|
81 | return null; |
---|
82 | } |
---|
83 | |
---|
84 | $rotation = null; |
---|
85 | |
---|
86 | $exif = exif_read_data($source_filepath); |
---|
87 | |
---|
88 | if (isset($exif['Orientation']) and preg_match('/^\s*(\d)/', $exif['Orientation'], $matches)) |
---|
89 | { |
---|
90 | $orientation = $matches[1]; |
---|
91 | if (in_array($orientation, array(3, 4))) |
---|
92 | { |
---|
93 | $rotation = 180; |
---|
94 | } |
---|
95 | elseif (in_array($orientation, array(5, 6))) |
---|
96 | { |
---|
97 | $rotation = 270; |
---|
98 | } |
---|
99 | elseif (in_array($orientation, array(7, 8))) |
---|
100 | { |
---|
101 | $rotation = 90; |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | return $rotation; |
---|
106 | } |
---|
107 | |
---|
108 | function get_resize_dimensions($width, $height, $max_width, $max_height, $rotation=null) |
---|
109 | { |
---|
110 | $rotate_for_dimensions = false; |
---|
111 | if (isset($rotation) and in_array(abs($rotation), array(90, 270))) |
---|
112 | { |
---|
113 | $rotate_for_dimensions = true; |
---|
114 | } |
---|
115 | |
---|
116 | if ($rotate_for_dimensions) |
---|
117 | { |
---|
118 | list($width, $height) = array($height, $width); |
---|
119 | } |
---|
120 | |
---|
121 | $ratio_width = $width / $max_width; |
---|
122 | $ratio_height = $height / $max_height; |
---|
123 | |
---|
124 | // maximal size exceeded ? |
---|
125 | if ($ratio_width > 1 or $ratio_height > 1) |
---|
126 | { |
---|
127 | if ($ratio_width < $ratio_height) |
---|
128 | { |
---|
129 | $destination_width = ceil($width / $ratio_height); |
---|
130 | $destination_height = $max_height; |
---|
131 | } |
---|
132 | else |
---|
133 | { |
---|
134 | $destination_width = $max_width; |
---|
135 | $destination_height = ceil($height / $ratio_width); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | if ($rotate_for_dimensions) |
---|
140 | { |
---|
141 | list($destination_width, $destination_height) = array($destination_height, $destination_width); |
---|
142 | } |
---|
143 | |
---|
144 | return array( |
---|
145 | 'width' => $destination_width, |
---|
146 | 'height'=> $destination_height, |
---|
147 | ); |
---|
148 | } |
---|
149 | |
---|
150 | function process_ratio(&$source_width, &$source_height, $rotation=null) |
---|
151 | { |
---|
152 | global $conf; |
---|
153 | |
---|
154 | if (!isset($conf['thumbnails_ratio']) or !preg_match('/^\d+:\d+$/', $conf['thumbnails_ratio'])) |
---|
155 | $conf['thumbnails_ratio'] = '1:1'; |
---|
156 | |
---|
157 | if (!isset($conf['thumbnails_ratio_orientation'])) |
---|
158 | $conf['thumbnails_ratio_orientation'] = true; |
---|
159 | |
---|
160 | if (!$conf['thumbnails_ratio_orientation'] and $rotation) |
---|
161 | $conf['thumbnails_ratio_orientation'] = true; |
---|
162 | |
---|
163 | $x = 0; |
---|
164 | $y = 0; |
---|
165 | |
---|
166 | if ($source_width >= $source_height or !$conf['thumbnails_ratio_orientation']) |
---|
167 | list($widthRatio, $heightRatio) = explode(':', $conf['thumbnails_ratio']); |
---|
168 | else |
---|
169 | list($heightRatio, $widthRatio) = explode(':', $conf['thumbnails_ratio']); |
---|
170 | |
---|
171 | $img_ratio = $source_width / $source_height; |
---|
172 | $dest_ratio = $widthRatio / $heightRatio; |
---|
173 | |
---|
174 | if($dest_ratio > $img_ratio) |
---|
175 | { |
---|
176 | $destHeight = ceil(($source_width * $heightRatio) / $widthRatio); |
---|
177 | $y = ceil(($source_height - $destHeight) / 2 ); |
---|
178 | $source_height = $destHeight ; |
---|
179 | } |
---|
180 | elseif ($dest_ratio < $img_ratio) |
---|
181 | { |
---|
182 | $destWidth = ceil(($source_height * $widthRatio) / $heightRatio); |
---|
183 | $x = ceil(($source_width - $destWidth) / 2 ); |
---|
184 | $source_width = $destWidth ; |
---|
185 | } |
---|
186 | |
---|
187 | return array('x' => $x, 'y' => $y); |
---|
188 | } |
---|
189 | |
---|
190 | add_event_handler('upload_image_resize', 'image_resize_ext_im', 30, 6); |
---|
191 | add_event_handler('upload_thumbnail_resize', 'thumb_resize_ext_im', 30, 6); |
---|
192 | |
---|
193 | ?> |
---|