2016-01-09 2 views
1

Я пытаюсь создать эскиз профиля для пользователя, когда он загружает изображение профиля. Я смотрю с некоторой помощью здесь, как недавно положил руку на Codeiniter, и я тоже новичок в php.Не удалось создать эскиз (изменить размер) изображение профиля в Codeigniter

В настоящее время это вставляет изображение профиля в папку «temp», но не изменяет его размер. Возможно, я ошибаюсь. Должен ли я создать новую функцию для миниатюры, или я могу включить ее вместе с тем, который у меня есть?

У меня нет проблем с добавлением нового изображения профиля. автоматически заменяя изображение и автоматически удаляя изображение профиля при добавлении нового. Просто изменение размера (уменьшенное изображение) изображения.

Вот контроллер:

public function profile_image() { 
    if($this->session->userdata('is_logged_in')){ 
    $username = $this->session->userdata('v_member_username'); 
    $url1 = $this->my_profile_model->see_if_old_image_exists($username); 

    if (empty($_FILES['profile_image']['tmp_name'])) { 
     return true; 
    }else{ 
     $url2 = $this->do_upload(); 
     $this->my_profile_model->update_profile_image($url2, $username); 
     if(!empty($url1)){ 
     $this->my_profile_model->delete_old_profile_image($url1); 
     } 
     } 
    } 
} 
private function do_upload() { 
    $type = explode('.', $_FILES['profile_image']['name']); 
    $type = $type[count($type)-1]; 
    $filename = uniqid(rand()).'.'.$type; 
    $url2 = './uploads/temp/'.$filename; 
    if(in_array($type, array('jpeg', 'gif', 'png', 'jpg'))) 
     if (empty($_FILES['profile_image']['tmp_name'])) { 
    return TRUE; 
    }else{ 
    if(is_uploaded_file($_FILES['profile_image']['tmp_name'])) 
     if(move_uploaded_file($_FILES['profile_image']['tmp_name'], $url2)); 

      return $url2; 
    return ''; 
    // do_thumb 
    $this->load->library('image_lib'); 
    $source_path = $_SERVER['DOCUMENT_ROOT'] . 'uploads/temp/' . $filename; 
    $target_path = $_SERVER['DOCUMENT_ROOT'] . 'uploads/profile/'; 
    $config_manip = array(
     'image_library' => 'gd2', 
     'source_image' => $source_path, 
     'new_image' => $target_path, 
     'maintain_ratio' => TRUE, 
     'create_thumb' => TRUE, 
     'thumb_marker' => '_thumb', 
     'width' => 270, 
     'height' => 263 
    ); 
    $this->load->library('image_lib', $config_manip); 
    if (!$this->image_lib->resize()) { 
     echo $this->image_lib->display_errors(); 
    } 
    // clear // 
    $this->image_lib->clear(); 
    } 
} 

И моя модель:

// Update profile Image 
    function update_profile_image($url2, $username){ 
     $this->db->set('profile_image', $url2); 
     $this->db->where('v_member_username', $username); 
     $this->db->update('vbc_registered_members'); 
    } 
// Look If There Was Any Old Image Earlier 
    function see_if_old_image_exists($username) { 
     $this->db->select('profile_image'); 
     $this->db->from('vbc_registered_members'); 
     $this->db->where('v_member_username', $username); 
     $query = $this->db->get(); 
     $query_result = $query->result(); 
     $row = $query_result[0]; 
     return $row->profile_image; 
    } 
// Auto Delete profile Image From Upload Folder On Updating New Image 
    function delete_old_profile_image($url1) { 
     unlink($url1); 
     return TRUE; 
    } 

Пожалуйста посоветуйте.

+0

Что такое сообщение об ошибке или ответ –

+0

@Abdulla, не сообщение об ошибке ... ли все, кроме миниатюры. –

ответ

1

Codeigniter предоставляет библиотеку для загрузки данных. см File Upload LibraryImage Library и

Это код, который я использую для загрузки изображений, создание эскиза + изменение размера изображения

/* 
* This function returns the path of imagemagick on your system 
*/ 
public static function getLibPath() 
{ 
    if(strlen(self::$_lib_path)==0) 
    { 
     self::$_lib_path=exec("/bin/bash -lc '/usr/bin/which convert'",$out,$rcode); 
    } 
    return self::$_lib_path; 
} 

/* 
* This function handles the upload and calls the resizeImage function 
* to generate the thumbnail and the resized image 
*/ 
public function update() 
{ 
    $config['upload_path'] = 'your upload path'; 
    $config['allowed_types'] = 'jpg|png|bmp'; 
    $config['max_size'] = '8192';//8mb //4096kb - 4mb 
    $config['max_width'] = '0'; 
    $config['max_height'] = '0'; 
    $config['overwrite'] = false; 
    $config['encrypt_name']=true;//this generates a filename for you 

    $this->load->library('upload', $config); 
    $result = $this->upload->do_upload(); 


    if($result) { 
     $fileInfo=$this->upload->data(); 

     $this->resizeImage($fileInfo['full_path']); 
     $data = array(
      'filename' => $fileInfo['file_name'], 
      'orig_file_name' =>$fileInfo['orig_name'], 
     ); 
     //pseudo function for storing the file info 
     save_metadata_to_db($data); 

    } 
    else 
    { 
     echo $this->upload->display_errors()); 
    } 
} 
/* 
* This function creates a thumbnail + the resized image 
*/ 
private function resizeImage($filename) 
{ 
    //This function creates a file with the orig. filename + _thumb 

    $config['image_library'] = 'ImageMagick'; 
    $config['source_image'] = $filename; 
    $config['create_thumb'] = TRUE; 
    $config['maintain_ratio'] = FALSE; 
    $config['width'] = 60; 
    $config['height'] = 60; 
    $config['library_path'] = $this->getLibPath(); 
    $config['quality'] = '100%'; 


    $this->load->library('image_lib', $config); 

    if (! $this->image_lib->resize()) 
    { 
     echo $this->image_lib->display_errors()); 
    } 


    $config['create_thumb'] = False; 
    $config['maintain_ratio'] = TRUE; 
    $config['width'] = 1080; 
    $config['height'] = 1920; 

    $this->image_lib->initialize($config); 

    if (! $this->image_lib->resize()) 
    { 
     echo $this->image_lib->display_errors()); 
    } 
} 
Смежные вопросы