如果用户没有照片代码点火器,则显示默认图像 [英] display default image if user have no photo codeigniter

查看:62
本文介绍了如果用户没有照片代码点火器,则显示默认图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在显示我的图像时,可以从数据库中获取它,但是如果没有用户尚未上传照片,我想显示默认图像。我将no-avatar.jpg保存到了/ uploads文件夹

In displaying my image getting it from the database is fine but I want to display the default image if no user hasn't uploaded a photo yet. I saved a no-avatar.jpg to my /uploads folder

这是我的显示用户图像。

Here is my display user image.


view

view


<img height="180px" width="180px"class="ppborder" src="<?php echo base_url().'/uploads/'.$this->session->userdata('image'); ?>"> 



上载

upload


public function updatephoto($id)
    {
    if ((int)$id < 1)//$id is not an integer
    {
    redirect('Memberlogincontroller/member_view', 'refresh');
    }
    else{
    $this->load->helper(array('form','file','url'));
    $this->load->library('form_validation');
    $config_image = array(
        'upload_path'   => './uploads/',
        'allowed_types' => 'gif|jpg|png',
        'max_size'      => '1024',
        'overwrite'     => true
  
    );
    $this->load->library('upload', $config_image);

    if($this->form_validation->run()==false and empty($_FILES['userfile']['name'][0]))
        {
            $id = $this->session->userdata('id');
            $memberinfo = array(
                'error_image' => ''
            );
        $this->session->set_flashdata('flashError', 'Oops no photo selected', $memberinfo);         
        redirect('index.php/Memberlogincontroller/editphoto/'.$id, 'refresh');
        }       
    else
        {           
            $this->upload->do_upload();
             $data = array('upload_data' => $this->upload->data());
             $this->image_resize($data['upload_data']['full_path'], $data['upload_data']['file_name']);
             $id = $this->session->userdata('id');
             $this->db->where('id', $id);
             $query = $this->db->get('member'); 
            $data = array(
                'image' => $data['upload_data']['file_name']    
        );
        $this->db->update('member',$data,array('id'=>$id));          
        $this->session->set_userdata($data); 
        $this->session->set_flashdata('flashSuccess', 'Your photo has been updated.');
        redirect('index.php/Memberlogincontroller/getMember/'.$id, 'refresh');       
    }       
    }
    }

如何显示默认图像?

推荐答案

只需添加条件以检查图像是否可用。如果是,则显示用户图像,否则显示默认图像。

Just add a condition to check if image is available. If yes show user's image else default image.

<?php
$user_img = !empty($this->session->userdata('image')) ? $this->session->userdata('image') : 'no-avatar.jpg';
?>
<img height="180px" width="180px" class="ppborder" src="<?php echo base_url().'/uploads/'.$user_img; ?>"> 

这篇关于如果用户没有照片代码点火器,则显示默认图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆