[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/lib/ -> gdlib.php (source)

   1  <?php
   2  
   3  /**
   4   * gdlib.php - Collection of routines in Moodle related to
   5   * processing images using GD
   6   *
   7   * @author Martin Dougiamas etc
   8   * @version  $Id: gdlib.php,v 1.14.2.3 2008/02/21 05:23:24 moodler Exp $
   9   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  10   * @package moodlecore
  11   */
  12  
  13  /**
  14   * short description (optional)
  15   *
  16   * long description
  17   * @uses $CFG
  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?
  28   * @return ?
  29   * @todo Finish documenting this function
  30   */
  31  function ImageCopyBicubic ($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) {
  32  
  33      global $CFG;
  34  
  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);
  38      }
  39  
  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']);
  44          }
  45      }
  46  
  47      $scaleX = ($src_w - 1) / $dst_w;
  48      $scaleY = ($src_h - 1) / $dst_h;
  49  
  50      $scaleX2 = $scaleX / 2.0;
  51      $scaleY2 = $scaleY / 2.0;
  52  
  53      for ($j = 0; $j < $dst_h; $j++) {
  54          $sY = $j * $scaleY;
  55  
  56          for ($i = 0; $i < $dst_w; $i++) {
  57              $sX = $i * $scaleX;
  58  
  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));
  63  
  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);
  67  
  68              $color = ImageColorClosest ($dst_img, $red, $green, $blue);
  69              ImageSetPixel ($dst_img, $i + $dst_x, $j + $dst_y, $color);
  70          }
  71      }
  72  }
  73  
  74  /**
  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
  79   */
  80  function delete_profile_image($id, $dir='users') {
  81      global $CFG;
  82  
  83      require_once $CFG->libdir.'/filelib.php';
  84      $location = $CFG->dataroot .'/'. $dir .'/'. $id;
  85  
  86      if (file_exists($location)) {
  87          return fulldelete($location);
  88      }
  89  
  90      return true;
  91  }
  92  
  93  /**
  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.
  96   *
  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
 100   */
 101  function create_profile_image_destination($id, $dir='user') {
 102      global $CFG;
 103  
 104      umask(0000);
 105  
 106      if (!file_exists($CFG->dataroot .'/'. $dir)) {
 107          if (! mkdir($CFG->dataroot .'/'. $dir, $CFG->directorypermissions)) {
 108              return false;
 109          }
 110      }
 111  
 112      if ($dir == 'user') {
 113          $destination = make_user_directory($id, true);
 114      } else {
 115          $destination = "$CFG->dataroot/$dir/$id";
 116      }
 117  
 118      if (!file_exists($destination)) {
 119          if (!make_upload_directory(str_replace($CFG->dataroot . '/', '', $destination))) {
 120              return false;
 121          }
 122      }
 123      return $destination;
 124  }
 125  
 126  /**
 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.
 129   *
 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
 134   */
 135  function save_profile_image($id, $uploadmanager, $dir='user') {
 136  
 137      if (!$uploadmanager) {
 138          return false;
 139      }
 140  
 141      $destination = create_profile_image_destination($id, $dir);
 142      if ($destination === false) {
 143          return false;
 144      }
 145  
 146      if (!$uploadmanager->save_files($destination)) {
 147          return false;
 148      }
 149  
 150      return process_profile_image($uploadmanager->get_new_filepath(), $destination);
 151  }
 152  
 153  /**
 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.
 156   *
 157   * @uses $CFG
 158   * @param string $originalfile the path of the original image file
 159   * @param string $destination the final destination directory of the profile image
 160   * @return boolean
 161   */
 162  function process_profile_image($originalfile, $destination) {
 163      global $CFG;
 164  
 165      if(!(is_file($originalfile) && is_dir($destination))) {
 166          return false;
 167      }
 168  
 169      if (empty($CFG->gdversion)) {
 170          return false;
 171      }
 172  
 173      $imageinfo = GetImageSize($originalfile);
 174  
 175      if (empty($imageinfo)) {
 176          if (file_exists($originalfile)) {
 177              unlink($originalfile);
 178          }
 179          return false;
 180      }
 181  
 182      $image->width  = $imageinfo[0];
 183      $image->height = $imageinfo[1];
 184      $image->type   = $imageinfo[2];
 185  
 186      switch ($image->type) {
 187          case IMAGETYPE_GIF:
 188              if (function_exists('ImageCreateFromGIF')) {
 189                  $im = ImageCreateFromGIF($originalfile);
 190              } else {
 191                  notice('GIF not supported on this server');
 192                  unlink($originalfile);
 193                  return false;
 194              }
 195              break;
 196          case IMAGETYPE_JPEG:
 197              if (function_exists('ImageCreateFromJPEG')) {
 198                  $im = ImageCreateFromJPEG($originalfile);
 199              } else {
 200                  notice('JPEG not supported on this server');
 201                  unlink($originalfile);
 202                  return false;
 203              }
 204              break;
 205          case IMAGETYPE_PNG:
 206              if (function_exists('ImageCreateFromPNG')) {
 207                  $im = ImageCreateFromPNG($originalfile);
 208              } else {
 209                  notice('PNG not supported on this server');
 210                  unlink($originalfile);
 211                  return false;
 212              }
 213              break;
 214          default:
 215              unlink($originalfile);
 216              return false;
 217      }
 218  
 219      unlink($originalfile);
 220  
 221      if (function_exists('ImageCreateTrueColor') and $CFG->gdversion >= 2) {
 222          $im1 = ImageCreateTrueColor(100,100);
 223          $im2 = ImageCreateTrueColor(35,35);
 224      } else {
 225          $im1 = ImageCreate(100,100);
 226          $im2 = ImageCreate(35,35);
 227      }
 228  
 229      $cx = $image->width / 2;
 230      $cy = $image->height / 2;
 231  
 232      if ($image->width < $image->height) {
 233          $half = floor($image->width / 2.0);
 234      } else {
 235          $half = floor($image->height / 2.0);
 236      }
 237  
 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);
 240  
 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);
 248              return 1;
 249          }
 250      } else {
 251          notify('PHP has not been configured to support JPEG images.  Please correct this.');
 252      }
 253      return 0;
 254  }
 255  
 256  /**
 257   * Given a user id this function scales and crops the user images to remove
 258   * the one pixel black border.
 259   *
 260   * @uses $CFG
 261   * @param int $id description?
 262   * @return boolean
 263   */
 264  function upgrade_profile_image($id, $dir='users') {
 265      global $CFG;
 266  
 267      $im = ImageCreateFromJPEG($CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg');
 268  
 269      if (function_exists('ImageCreateTrueColor') and $CFG->gdversion >= 2) {
 270          $im1 = ImageCreateTrueColor(100,100);
 271          $im2 = ImageCreateTrueColor(35,35);
 272      } else {
 273          $im1 = ImageCreate(100,100);
 274          $im2 = ImageCreate(35,35);
 275      }
 276  
 277      if (function_exists('ImageCopyResampled') and $CFG->gdversion >= 2) {
 278          ImageCopyBicubic($im1, $im, 0, 0, 2, 2, 100, 100, 96, 96);
 279      } else {
 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);
 300          }
 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);
 308          }
 309      }
 310      ImageCopyBicubic($im2, $im, 0, 0, 2, 2, 35, 35, 96, 96);
 311  
 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);
 317              return 1;
 318          }
 319      } else {
 320          notify('PHP has not been configured to support JPEG images.  Please correct this.');
 321      }
 322      return 0;
 323  }
 324  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7