Ejemplo n.º 1
0
 /**
  * Saves avatar to DB and outputs that as PNG
  * @param int $id
  */
 public function generate($id)
 {
     $uid = (int) $id;
     $user = User::get($uid);
     $initial = strtoupper($user->username[0]);
     $bI = rand(0, 41);
     $color = $this->BACKGROUND_COLORS[$bI];
     $rgb = $this->hex2rgb($color);
     // Create a 300x100 image
     $im = imagecreatetruecolor(128, 128);
     $red = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);
     $black = imagecolorallocate($im, 0xff, 0xff, 0xff);
     // Make the background red
     imagefilledrectangle($im, 0, 0, 128, 128, $red);
     // Path to our ttf font file
     $font_file = ASSET_DIR . 'img/general/SourceCodePro-Light.ttf';
     // Draw the text 'PHP Manual' using font size 13
     imagefttext($im, 80, 0, 32, 100, $black, $font_file, $initial);
     // Output image to the browser
     header('Content-Type: image/png');
     $name = $initial . '_' . str_replace('#', '', $color) . '.png';
     imagepng($im, AVATAR_PATH . $name);
     $resizer = new \Ext\ImageResize();
     $iconW = 36;
     $iconH = 36;
     $iconPath = AVATAR_PATH . 'icons/' . $name;
     $success = $resizer->smart_resize_image(AVATAR_PATH . $name, NULL, $iconW, $iconH, true, $iconPath, FALSE, FALSE, 75);
     if (!$success) {
         @copy(AVATAR_PATH . $name, $iconPath);
     }
     $user->set(array('avatar' => $name));
     imagepng($im);
     imagedestroy($im);
 }
Ejemplo n.º 2
0
 /**
  * Save an uploaded file to a new location. If no filename is provided,
  * the original filename will be used, with a unique prefix added.
  *
  * This method should be used after validating the $_FILES array:
  *
  *     if ($array->check())
  *     {
  *         // Upload is valid, save it
  *         Upload::save($array['file']);
  *     }
  *
  * @param   array   $file       uploaded file data
  * @param   string  $filename   new filename
  * @param   string  $directory  new directory
  * @param   integer $chmod      chmod mask
  * @return  string  on success, full path to new file
  * @return  FALSE   on failure
  */
 public static function save(array $file, $filename = NULL, $directory = NULL, $chmod = 0644)
 {
     if (!isset($file['tmp_name']) or !is_uploaded_file($file['tmp_name'])) {
         // Ignore corrupted uploads
         return FALSE;
     }
     if ($filename === NULL) {
         // Use the default filename, with a timestamp pre-pended
         $filename = uniqid() . $file['name'];
     }
     if (Upload::$remove_spaces === TRUE) {
         // Remove spaces from the filename
         $filename = preg_replace('/\\s+/u', '_', $filename);
     }
     if ($directory === NULL) {
         // Use the pre-configured upload directory
         $directory = Upload::$default_directory;
     }
     if (!is_dir($directory) or !is_writable(realpath($directory))) {
         Log::error("Upload directory not writable");
     }
     $resizer = new \Ext\ImageResize();
     // Make the filename into a complete path
     $file_info["name"] = $filename;
     $filename = realpath($directory) . DIRECTORY_SEPARATOR . $filename;
     $file_info["path"] = $filename;
     if (self::image($file) && self::$resizeImage) {
         $success = $resizer->smart_resize_image($file['tmp_name'], NULL, self::$width, self::$height, true, $filename, FALSE, FALSE, 75);
         if (self::$resizeIconPath != '') {
             $w = $h = self::$resizeIconSize;
             $iconPath = self::$resizeIconPath . $file_info["name"];
             $success = $resizer->smart_resize_image($file['tmp_name'], NULL, $w, $h, true, $iconPath, FALSE, FALSE, 75);
         }
     } else {
         $success = move_uploaded_file($file['tmp_name'], $filename);
         if (self::$resizeIconPath != '') {
             @copy($filename, self::$resizeIconPath . $file_info["name"]);
         }
     }
     if ($success) {
         if ($chmod !== FALSE) {
             // Set permissions on filename
             chmod($filename, $chmod);
         }
         // Return new file path
         return $file_info;
     }
     return FALSE;
 }