Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/gdlib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00027 defined('MOODLE_INTERNAL') || die();
00028 
00046 function ImageCopyBicubic($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) {
00047 
00048     global $CFG;
00049 
00050     if (function_exists('ImageCopyResampled') and $CFG->gdversion >= 2) {
00051        return ImageCopyResampled($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y,
00052                                  $dst_w, $dst_h, $src_w, $src_h);
00053     }
00054 
00055     $totalcolors = imagecolorstotal($src_img);
00056     for ($i=0; $i<$totalcolors; $i++) {
00057         if ($colors = ImageColorsForIndex($src_img, $i)) {
00058             ImageColorAllocate($dst_img, $colors['red'], $colors['green'], $colors['blue']);
00059         }
00060     }
00061 
00062     $scaleX = ($src_w - 1) / $dst_w;
00063     $scaleY = ($src_h - 1) / $dst_h;
00064 
00065     $scaleX2 = $scaleX / 2.0;
00066     $scaleY2 = $scaleY / 2.0;
00067 
00068     for ($j = 0; $j < $dst_h; $j++) {
00069         $sY = $j * $scaleY;
00070 
00071         for ($i = 0; $i < $dst_w; $i++) {
00072             $sX = $i * $scaleX;
00073 
00074             $c1 = ImageColorsForIndex($src_img,ImageColorAt($src_img,(int)$sX,(int)$sY+$scaleY2));
00075             $c2 = ImageColorsForIndex($src_img,ImageColorAt($src_img,(int)$sX,(int)$sY));
00076             $c3 = ImageColorsForIndex($src_img,ImageColorAt($src_img,(int)$sX+$scaleX2,(int)$sY+$scaleY2));
00077             $c4 = ImageColorsForIndex($src_img,ImageColorAt($src_img,(int)$sX+$scaleX2,(int)$sY));
00078 
00079             $red = (int) (($c1['red'] + $c2['red'] + $c3['red'] + $c4['red']) / 4);
00080             $green = (int) (($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) / 4);
00081             $blue = (int) (($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue']) / 4);
00082 
00083             $color = ImageColorClosest ($dst_img, $red, $green, $blue);
00084             ImageSetPixel ($dst_img, $i + $dst_x, $j + $dst_y, $color);
00085         }
00086     }
00087 }
00088 
00098 function process_new_icon($context, $component, $filearea, $itemid, $originalfile) {
00099     global $CFG;
00100 
00101     if (empty($CFG->gdversion)) {
00102         return false;
00103     }
00104 
00105     if (!is_file($originalfile)) {
00106         return false;
00107     }
00108 
00109     $imageinfo = GetImageSize($originalfile);
00110 
00111     if (empty($imageinfo)) {
00112         return false;
00113     }
00114 
00115     $image = new stdClass();
00116     $image->width  = $imageinfo[0];
00117     $image->height = $imageinfo[1];
00118     $image->type   = $imageinfo[2];
00119 
00120     switch ($image->type) {
00121         case IMAGETYPE_GIF:
00122             if (function_exists('ImageCreateFromGIF')) {
00123                 $im = ImageCreateFromGIF($originalfile);
00124             } else {
00125                 debugging('GIF not supported on this server');
00126                 return false;
00127             }
00128             break;
00129         case IMAGETYPE_JPEG:
00130             if (function_exists('ImageCreateFromJPEG')) {
00131                 $im = ImageCreateFromJPEG($originalfile);
00132             } else {
00133                 debugging('JPEG not supported on this server');
00134                 return false;
00135             }
00136             break;
00137         case IMAGETYPE_PNG:
00138             if (function_exists('ImageCreateFromPNG')) {
00139                 $im = ImageCreateFromPNG($originalfile);
00140             } else {
00141                 debugging('PNG not supported on this server');
00142                 return false;
00143             }
00144             break;
00145         default:
00146             return false;
00147     }
00148 
00149     if (function_exists('ImagePng')) {
00150         $imagefnc = 'ImagePng';
00151         $imageext = '.png';
00152         $filters = PNG_NO_FILTER;
00153         $quality = 1;
00154     } else if (function_exists('ImageJpeg')) {
00155         $imagefnc = 'ImageJpeg';
00156         $imageext = '.jpg';
00157         $filters = null; // not used
00158         $quality = 90;
00159     } else {
00160         debugging('Jpeg and png not supported on this server, please fix server configuration');
00161         return false;
00162     }
00163 
00164     if (function_exists('ImageCreateTrueColor') and $CFG->gdversion >= 2) {
00165         $im1 = ImageCreateTrueColor(100,100);
00166         $im2 = ImageCreateTrueColor(35,35);
00167         if ($image->type == IMAGETYPE_PNG and $imagefnc === 'ImagePng') {
00168             imagealphablending($im1, false);
00169             $color = imagecolorallocatealpha($im1, 0, 0,  0, 127);
00170             imagefill($im1, 0, 0,  $color);
00171             imagesavealpha($im1, true);
00172             imagealphablending($im2, false);
00173             $color = imagecolorallocatealpha($im2, 0, 0,  0, 127);
00174             imagefill($im2, 0, 0,  $color);
00175             imagesavealpha($im2, true);
00176         }
00177     } else {
00178         $im1 = ImageCreate(100,100);
00179         $im2 = ImageCreate(35,35);
00180     }
00181 
00182     $cx = $image->width / 2;
00183     $cy = $image->height / 2;
00184 
00185     if ($image->width < $image->height) {
00186         $half = floor($image->width / 2.0);
00187     } else {
00188         $half = floor($image->height / 2.0);
00189     }
00190 
00191     ImageCopyBicubic($im1, $im, 0, 0, $cx-$half, $cy-$half, 100, 100, $half*2, $half*2);
00192     ImageCopyBicubic($im2, $im, 0, 0, $cx-$half, $cy-$half, 35, 35, $half*2, $half*2);
00193 
00194     $fs = get_file_storage();
00195 
00196     $icon = array('contextid'=>$context->id, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>'/');
00197 
00198     ob_start();
00199     if (!$imagefnc($im1, NULL, $quality, $filters)) {
00200         // keep old icons
00201         ob_end_clean();
00202         return false;
00203     }
00204     $data = ob_get_clean();
00205     ImageDestroy($im1);
00206     $icon['filename'] = 'f1'.$imageext;
00207     $fs->delete_area_files($context->id, $component, $filearea, $itemid);
00208     $fs->create_file_from_string($icon, $data);
00209 
00210     ob_start();
00211     if (!$imagefnc($im2, NULL, $quality, $filters)) {
00212         ob_end_clean();
00213         $fs->delete_area_files($context->id, $component, $filearea, $itemid);
00214         return false;
00215     }
00216     $data = ob_get_clean();
00217     ImageDestroy($im2);
00218     $icon['filename'] = 'f2'.$imageext;
00219     $fs->create_file_from_string($icon, $data);
00220 
00221     return true;
00222 }
00223 
 All Data Structures Namespaces Files Functions Variables Enumerations