Example #1
0
 public function PictureFile($thumb = false, $newpic = null)
 {
     if (!$this->ID) {
         fail("Can't set profile picture when this user doesn't have an ID.");
     }
     $path = "uploads";
     // Path to store the uploaded pics relative to doc root
     if ($newpic) {
         $filename = $newpic['name'];
         $tmppath = $newpic['tmp_name'];
         $type = $newpic['type'];
         $size = $newpic['size'];
         if (!$tmppath) {
             fail("That picture file ({$filename}) was probably too large; try a smaller one, or report this error to have it resolved.");
         }
         // Get extension (excluding the '.')
         $ext = extension($filename, "jpg");
         // Extension and filetype valid?
         if (strtolower($ext) != "jpg" && strtolower($ext) != "jpeg" || $type != "image/jpeg") {
             fail("You are only allowed to upload JPG files, nothing else. You tried to upload a {$type} file ({$filename}).");
         }
         // Random number to append to filename (helps with browser caching issues)
         $rnd = rand(1000, 9999);
         $newpath = DOCROOT . "/{$path}/";
         $newfilename = "{$this->FirstName}_{$this->LastName}_{$this->ID}_{$rnd}.{$ext}";
         $newfilenameThumb = "{$this->FirstName}_{$this->LastName}_{$this->ID}_{$rnd}_thumb.{$ext}";
         $newfilenameMedium = "{$this->FirstName}_{$this->LastName}_{$this->ID}_{$rnd}_med.{$ext}";
         // Make filenames safe
         $newfilename = preg_replace('/[^a-zA-Z0-9_.()]/', '', $newfilename);
         $newfilenameThumb = preg_replace('/[^a-zA-Z0-9_.()]/', '', $newfilenameThumb);
         $newfilenameMedium = preg_replace('/[^a-zA-Z0-9_.()]/', '', $newfilenameMedium);
         // Delete old files, since the filename will probably be different
         $this->DeletePictureFile();
         // Create and save new profile pictures.
         create_jpgthumb($tmppath, $newpath . $newfilenameMedium, Member::MEDIUM_DIM, Member::MEDIUM_DIM, 90);
         // For display on site
         create_jpgthumb($tmppath, $newpath . $newfilenameThumb, Member::THUMB_DIM, Member::THUMB_DIM, 90, false);
         // Thumbnail
         move_uploaded_file($tmppath, $newpath . $newfilename);
         // Save database row
         $this->PictureFile = $newfilename;
         $this->Save();
     } else {
         if (!$this->PictureFile) {
             if ($this->Gender == Gender::Male) {
                 return "/resources/images/brother.png";
             } else {
                 return "/resources/images/sister.png";
             }
         }
         $main = filename($this->PictureFile);
         $ext = extension($this->PictureFile, "jpg");
         return "/{$path}/" . ($thumb ? $main . "_thumb." . $ext : $main . '_med.' . $ext);
     }
 }
Example #2
0
// SAFETY LINE; disable to use this file
require_once "../lib/init.php";
echo "<pre>";
set_time_limit(0);
$mems = DB::Run("SELECT ID FROM Members ORDER BY ID ASC");
$i = 0;
while ($row = mysql_fetch_array($mems)) {
    $mem = Member::Load($row['ID']);
    if (!$mem->PictureFile) {
        continue;
    }
    $picFile = $mem->PictureFile;
    $main = filename($mem->PictureFile);
    $ext = extension($mem->PictureFile, "jpg");
    $newRand = rand(1000, 9999);
    $newMain = $mem->FirstName . "_" . $mem->LastName . "_" . $mem->ID() . "_" . $newRand;
    $newFull = $newMain . "." . $ext;
    $newMedium = $newMain . "_med." . $ext;
    $newThumb = $newMain . "_thumb." . $ext;
    echo "PICTURE:\n{$newMain}\n{$newFull}\n{$newMedium}\n{$newThumb}\n";
    copy("uploads/" . $mem->PictureFile, "uploads/" . $newFull);
    create_jpgthumb("uploads/" . $mem->PictureFile, "uploads/" . $newMedium, Member::MEDIUM_DIM, Member::MEDIUM_DIM, 85, true);
    create_jpgthumb("uploads/" . $mem->PictureFile, "uploads/" . $newThumb, Member::THUMB_DIM, Member::THUMB_DIM, 70, false);
    $mem->DeletePictureFile();
    $mem->PictureFile = $newFull;
    $mem->Save();
    $i++;
    echo "DONE.\n\n";
}
echo "Updated {$i} members' pictures.\n";
echo "</pre>";