4 * gdlib.php - Collection of routines in Moodle related to
5 * processing images using GD
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 object $uploadmanager object referencing the image
99 * @param string $dir type of entity - groups, users, ...
100 * @return boolean success
102 function save_profile_image($id, $uploadmanager, $dir='users') {
107 if (empty($CFG->gdversion
)) {
111 if (!$uploadmanager) {
117 if (!file_exists($CFG->dataroot
.'/'. $dir)) {
118 if (! mkdir($CFG->dataroot
.'/'. $dir, $CFG->directorypermissions
)) {
123 if (!file_exists($CFG->dataroot
.'/'. $dir .'/'. $id)) {
124 if (! mkdir($CFG->dataroot
.'/'. $dir .'/'. $id, $CFG->directorypermissions
)) {
129 $destination = $CFG->dataroot
.'/'. $dir .'/'. $id;
130 if (!$uploadmanager->save_files($destination)) {
134 $originalfile = $uploadmanager->get_new_filepath();
136 $imageinfo = GetImageSize($originalfile);
138 if (empty($imageinfo)) {
139 if (file_exists($originalfile)) {
140 unlink($originalfile);
145 $image->width
= $imageinfo[0];
146 $image->height
= $imageinfo[1];
147 $image->type
= $imageinfo[2];
149 switch ($image->type
) {
151 if (function_exists('ImageCreateFromGIF')) {
152 $im = ImageCreateFromGIF($originalfile);
154 notice('GIF not supported on this server');
155 unlink($originalfile);
160 if (function_exists('ImageCreateFromJPEG')) {
161 $im = ImageCreateFromJPEG($originalfile);
163 notice('JPEG not supported on this server');
164 unlink($originalfile);
169 if (function_exists('ImageCreateFromPNG')) {
170 $im = ImageCreateFromPNG($originalfile);
172 notice('PNG not supported on this server');
173 unlink($originalfile);
178 unlink($originalfile);
182 unlink($originalfile);
184 if (function_exists('ImageCreateTrueColor') and $CFG->gdversion
>= 2) {
185 $im1 = ImageCreateTrueColor(100,100);
186 $im2 = ImageCreateTrueColor(35,35);
188 $im1 = ImageCreate(100,100);
189 $im2 = ImageCreate(35,35);
192 $cx = $image->width
/ 2;
193 $cy = $image->height
/ 2;
195 if ($image->width
< $image->height
) {
196 $half = floor($image->width
/ 2.0);
198 $half = floor($image->height
/ 2.0);
201 ImageCopyBicubic($im1, $im, 0, 0, $cx-$half, $cy-$half, 100, 100, $half*2, $half*2);
202 ImageCopyBicubic($im2, $im, 0, 0, $cx-$half, $cy-$half, 35, 35, $half*2, $half*2);
204 if (function_exists('ImageJpeg')) {
205 @touch
($CFG->dataroot
.'/'. $dir .'/'. $id .'/f1.jpg'); // Helps in Safe mode
206 @touch
($CFG->dataroot
.'/'. $dir .'/'. $id .'/f2.jpg'); // Helps in Safe mode
207 if (ImageJpeg($im1, $CFG->dataroot
.'/'. $dir .'/'. $id .'/f1.jpg', 90) and
208 ImageJpeg($im2, $CFG->dataroot
.'/'. $dir .'/'. $id .'/f2.jpg', 95) ) {
209 @chmod
($CFG->dataroot
.'/'. $dir .'/'. $id .'/f1.jpg', 0666);
210 @chmod
($CFG->dataroot
.'/'. $dir .'/'. $id .'/f2.jpg', 0666);
214 notify('PHP has not been configured to support JPEG images. Please correct this.');
220 * Given a user id this function scales and crops the user images to remove
221 * the one pixel black border.
224 * @param int $id description?
227 function upgrade_profile_image($id, $dir='users') {
230 $im = ImageCreateFromJPEG($CFG->dataroot
.'/'. $dir .'/'. $id .'/f1.jpg');
232 if (function_exists('ImageCreateTrueColor') and $CFG->gdversion
>= 2) {
233 $im1 = ImageCreateTrueColor(100,100);
234 $im2 = ImageCreateTrueColor(35,35);
236 $im1 = ImageCreate(100,100);
237 $im2 = ImageCreate(35,35);
240 if (function_exists('ImageCopyResampled') and $CFG->gdversion
>= 2) {
241 ImageCopyBicubic($im1, $im, 0, 0, 2, 2, 100, 100, 96, 96);
243 imagecopy($im1, $im, 0, 0, 0, 0, 100, 100);
244 $c = ImageColorsForIndex($im1,ImageColorAt($im1,2,2));
245 $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']);
246 ImageSetPixel ($im1, 0, 0, $color);
247 $c = ImageColorsForIndex($im1,ImageColorAt($im1,2,97));
248 $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']);
249 ImageSetPixel ($im1, 0, 99, $color);
250 $c = ImageColorsForIndex($im1,ImageColorAt($im1,97,2));
251 $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']);
252 ImageSetPixel ($im1, 99, 0, $color);
253 $c = ImageColorsForIndex($im1,ImageColorAt($im1,97,97));
254 $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']);
255 ImageSetPixel ($im1, 99, 99, $color);
256 for ($x = 1; $x < 99; $x++
) {
257 $c1 = ImageColorsForIndex($im1,ImageColorAt($im,$x,1));
258 $color = ImageColorClosest ($im, $c1['red'], $c1['green'], $c1['blue']);
259 ImageSetPixel ($im1, $x, 0, $color);
260 $c2 = ImageColorsForIndex($im1,ImageColorAt($im1,$x,98));
261 $color = ImageColorClosest ($im, $c2['red'], $c2['green'], $c2['blue']);
262 ImageSetPixel ($im1, $x, 99, $color);
264 for ($y = 1; $y < 99; $y++
) {
265 $c3 = ImageColorsForIndex($im1,ImageColorAt($im, 1, $y));
266 $color = ImageColorClosest ($im, $c3['red'], $c3['green'], $c3['blue']);
267 ImageSetPixel ($im1, 0, $y, $color);
268 $c4 = ImageColorsForIndex($im1,ImageColorAt($im1, 98, $y));
269 $color = ImageColorClosest ($im, $c4['red'], $c4['green'], $c4['blue']);
270 ImageSetPixel ($im1, 99, $y, $color);
273 ImageCopyBicubic($im2, $im, 0, 0, 2, 2, 35, 35, 96, 96);
275 if (function_exists('ImageJpeg')) {
276 if (ImageJpeg($im1, $CFG->dataroot
.'/'. $dir .'/'. $id .'/f1.jpg', 90) and
277 ImageJpeg($im2, $CFG->dataroot
.'/'. $dir .'/'. $id .'/f2.jpg', 95) ) {
278 @chmod
($CFG->dataroot
.'/'. $dir .'/'. $id .'/f1.jpg', 0666);
279 @chmod
($CFG->dataroot
.'/'. $dir .'/'. $id .'/f2.jpg', 0666);
283 notify('PHP has not been configured to support JPEG images. Please correct this.');