ankh2054 Posted May 6, 2014 Author Share Posted May 6, 2014 Is there a way to remove the existing image attached to that field, because what happens now is that after if a user changes his profile pic with another, it always references the first one. Is there a way to remove the original and replace with with newly uploaded one? Link to comment Share on other sites More sharing options...
Soma Posted May 6, 2014 Share Posted May 6, 2014 Ah. The thing is, I just tested now again, that when you use current user object $user echo $user->avatar->first->url; // will be an array of type Pageimages It will always be an array, cause no output formatting for the $user. BUT if you load the user via API, it's different... $u = $users->get("username"); echo $u->avatar->url; // will be single object of type Pageimage cause output formatting is now on. Got it? ;D (I sometimes think it would be easier and less confusing if this output formatting behaviour wasn't there for images.) Is there a way to remove the existing image attached to that field, because what happens now is that after if a user changes his profile pic with another, it always references the first one. Is there a way to remove the original and replace with with newly uploaded one? sure: $user->avatar->removeAll(); $user->avatar->add($newimage); $user->save(); 3 Link to comment Share on other sites More sharing options...
ankh2054 Posted May 6, 2014 Author Share Posted May 6, 2014 thanks all for the very helpful comments, you guys are really making this CMS amazing. Hopefully one day I will be able to help new users myself. My final code for the avatar display: //Load the user via API $u = $users->get($username); //Will be single object of type Pageimage $avatar = $u->avatar; //Check if avatar field is empty and assigns default image if ($u->avatar->url){ $thumb = $avatar->size(142); $avatar_thumb .= "<img class='img-rounded img-responsive' src='{$thumb->url}'' alt='{$avatar->description}'' />"; } else { $avatar_thumb .= "<img class='img-rounded img-responsive' src='{$config->urls->templates}/styles/images/bom-mashien.png' alt='BOMmachine' />"; } ?> <div class="user-image"> <?php echo $avatar_thumb; ?> </div> Avatar upload Image: // Upload avatar settings $upload_path = $config->paths->assets . "files/avatar_uploads/"; $f = new WireUpload('avatar'); //avatar is the name of the input-file field $f->setMaxFiles(1); $f->setMaxFileSize(3*1024*1024); $f->setOverwrite(true); $f->setDestinationPath($upload_path); $f->setValidExtensions(array('jpg', 'jpeg', 'png', 'gif')); // Process code if form_submit is issued if($input->post->form_submit) { if(!wireMkdir($upload_path)) { throw new WireException("No upload path!"); } $files = $f->execute(); if ($f->getErrors()) { foreach($files as $filename) @unlink($upload_path . $filename); foreach($f->getErrors() as $e) echo $e; } else { $user->of(false); $user->avatar->removeAll(); $user->avatar = $upload_path . $files[0]; $user->save(); $user->of(true); @unlink($upload_path . $files[0]); $session->redirect("/profile/"); } } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now