3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * gdlib.php - Collection of routines in Moodle related to
20 * processing images using GD
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 * @param object $dst_img
32 * @param object $src_img
42 * @todo Finish documenting this function
44 function ImageCopyBicubic ($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) {
48 if (function_exists('ImageCopyResampled') and $CFG->gdversion >= 2) {
49 return ImageCopyResampled($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y,
50 $dst_w, $dst_h, $src_w, $src_h);
53 $totalcolors = imagecolorstotal($src_img);
54 for ($i=0; $i<$totalcolors; $i++) {
55 if ($colors = ImageColorsForIndex($src_img, $i)) {
56 ImageColorAllocate($dst_img, $colors['red'], $colors['green'], $colors['blue']);
60 $scaleX = ($src_w - 1) / $dst_w;
61 $scaleY = ($src_h - 1) / $dst_h;
63 $scaleX2 = $scaleX / 2.0;
64 $scaleY2 = $scaleY / 2.0;
66 for ($j = 0; $j < $dst_h; $j++) {
69 for ($i = 0; $i < $dst_w; $i++) {
72 $c1 = ImageColorsForIndex($src_img,ImageColorAt($src_img,(int)$sX,(int)$sY+$scaleY2));
73 $c2 = ImageColorsForIndex($src_img,ImageColorAt($src_img,(int)$sX,(int)$sY));
74 $c3 = ImageColorsForIndex($src_img,ImageColorAt($src_img,(int)$sX+$scaleX2,(int)$sY+$scaleY2));
75 $c4 = ImageColorsForIndex($src_img,ImageColorAt($src_img,(int)$sX+$scaleX2,(int)$sY));
77 $red = (int) (($c1['red'] + $c2['red'] + $c3['red'] + $c4['red']) / 4);
78 $green = (int) (($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) / 4);
79 $blue = (int) (($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue']) / 4);
81 $color = ImageColorClosest ($dst_img, $red, $green, $blue);
82 ImageSetPixel ($dst_img, $i + $dst_x, $j + $dst_y, $color);
88 * Delete profile images associated with user or group
91 * @param int $id user or group id
92 * @param string $dir type of entity - 'groups' or 'users'
93 * @return boolean success
95 function delete_profile_image($id, $dir='users') {
98 require_once $CFG->libdir.'/filelib.php';
99 $location = $CFG->dataroot .'/'. $dir .'/'. $id;
101 if (file_exists($location)) {
102 return fulldelete($location);
109 * Given an upload manager with the right settings, this function performs a virus scan, and then scales and crops
110 * it and saves it in the right place to be a "user" or "group" image.
113 * @param int $id user or group id
114 * @param string $dir type of entity - groups, user, ...
115 * @return string $destination (profile image destination path) or false on error
117 function create_profile_image_destination($id, $dir='user') {
122 if (!file_exists($CFG->dataroot .'/'. $dir)) {
123 if (!mkdir($CFG->dataroot .'/'. $dir, $CFG->directorypermissions)) {
128 if ($dir == 'user') {
129 $destination = make_user_directory($id, true);
131 $destination = "$CFG->dataroot/$dir/$id";
134 if (!file_exists($destination)) {
135 if (!make_upload_directory(str_replace($CFG->dataroot . '/', '', $destination))) {
143 * Given an upload manager with the right settings, this function performs a virus scan, and then scales and crops
144 * it and saves it in the right place to be a "user" or "group" image.
146 * @param int $id user or group id
147 * @param object $userform with imagefile upload field
148 * @param string $dir type of entity - groups, user, ...
149 * @return boolean success
151 function save_profile_image($id, $userform, $dir='user') {
153 $destination = create_profile_image_destination($id, $dir);
154 if ($destination === false) {
158 $filename = $userform->get_new_filename('imagefile');
159 $pathname = $destination.'/'.$filename;
161 if (!$userform->save_file('imagefile', $pathname, true)) {
165 return process_profile_image($pathname, $destination);
169 * Given a path to an image file this function scales and crops it and saves it in
170 * the right place to be a "user" or "group" image.
173 * @param string $originalfile the path of the original image file
174 * @param string $destination the final destination directory of the profile image
177 function process_profile_image($originalfile, $destination) {
178 global $CFG, $OUTPUT;
180 if(!(is_file($originalfile) && is_dir($destination))) {
184 if (empty($CFG->gdversion)) {
188 $imageinfo = GetImageSize($originalfile);
190 if (empty($imageinfo)) {
191 if (file_exists($originalfile)) {
192 unlink($originalfile);
197 $image->width = $imageinfo[0];
198 $image->height = $imageinfo[1];
199 $image->type = $imageinfo[2];
201 switch ($image->type) {
203 if (function_exists('ImageCreateFromGIF')) {
204 $im = ImageCreateFromGIF($originalfile);
206 notice('GIF not supported on this server');
207 unlink($originalfile);
212 if (function_exists('ImageCreateFromJPEG')) {
213 $im = ImageCreateFromJPEG($originalfile);
215 notice('JPEG not supported on this server');
216 unlink($originalfile);
221 if (function_exists('ImageCreateFromPNG')) {
222 $im = ImageCreateFromPNG($originalfile);
224 notice('PNG not supported on this server');
225 unlink($originalfile);
230 unlink($originalfile);
234 unlink($originalfile);
236 if (function_exists('ImageCreateTrueColor') and $CFG->gdversion >= 2) {
237 $im1 = ImageCreateTrueColor(100,100);
238 $im2 = ImageCreateTrueColor(35,35);
240 $im1 = ImageCreate(100,100);
241 $im2 = ImageCreate(35,35);
244 $cx = $image->width / 2;
245 $cy = $image->height / 2;
247 if ($image->width < $image->height) {
248 $half = floor($image->width / 2.0);
250 $half = floor($image->height / 2.0);
253 ImageCopyBicubic($im1, $im, 0, 0, $cx-$half, $cy-$half, 100, 100, $half*2, $half*2);
254 ImageCopyBicubic($im2, $im, 0, 0, $cx-$half, $cy-$half, 35, 35, $half*2, $half*2);
256 if (function_exists('ImageJpeg')) {
257 @touch($destination .'/f1.jpg'); // Helps in Safe mode
258 @touch($destination .'/f2.jpg'); // Helps in Safe mode
259 if (ImageJpeg($im1, $destination .'/f1.jpg', 90) and
260 ImageJpeg($im2, $destination .'/f2.jpg', 95) ) {
261 @chmod($destination .'/f1.jpg', 0666);
262 @chmod($destination .'/f2.jpg', 0666);
266 echo $OUTPUT->notification('PHP has not been configured to support JPEG images. Please correct this.');
272 * Given a user id this function scales and crops the user images to remove
273 * the one pixel black border.
280 function upgrade_profile_image($id, $dir='users') {
281 global $CFG, $OUTPUT;
283 $im = ImageCreateFromJPEG($CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg');
285 if (function_exists('ImageCreateTrueColor') and $CFG->gdversion >= 2) {
286 $im1 = ImageCreateTrueColor(100,100);
287 $im2 = ImageCreateTrueColor(35,35);
289 $im1 = ImageCreate(100,100);
290 $im2 = ImageCreate(35,35);
293 if (function_exists('ImageCopyResampled') and $CFG->gdversion >= 2) {
294 ImageCopyBicubic($im1, $im, 0, 0, 2, 2, 100, 100, 96, 96);
296 imagecopy($im1, $im, 0, 0, 0, 0, 100, 100);
297 $c = ImageColorsForIndex($im1,ImageColorAt($im1,2,2));
298 $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']);
299 ImageSetPixel ($im1, 0, 0, $color);
300 $c = ImageColorsForIndex($im1,ImageColorAt($im1,2,97));
301 $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']);
302 ImageSetPixel ($im1, 0, 99, $color);
303 $c = ImageColorsForIndex($im1,ImageColorAt($im1,97,2));
304 $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']);
305 ImageSetPixel ($im1, 99, 0, $color);
306 $c = ImageColorsForIndex($im1,ImageColorAt($im1,97,97));
307 $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']);
308 ImageSetPixel ($im1, 99, 99, $color);
309 for ($x = 1; $x < 99; $x++) {
310 $c1 = ImageColorsForIndex($im1,ImageColorAt($im,$x,1));
311 $color = ImageColorClosest ($im, $c1['red'], $c1['green'], $c1['blue']);
312 ImageSetPixel ($im1, $x, 0, $color);
313 $c2 = ImageColorsForIndex($im1,ImageColorAt($im1,$x,98));
314 $color = ImageColorClosest ($im, $c2['red'], $c2['green'], $c2['blue']);
315 ImageSetPixel ($im1, $x, 99, $color);
317 for ($y = 1; $y < 99; $y++) {
318 $c3 = ImageColorsForIndex($im1,ImageColorAt($im, 1, $y));
319 $color = ImageColorClosest ($im, $c3['red'], $c3['green'], $c3['blue']);
320 ImageSetPixel ($im1, 0, $y, $color);
321 $c4 = ImageColorsForIndex($im1,ImageColorAt($im1, 98, $y));
322 $color = ImageColorClosest ($im, $c4['red'], $c4['green'], $c4['blue']);
323 ImageSetPixel ($im1, 99, $y, $color);
326 ImageCopyBicubic($im2, $im, 0, 0, 2, 2, 35, 35, 96, 96);
328 if (function_exists('ImageJpeg')) {
329 if (ImageJpeg($im1, $CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg', 90) and
330 ImageJpeg($im2, $CFG->dataroot .'/'. $dir .'/'. $id .'/f2.jpg', 95) ) {
331 @chmod($CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg', 0666);
332 @chmod($CFG->dataroot .'/'. $dir .'/'. $id .'/f2.jpg', 0666);
336 echo $OUTPUT->notification('PHP has not been configured to support JPEG images. Please correct this.');