Exemplo n.º 1
0
 public function updateProfilePicture()
 {
     $user = $this->model->where('id', '=', $_POST['user_id'])->first();
     $now = \Carbon\Carbon::instance($user->created_at);
     $types = array('_o.', '_s.', '_m.', '_l.');
     $sizes = array(100, 300, 600);
     $target_path = base_path() . '/public/imgs/';
     $file = \Input::file('image');
     $hashSeed = "user" . $user->id . $file->getClientOriginalName();
     while (1) {
         $file_name = \Func::getHashedValue($hashSeed);
         if (!\File::exists($target_path . \Func::getImgPath($file_name))) {
             break;
         }
         $hashSeed .= rand();
     }
     $target_path .= \Func::getImgPath($file_name) . '/';
     if (!\File::exists($target_path)) {
         \File::makeDirectory($target_path, 0775, true);
     }
     $file_ext = $file->getClientOriginalExtension();
     $original = $file_name . array_shift($types) . $file_ext;
     $file->move($target_path, $original);
     // Move the original one first
     foreach ($types as $key => $type) {
         $new_name = $file_name . $type . $file_ext;
         \File::copy($target_path . $original, $target_path . $new_name);
         \Image::make($target_path . $new_name)->resize($sizes[$key], null, function ($constraint) {
             $constraint->aspectRatio();
             $constraint->upsize();
         })->save($target_path . $new_name);
     }
     \File::delete($target_path . $original);
     $user->profile_img_path = $file_name . "." . $file_ext;
     $user->save();
     $this->common_func->storeActivityLogWithUser('update_user_profile_picture', $user->id, 'user', $_POST['user_id']);
     return $user;
 }
Exemplo n.º 2
0
 public function storeAPI()
 {
     \DB::beginTransaction();
     try {
         // post save
         $post = $this->model->create(array('user_id' => $_POST['user_id'], 'content' => $_POST['captions']));
         if (!$post) {
             \DB::rollback();
             return;
         }
         $post_group = $this->post_group->where('post_groups.user_id', '=', $_POST['user_id'])->where('post_groups.name', '=', "Uncategorized")->first();
         $post->postGroups()->attach($post_group->id);
         $types = array('_o.', '_s.', '_m.', '_l.');
         $sizes = array(100, 300, 600);
         $target_path = base_path() . '/public/imgs/';
         $file = \Input::file('image');
         $hashSeed = "post" . $post->id . $file->getClientOriginalName();
         while (1) {
             $file_name = \Func::getHashedValue($hashSeed);
             if (!\File::exists($target_path . \Func::getImgPath($file_name))) {
                 break;
             }
             $hashSeed .= rand();
         }
         $target_path .= \Func::getImgPath($file_name) . '/';
         if (!\File::exists($target_path)) {
             \File::makeDirectory($target_path, 0775, true);
         }
         $file_ext = $file->getClientOriginalExtension();
         $original = $file_name . array_shift($types) . $file_ext;
         $file->move($target_path, $original);
         // Move the original one first
         foreach ($types as $key => $type) {
             $new_name = $file_name . $type . $file_ext;
             \File::copy($target_path . $original, $target_path . $new_name);
             \Image::make($target_path . $new_name)->resize($sizes[$key], null, function ($constraint) {
                 $constraint->aspectRatio();
                 $constraint->upsize();
             })->save($target_path . $new_name);
         }
         \File::delete($target_path . $original);
         $photo = $this->photo->create(array('post_id' => $post->id, 'sequence' => 1, 'img_path' => $file_name . '.' . $file_ext));
         if (!$photo) {
             \DB::rollback();
             return;
         }
         // hash tag save
         $temp = $_POST['captions'];
         preg_match_all('/#(\\w+)/', $temp, $matches);
         $hashtags = $matches[1];
         foreach ($hashtags as $hashtag) {
             $tag = $this->tag->firstOrCreate(['name' => $hashtag]);
             $post->Tags()->attach($tag->id);
         }
         $this->common_func->storeActivityLogWithUser('store_post', $post->id, 'post', $_POST['user_id']);
     } catch (ValidationException $e) {
         \DB::rollback();
     } catch (\Exception $e) {
         \DB::rollback();
         throw $e;
     }
     \DB::commit();
     return $post;
 }