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 * Given an upload manager with the right settings, this function performs a virus scan, and then scales and crops
76 * it and saves it in the right place to be a "user" or "group" image.
79 * @param int $id description?
80 * @param object $uploadmanager description?
81 * @param string $dir description?
83 * @todo Finish documenting this function
85 function save_profile_image($id, $uploadmanager, $dir='users') {
90 if (empty($CFG->gdversion
)) {
94 if (!$uploadmanager) {
100 if (!file_exists($CFG->dataroot
.'/'. $dir)) {
101 if (! mkdir($CFG->dataroot
.'/'. $dir, $CFG->directorypermissions
)) {
106 if (!file_exists($CFG->dataroot
.'/'. $dir .'/'. $id)) {
107 if (! mkdir($CFG->dataroot
.'/'. $dir .'/'. $id, $CFG->directorypermissions
)) {
112 $destination = $CFG->dataroot
.'/'. $dir .'/'. $id;
113 if (!$uploadmanager->save_files($destination)) {
117 $originalfile = $uploadmanager->get_new_filepath();
119 $imageinfo = GetImageSize($originalfile);
121 if (empty($imageinfo)) {
122 if (file_exists($originalfile)) {
123 unlink($originalfile);
128 $image->width
= $imageinfo[0];
129 $image->height
= $imageinfo[1];
130 $image->type
= $imageinfo[2];
132 switch ($image->type
) {
134 if (function_exists('ImageCreateFromGIF')) {
135 $im = ImageCreateFromGIF($originalfile);
137 notice('GIF not supported on this server');
138 unlink($originalfile);
143 if (function_exists('ImageCreateFromJPEG')) {
144 $im = ImageCreateFromJPEG($originalfile);
146 notice('JPEG not supported on this server');
147 unlink($originalfile);
152 if (function_exists('ImageCreateFromPNG')) {
153 $im = ImageCreateFromPNG($originalfile);
155 notice('PNG not supported on this server');
156 unlink($originalfile);
161 unlink($originalfile);
165 unlink($originalfile);
167 if (function_exists('ImageCreateTrueColor') and $CFG->gdversion
>= 2) {
168 $im1 = ImageCreateTrueColor(100,100);
169 $im2 = ImageCreateTrueColor(35,35);
171 $im1 = ImageCreate(100,100);
172 $im2 = ImageCreate(35,35);
175 $cx = $image->width
/ 2;
176 $cy = $image->height
/ 2;
178 if ($image->width
< $image->height
) {
179 $half = floor($image->width
/ 2.0);
181 $half = floor($image->height
/ 2.0);
184 ImageCopyBicubic($im1, $im, 0, 0, $cx-$half, $cy-$half, 100, 100, $half*2, $half*2);
185 ImageCopyBicubic($im2, $im, 0, 0, $cx-$half, $cy-$half, 35, 35, $half*2, $half*2);
187 if (function_exists('ImageJpeg')) {
188 @touch
($CFG->dataroot
.'/'. $dir .'/'. $id .'/f1.jpg'); // Helps in Safe mode
189 @touch
($CFG->dataroot
.'/'. $dir .'/'. $id .'/f2.jpg'); // Helps in Safe mode
190 if (ImageJpeg($im1, $CFG->dataroot
.'/'. $dir .'/'. $id .'/f1.jpg', 90) and
191 ImageJpeg($im2, $CFG->dataroot
.'/'. $dir .'/'. $id .'/f2.jpg', 95) ) {
192 @chmod
($CFG->dataroot
.'/'. $dir .'/'. $id .'/f1.jpg', 0666);
193 @chmod
($CFG->dataroot
.'/'. $dir .'/'. $id .'/f2.jpg', 0666);
197 notify('PHP has not been configured to support JPEG images. Please correct this.');
203 * Given a user id this function scales and crops the user images to remove
204 * the one pixel black border.
207 * @param int $id description?
210 function upgrade_profile_image($id, $dir='users') {
213 $im = ImageCreateFromJPEG($CFG->dataroot
.'/'. $dir .'/'. $id .'/f1.jpg');
215 if (function_exists('ImageCreateTrueColor') and $CFG->gdversion
>= 2) {
216 $im1 = ImageCreateTrueColor(100,100);
217 $im2 = ImageCreateTrueColor(35,35);
219 $im1 = ImageCreate(100,100);
220 $im2 = ImageCreate(35,35);
223 if (function_exists('ImageCopyResampled') and $CFG->gdversion
>= 2) {
224 ImageCopyBicubic($im1, $im, 0, 0, 2, 2, 100, 100, 96, 96);
226 imagecopy($im1, $im, 0, 0, 0, 0, 100, 100);
227 $c = ImageColorsForIndex($im1,ImageColorAt($im1,2,2));
228 $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']);
229 ImageSetPixel ($im1, 0, 0, $color);
230 $c = ImageColorsForIndex($im1,ImageColorAt($im1,2,97));
231 $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']);
232 ImageSetPixel ($im1, 0, 99, $color);
233 $c = ImageColorsForIndex($im1,ImageColorAt($im1,97,2));
234 $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']);
235 ImageSetPixel ($im1, 99, 0, $color);
236 $c = ImageColorsForIndex($im1,ImageColorAt($im1,97,97));
237 $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']);
238 ImageSetPixel ($im1, 99, 99, $color);
239 for ($x = 1; $x < 99; $x++
) {
240 $c1 = ImageColorsForIndex($im1,ImageColorAt($im,$x,1));
241 $color = ImageColorClosest ($im, $c1['red'], $c1['green'], $c1['blue']);
242 ImageSetPixel ($im1, $x, 0, $color);
243 $c2 = ImageColorsForIndex($im1,ImageColorAt($im1,$x,98));
244 $color = ImageColorClosest ($im1, $red, $green, $blue);
245 $color = ImageColorClosest ($im, $c2['red'], $c2['green'], $c2['blue']);
246 ImageSetPixel ($im1, $x, 99, $color);
248 for ($y = 1; $y < 99; $y++
) {
249 $c3 = ImageColorsForIndex($im1,ImageColorAt($im, 1, $y));
250 $color = ImageColorClosest ($im, $red, $green, $blue);
251 $color = ImageColorClosest ($im, $c3['red'], $c3['green'], $c3['blue']);
252 ImageSetPixel ($im1, 0, $y, $color);
253 $c4 = ImageColorsForIndex($im1,ImageColorAt($im1, 98, $y));
254 $color = ImageColorClosest ($im, $c4['red'], $c4['green'], $c4['blue']);
255 ImageSetPixel ($im1, 99, $y, $color);
258 ImageCopyBicubic($im2, $im, 0, 0, 2, 2, 35, 35, 96, 96);
260 if (function_exists('ImageJpeg')) {
261 if (ImageJpeg($im1, $CFG->dataroot
.'/'. $dir .'/'. $id .'/f1.jpg', 90) and
262 ImageJpeg($im2, $CFG->dataroot
.'/'. $dir .'/'. $id .'/f2.jpg', 95) ) {
263 @chmod
($CFG->dataroot
.'/'. $dir .'/'. $id .'/f1.jpg', 0666);
264 @chmod
($CFG->dataroot
.'/'. $dir .'/'. $id .'/f2.jpg', 0666);
268 notify('PHP has not been configured to support JPEG images. Please correct this.');