1

I have set up my php view to select the user's uploaded avatar in a table named 'user', the row is 'avatar':

<img src="<?=$user->avatar; ?>" class='profile'>

I am trying to set up a link to a default image if there is no image uploaded:

<? if(empty($avatar)) $avatar = "/uploads/default.gif"; ?>

I can't seem to get this to work - any help would be appreciated. Many thanks.

I've updated to the below but it seems to just default to the default.gif

<?php if(isset($avatar)): ?>
    <img src="<?=$user->avatar; ?>" class='profile'>

<?php else: ?>
    <img src='/uploads/default.gif'>
    <div id="prompt">Upload your own avatar image</div>
<?php endif; ?>

Process Image Upload - in users controller:

public function picture() {

        # Sanitize Data Entry
        $_POST = DB::instance(DB_NAME)->sanitize($_POST);

        # Upload Image
        if ($_FILES['avatar']['error'] == 0) {

            $avatar = Upload::upload($_FILES, "/uploads/avatars/", array('jpg', 'jpeg', 'gif', 'png', 'JPG', 'JPEG', 'GIF', 'PNG'), $this->user->user_id);

            if($avatar == 'Invalid file type.') {

                # Error
                Router::redirect('/users/profile/error'); 
            }

            else {

                # Upload Image
                $data = Array('avatar' => $avatar);
                DB::instance(DB_NAME)->update('users', $data, 'WHERE user_id = '.$this->user->user_id);

                # Resize and Save Image
                $imageObj = new Image($_SERVER['DOCUMENT_ROOT'].'/uploads/avatars/'.$avatar);
                $imageObj->resize(150,150,'crop');
                $imageObj->save_image($_SERVER['DOCUMENT_ROOT'].'/uploads/avatars/'.$avatar); 
            }
        }

        else {

            # Error
            Router::redirect("/users/profile/error");  
        }

        # Send to Profile Page
        Router::redirect('/users/profile'); 
    }  
snooks
  • 13
  • 1
  • 5

5 Answers5

1

You should prepare your variables well.

<?php
if (empty($user->avatar)){
   $avatar = 'http://example.com/uploads/default.gif';
}
else{
   $avatar = 'http://example.com'.$user->avatar;
}
?>

then in your image HTML:

<img src="<?php echo avatar; ?>" class='profile'>
Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
SaidbakR
  • 13,303
  • 20
  • 101
  • 195
0

Try this..

<? if(empty($user->avatar)) $user->avatar = "/uploads/default.gif"; ?>
Balaji Perumal
  • 830
  • 1
  • 6
  • 20
  • 1
    thank you, but unfortunately that didn't work either - do I need to include img src in there somewhere maybe? – snooks Dec 14 '13 at 14:19
0

Your php code generates static html on server side. Suppose your server name is "site.com", php file where your avatar code is located is "index.php". If server is working correctly you can open "site.com/index.php" in browser on client side. Browser has function "view page source" it shows your generated html.

After that you must do this:

  1. Examine html content of the generated page where your img tag is used.
  2. Find out what img src property value is.
  3. Correct your php code If value is wrong.
  4. If value is correct, but avatar did not load look if img src points to valid uri.
sim
  • 756
  • 6
  • 18
0

Try this:

<? if(!empty($user->avatar)) { ?>
    <img src="<?=$user->avatar; ?>" class="profile">
<? } else { ?>
    <img src="/uploads/default.gif" class="profile">
    <!--Make sure /uploads/default.gif is correct-->
<? } ?>

Don't use php short tags see here

Please make sure your image path is correct.

Community
  • 1
  • 1
Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
  • yes, I am - I think I'm more confused than when I first posted this question, I have tried every suggestion on this page. Thank you for your help – snooks Dec 15 '13 at 13:23
0

This is not going to help you get your image path correct, but based on how you are calling this I am assuming you are using an ORM? Regardless, you should be able to tweak whatever class $user calls, to address this in your class.

class User {
    protected $avatar; //changed to protected because now you are calling getAvatar();
    ....
    function getAvatar() {
        if(!isset($this->avatar)) {
            $this->avatar = "[URL to image]";
        }
        return $this->avatar;
    }
    ...
}

Although I'm not sure it's a good idea, you could put it in your __construct class if you want to call $user->avatar instead.

class User {
    public $avatar; //still public now..

    function __construct() {
        if(!isset($this->avatar)) {
            $this->avatar = "[URL to image]";
        }
        return $this->avatar;
    }
    ...
}

The benefit of this is cleaner code in your HTML and fewer times you need to change the URL if you ever do need to. To be exact - you would only need to change it once!

smcjones
  • 5,490
  • 1
  • 23
  • 39
  • it seemed to work but I realised on further testing it was just defaulting to the default gif - I've updated my initial code above – snooks Dec 14 '13 at 17:08