4 * gdlib.php - Collection of routines in Moodle related to
5 * processing images using GD
7 * @author Martin Dougiamas etc
9 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
14 * short description (optional)
18 * @param type? $dst_img description?
19 * @param type? $src_img description?
20 * @param type? $dst_x description?
21 * @param type? $dst_y description?
22 * @param type? $src_x description?
23 * @param type? $src_y description?
24 * @param type? $dst_w description?
25 * @param type? $dst_h description?
26 * @param type? $src_w description?
27 * @param type? $src_h description?
29 * @todo Finish documenting this function
31 function ImageCopyBicubic ($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) {
35 if (function_exists('ImageCopyResampled') and $CFG->gdversion
>= 2) {
36 return ImageCopyResampled($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y,
37 $dst_w, $dst_h, $src_w, $src_h);
40 $totalcolors = imagecolorstotal($src_img);
41 for ($i=0; $i<$totalcolors; $i++
) {
42 if ($colors = ImageColorsForIndex($src_img, $i)) {
43 ImageColorAllocate($dst_img, $colors['red'], $colors['green'], $colors['blue']);
47 $scaleX = ($src_w - 1) / $dst_w;
48 $scaleY = ($src_h - 1) / $dst_h;
50 $scaleX2 = $scaleX / 2.0;
51 $scaleY2 = $scaleY / 2.0;
53 for ($j = 0; $j < $dst_h; $j++
) {
56 for ($i = 0; $i < $dst_w; $i++
) {
59 $c1 = ImageColorsForIndex($src_img,ImageColorAt($src_img,(int)$sX,(int)$sY+
$scaleY2));
60 $c2 = ImageColorsForIndex($src_img,ImageColorAt($src_img,(int)$sX,(int)$sY));
61 $c3 = ImageColorsForIndex($src_img,ImageColorAt($src_img,(int)$sX+
$scaleX2,(int)$sY+
$scaleY2));
62 $c4 = ImageColorsForIndex($src_img,ImageColorAt($src_img,(int)$sX+
$scaleX2,(int)$sY));
64 $red = (int) (($c1['red'] +
$c2['red'] +
$c3['red'] +
$c4['red']) / 4);
65 $green = (int) (($c1['green'] +
$c2['green'] +
$c3['green'] +
$c4['green']) / 4);
66 $blue = (int) (($c1['blue'] +
$c2['blue'] +
$c3['blue'] +
$c4['blue']) / 4);
68 $color = ImageColorClosest ($dst_img, $red, $green, $blue);
69 ImageSetPixel ($dst_img, $i +
$dst_x, $j +
$dst_y, $color);
75 * Delete profile images associated with user or group
76 * @param int $id user or group id
77 * @param string $dir type of entity - 'groups' or 'users'
78 * @return boolean success
80 function delete_profile_image($id, $dir='users') {
83 require_once $CFG->libdir
.'/filelib.php';
84 $location = $CFG->dataroot
.'/'. $dir .'/'. $id;
86 if (file_exists($location)) {
87 return fulldelete($location);
94 * Given an upload manager with the right settings, this function performs a virus scan, and then scales and crops
95 * it and saves it in the right place to be a "user" or "group" image.
97 * @param int $id user or group id
98 * @param string $dir type of entity - groups, user, ...
99 * @return string $destination (profile image destination path) or false on error
101 function create_profile_image_destination($id, $dir='user') {
106 if (!file_exists($CFG->dataroot
.'/'. $dir)) {
107 if (! mkdir($CFG->dataroot
.'/'. $dir, $CFG->directorypermissions
)) {
112 if ($dir == 'user') {
113 $destination = make_user_directory($id, true);
115 $destination = "$CFG->dataroot/$dir/$id";
118 if (!file_exists($destination)) {
119 if (!make_upload_directory(str_replace($CFG->dataroot
. '/', '', $destination))) {
127 * Given an upload manager with the right settings, this function performs a virus scan, and then scales and crops
128 * it and saves it in the right place to be a "user" or "group" image.
130 * @param int $id user or group id
131 * @param object $uploadmanager object referencing the image
132 * @param string $dir type of entity - groups, user, ...
133 * @return boolean success
135 function save_profile_image($id, $uploadmanager, $dir='user') {
137 if (!$uploadmanager) {
141 $destination = create_profile_image_destination($id, $dir);
142 if ($destination === false) {
146 if (!$uploadmanager->save_files($destination)) {
150 return process_profile_image($uploadmanager->get_new_filepath(), $destination);
154 * Given a path to an image file this function scales and crops it and saves it in
155 * the right place to be a "user" or "group" image.
158 * @param string $originalfile the path of the original image file
159 * @param string $destination the final destination directory of the profile image
162 function process_profile_image($originalfile, $destination) {
165 if(!(is_file($originalfile) && is_dir($destination))) {
169 if (empty($CFG->gdversion
)) {
173 $imageinfo = GetImageSize($originalfile);
175 if (empty($imageinfo)) {
176 if (file_exists($originalfile)) {
177 unlink($originalfile);
182 $image->width
= $imageinfo[0];
183 $image->height
= $imageinfo[1];
184 $image->type
= $imageinfo[2];
186 switch ($image->type
) {
188 if (function_exists('ImageCreateFromGIF')) {
189 $im = ImageCreateFromGIF($originalfile);
191 notice('GIF not supported on this server');
192 unlink($originalfile);
197 if (function_exists('ImageCreateFromJPEG')) {
198 $im = ImageCreateFromJPEG($originalfile);
200 notice('JPEG not supported on this server');
201 unlink($originalfile);
206 if (function_exists('ImageCreateFromPNG')) {
207 $im = ImageCreateFromPNG($originalfile);
209 notice('PNG not supported on this server');
210 unlink($originalfile);
215 unlink($originalfile);
219 unlink($originalfile);
221 if (function_exists('ImageCreateTrueColor') and $CFG->gdversion
>= 2) {
222 $im1 = ImageCreateTrueColor(100,100);
223 $im2 = ImageCreateTrueColor(35,35);
225 $im1 = ImageCreate(100,100);
226 $im2 = ImageCreate(35,35);
229 $cx = $image->width
/ 2;
230 $cy = $image->height
/ 2;
232 if ($image->width
< $image->height
) {
233 $half = floor($image->width
/ 2.0);
235 $half = floor($image->height
/ 2.0);
238 ImageCopyBicubic($im1, $im, 0, 0, $cx-$half, $cy-$half, 100, 100, $half*2, $half*2);
239 ImageCopyBicubic($im2, $im, 0, 0, $cx-$half, $cy-$half, 35, 35, $half*2, $half*2);
241 if (function_exists('ImageJpeg')) {
242 @touch
($destination .'/f1.jpg'); // Helps in Safe mode
243 @touch
($destination .'/f2.jpg'); // Helps in Safe mode
244 if (ImageJpeg($im1, $destination .'/f1.jpg', 90) and
245 ImageJpeg($im2, $destination .'/f2.jpg', 95) ) {
246 @chmod
($destination .'/f1.jpg', 0666);
247 @chmod
($destination .'/f2.jpg', 0666);
251 notify('PHP has not been configured to support JPEG images. Please correct this.');
257 * Given a user id this function scales and crops the user images to remove
258 * the one pixel black border.
261 * @param int $id description?
264 function upgrade_profile_image($id, $dir='users') {
267 $im = ImageCreateFromJPEG($CFG->dataroot
.'/'. $dir .'/'. $id .'/f1.jpg');
269 if (function_exists('ImageCreateTrueColor') and $CFG->gdversion
>= 2) {
270 $im1 = ImageCreateTrueColor(100,100);
271 $im2 = ImageCreateTrueColor(35,35);
273 $im1 = ImageCreate(100,100);
274 $im2 = ImageCreate(35,35);
277 if (function_exists('ImageCopyResampled') and $CFG->gdversion
>= 2) {
278 ImageCopyBicubic($im1, $im, 0, 0, 2, 2, 100, 100, 96, 96);
280 imagecopy($im1, $im, 0, 0, 0, 0, 100, 100);
281 $c = ImageColorsForIndex($im1,ImageColorAt($im1,2,2));
282 $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']);
283 ImageSetPixel ($im1, 0, 0, $color);
284 $c = ImageColorsForIndex($im1,ImageColorAt($im1,2,97));
285 $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']);
286 ImageSetPixel ($im1, 0, 99, $color);
287 $c = ImageColorsForIndex($im1,ImageColorAt($im1,97,2));
288 $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']);
289 ImageSetPixel ($im1, 99, 0, $color);
290 $c = ImageColorsForIndex($im1,ImageColorAt($im1,97,97));
291 $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']);
292 ImageSetPixel ($im1, 99, 99, $color);
293 for ($x = 1; $x < 99; $x++
) {
294 $c1 = ImageColorsForIndex($im1,ImageColorAt($im,$x,1));
295 $color = ImageColorClosest ($im, $c1['red'], $c1['green'], $c1['blue']);
296 ImageSetPixel ($im1, $x, 0, $color);
297 $c2 = ImageColorsForIndex($im1,ImageColorAt($im1,$x,98));
298 $color = ImageColorClosest ($im, $c2['red'], $c2['green'], $c2['blue']);
299 ImageSetPixel ($im1, $x, 99, $color);
301 for ($y = 1; $y < 99; $y++
) {
302 $c3 = ImageColorsForIndex($im1,ImageColorAt($im, 1, $y));
303 $color = ImageColorClosest ($im, $c3['red'], $c3['green'], $c3['blue']);
304 ImageSetPixel ($im1, 0, $y, $color);
305 $c4 = ImageColorsForIndex($im1,ImageColorAt($im1, 98, $y));
306 $color = ImageColorClosest ($im, $c4['red'], $c4['green'], $c4['blue']);
307 ImageSetPixel ($im1, 99, $y, $color);
310 ImageCopyBicubic($im2, $im, 0, 0, 2, 2, 35, 35, 96, 96);
312 if (function_exists('ImageJpeg')) {
313 if (ImageJpeg($im1, $CFG->dataroot
.'/'. $dir .'/'. $id .'/f1.jpg', 90) and
314 ImageJpeg($im2, $CFG->dataroot
.'/'. $dir .'/'. $id .'/f2.jpg', 95) ) {
315 @chmod
($CFG->dataroot
.'/'. $dir .'/'. $id .'/f1.jpg', 0666);
316 @chmod
($CFG->dataroot
.'/'. $dir .'/'. $id .'/f2.jpg', 0666);
320 notify('PHP has not been configured to support JPEG images. Please correct this.');