Ejemplo n.º 1
0
 public static function uploadUserAvatar($avatar, $user_id = 0)
 {
     //try {
     if (($imageinfo = getimagesize($avatar)) !== false) {
         if (!file_exists(BASE_PATH . '/uploads/cache_avatars/' . $user_id) || !is_dir(BASE_PATH . '/uploads/cache_avatars/' . $user_id)) {
             @mkdir(BASE_PATH . '/uploads/cache_avatars/' . $user_id, 0777, true);
         }
         $ext = strtolower(strrchr($avatar, "."));
         if (!$ext) {
             $mime_ext = explode('/', $imageinfo['mime']);
             if (isset($mime_ext[1])) {
                 $ext = '.' . $mime_ext[1];
             }
         }
         $name = md5(time() . mt_rand()) . '_' . $user_id . $ext;
         if (@copy($avatar, BASE_PATH . '/uploads/cache_avatars/' . $user_id . '/' . $name)) {
             $added_date = time();
             if (is_array($user_info = Model_Users::getUser($user_id))) {
                 $added_date = $user_info['date_added'];
                 self::deleteUserImage($user_info);
             }
             $user_agent = ini_get('user_agent');
             ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9');
             $container = 'amatteur_users';
             $auth = new JO_Api_Rackspace_Authentication(JO_Registry::get('rsUsername'), JO_Registry::get('rsApiKey'));
             if ($auth->authenticate()) {
                 $conn = new JO_Api_Rackspace_Connection($auth);
                 $contaners = $conn->list_public_containers();
                 if (!in_array($container, $contaners)) {
                     $conn->create_container($container);
                     $contaners[] = $container;
                 }
                 if ($contaners && in_array($container, $contaners)) {
                     $images = $conn->get_container($container);
                     $images->make_public(86400 * 365);
                     $images = $conn->get_container($container);
                     if (!$images->cdn_uri) {
                         return false;
                     }
                     $object = $images->create_object($name);
                     $object->load_from_filename(BASE_PATH . '/uploads/cache_avatars/' . $user_id . '/' . $name);
                     $image_info = $images->get_object($name);
                     if (!$image_info->name) {
                         return false;
                     }
                     $model_images = new Helper_Images();
                     $temp_width = 0;
                     $temp_height = 0;
                     $sizes = self::userThumbSizes();
                     if ($sizes) {
                         foreach ($sizes as $size => $prefix) {
                             $sizes = explode('x', $size);
                             $width = (int) isset($sizes[0]) ? $sizes[0] : 0;
                             $height = (int) isset($sizes[1]) ? $sizes[1] : 0;
                             $name_pref = basename($name, $ext) . $prefix . $ext;
                             if ($width && $height) {
                                 $thumb_a = $model_images->resize('/cache_avatars/' . $user_id . '/' . $name, $width, $height, true);
                             } else {
                                 if ($width && !$height) {
                                     $thumb_a = $model_images->resizeWidth('/cache_avatars/' . $user_id . '/' . $name, $width);
                                 } else {
                                     if ($height && !$width) {
                                         $thumb_a = $model_images->resizeHeight('/cache_avatars/' . $user_id . '/' . $name, $height);
                                     }
                                 }
                             }
                             if ($prefix == '_B') {
                                 $temp_width = $model_images->getSizes('width');
                                 $temp_height = $model_images->getSizes('height');
                             }
                             $thumb_a1 = explode('/uploads/', $thumb_a);
                             if ($thumb_a1 && isset($thumb_a1[1])) {
                                 $object = $images->create_object($name_pref);
                                 $object->load_from_filename(BASE_PATH . '/uploads/' . $thumb_a1[1]);
                             }
                         }
                     }
                     self::recursiveDelete(BASE_PATH . '/uploads/cache_avatars/' . $user_id . '/');
                     self::recursiveDelete(BASE_PATH . '/uploads/cache/cache_avatars/' . $user_id . '/');
                     if ($temp_width) {
                         return array('store' => 'rackspace', 'image' => $images->cdn_uri . '/' . $image_info->name, 'width' => $temp_width, 'height' => $temp_height);
                     }
                 }
             }
         }
     }
     return false;
     /*} catch (JO_Exception $e) {
     			return false;
     		}*/
     return false;
 }