I want to crop my image upload and change my profile photo, but I don't know why it's not working even the picture was upload, profile picture not change, you must logout and picture was be change. this is my controller.
private $pk = 'nim';
private $table = 'mahasiswa';
public function update()
{
$nim = $this->session->userdata('nim');
if($_POST) {
$file = $this->upload();
if ($this->m_data->update($this->pk, $nim, $this->table, $this->field_data($file['data']))) {
redirect('Mahasiswa/Profil');
}
} else {
redirect('Mahasiswa/Formubah');
}
}
private function field_data($file = '')
{
$data['username'] = $this->input->post('username');
$data['password'] = $this->input->post('password');
if ($file != '') {
$data['foto'] = $file['file_name'];
}
return $data;
}
public function upload()
{
$config['upload_path'] = './assets/image/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 0;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('foto')){
return[
'data' => $this->upload->display_errors(),
];
} else {
$data = $this->upload->data();
$resize['image_library'] = 'gd2';
$resize['x_axis'] = 50;
$resize['y_axis'] = 50;
$resize['width'] = $img['config']['foto']['width'];
$this->image_lib->initialize($resize);
$this->image_lib->crop();
return [
'data' => $this->upload->data(),
];
}
}
i hope controller was enough. i forgot to tell, i use this show my picture. <?php echo base_url('assets/image/').$this->session->userdata('foto');?>
You are not properly implementing the library. Y forgot the library load and have not given its path.
$image_data = $this->upload->data();
$config['image_library'] = 'imagemagick';
$config['library_path'] = 'C:ImageMagick-7.0.1-3-portable-Q16-x86';//your library
$config['source_image'] = $image_data['full_path']; //get original image
$config['x_axis'] = 50;
$config['y_axis'] = 50;
$this->load->library('image_lib', $config);
if (!$this->image_lib->crop()) {
$this->handle_error($this->image_lib->display_errors());
}
Related
I am uploading multiple images in codeigniter framework. When i select multiple images, it upload only last selected image.
Below is my controller code, where i am doing wrong?
$files = $_FILES;
for($i=0; $i< count($_FILES['item_image']['name']); $i++)
{
$_FILES['item_image']['name']= $files['item_image']['name'][$i];
$_FILES['item_image']['type']= $files['item_image']['type'][$i];
$_FILES['item_image']['tmp_name']= $files['item_image']['tmp_name'][$i];
$_FILES['item_image']['error']= $files['item_image']['error'][$i];
$_FILES['item_image']['size']= $files['item_image']['size'][$i];
$this->upload->initialize($config);
if ($this->upload->do_upload('item_image'))
{
$file_name=$this->upload->data('file_name');
$newdata = array(
'item_id'=>$last_id,
'image_name'=>$file_name
);
$this->Item_model->add_item_image($newdata);
$newdatatwo = array(
'item_image'=>$file_name
);
$this->Item_model->update_item($newdatatwo,$last_id);
$data['error_or_success_message'] = $this->session->set_flashdata('error_or_success_message', 'Item added Successfully!');
}else{
$data['error_or_success_message'] = $this->session->set_flashdata('error_or_success_message', 'Some Error, Please Try Again!');
}
}
The second condition in your for loop gets redefined in the first line of your for loop. I would check to make sure that isn't causing errors.
Refer The Below Code
Controller Code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Multiple_upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
}
public function index()
{
$this->load->view('upload');
}
public function Upload()
{
$files = $_FILES;
$config['upload_path'] = 'common_assets/images';
$config['allowed_types'] = 'jpg|gif|png';
$config['max_size'] = '';
$config['remove_spaces'] = true;
$config['overwrite'] = true;
$config['max_width'] = '';
$config['max_height'] = '';
$config['max_filename'] = 0;
foreach ($_FILES['item_image']['name'] as $key => $value)
{
$_FILES['item_image']['name']= $files['item_image']['name'][$key];
$_FILES['item_image']['type']= $files['item_image']['type'][$key];
$_FILES['item_image']['tmp_name']= $files['item_image']['tmp_name'][$key];
$_FILES['item_image']['error']= $files['item_image']['error'][$key];
$_FILES['item_image']['size']= $files['item_image']['size'][$key];
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!empty($value))
{
if (!$this->upload->do_upload('item_image'))
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
exit();
}else{
$file_name=$this->upload->data('file_name');
//**********Database Entry*********************
// $newdata = array(
// 'item_id'=>$last_id,
// 'image_name'=>$file_name
// );
// $this->Item_model->add_item_image($newdata);
// $newdatatwo = array(
// 'item_image'=>$file_name
// );
// $this->Item_model->update_item($newdatatwo,$last_id);
}
}
}
echo "<pre>Uploaded Successfully"; die;
}
}
?>
//This works for me:
$config['upload_path'] = 'common_assets/images';
$config['allowed_types'] = 'jpg|gif|png';
$config['max_size'] = '';
$config['remove_spaces'] = true;
$config['overwrite'] = true;
$config['max_width'] = '';
$config['max_height'] = '';
$config['max_filename'] = 0;
$this->load->library('upload', $config);
foreach ($_FILES as $fieldname => $fileObject) {
if (!empty($fileObject['name'])) {
$this->upload->initialize($config);
if (!$this->upload->do_upload($fieldname)) {
$errors = $this->upload->display_errors();
redirect('error_page', $errors);
} else {
$data = array($fieldname => $this->upload->data());
extract($data[$fieldname]);
$$fieldname = $file_name;
}
}
}
$array_to_save = array('tbl_name_field_1' => $file_name_from_post_1,
'tbl_name_field_2' => $file_name_from_post_2);
I am trying to upload an image.
Here is the code
<?php
$PerpetualCalendar = $this->input->post();
if($files['imgQuarter1']['name'][$key]!="")
{
$_FILES['imgQuarter1']['name']= $files['imgQuarter1']['name'][$key];
$_FILES['imgQuarter1']['type']= $files['imgQuarter1']['type'][$key];
$_FILES['imgQuarter1']['tmp_name']= $files['imgQuarter1']['tmp_name'][$key];
$_FILES['imgQuarter1']['error']= $files['imgQuarter1']['error'][$key];
$_FILES['imgQuarter1']['size']= $files['imgQuarter1']['size'][$key];
if(isset($PerpetualCalendar['id1'][$key]))
{
$img_id=$PerpetualCalendar['id1'][$key];
}
else
{
$img_id=$GetLastID;
}
$fileName = $img_id.'_NAME_'. $files['imgQuarter1']['name'][$key];
$config['file_name'] = $fileName;
$this->upload->initialize($config);
if($this->upload->do_upload('imgQuarter1'))
{
if(isset($PerpetualCalendar['NAMEpath_idQ1'][$key])){
$ImgUpdate[$imgkey]['tbl_name']='perpetual_calendar';
$ImgUpdate[$imgkey]['field_name']='event_name';
$ImgUpdate[$imgkey]['user_FK']=$user_id;
$ImgUpdate[$imgkey]['path']=$uploadpath.'/'.$fileName;
$ImgUpdate[$imgkey]['entry_id']=$img_id;
$ImgUpdate[$imgkey]['path_id']=$PerpetualCalendar['NAMEpath_idQ1'][$key]; //for update purpose
}else{
$ImgInsert[$imgkey]['tbl_name']='perpetual_calendar';
$ImgInsert[$imgkey]['field_name']='event_name';
$ImgInsert[$imgkey]['user_FK']=$user_id;
$ImgInsert[$imgkey]['path']=$uploadpath.'/'.$fileName;
$ImgInsert[$imgkey]['entry_id']=$img_id;
}
$imgkey++;
}
else
{
$this->data['error'] = $this->session->set_flashdata(array('error' => $this->upload->display_errors()));
}
}
?>
here is the View
echo form_upload(array('name'=>'imgQuarter1['.$key.']','class'=>'default'),'');
echo form_input(array('name' => 'id1['.$key.']', 'type'=>'hidden','value'=>$ValuePerpetualCalendar['cal_id']));
in above code,i can get $PerpetualCalendar['id1'][$key] this Id,but
image can not upload in the folder and can't save in Database Aswell !
Any Help?
public function upload_multiple_image($user_id){
$image = $_FILES['image']['name'];
$files = $_FILES['image'];
$image_title = $this->input->post('image_title');
$image_link = $this->input->post('image_link');
foreach($image as $key=>$data){
if($files['name'][$key]!=""){
$add_data['user_id'] = $user_id;
$add_data['title'] = $image_title[$key];
$add_data['description'] = $image_link[$key];
$add_data['status'] = 1;
$this->db->insert('user_images',$add_data);
$last_inserted_id = $this->db->insert_id();
$_FILES['image']['name'] = $files['name'][$key];
$_FILES['image']['type'] = $files['type'][$key];
$_FILES['image']['tmp_name'] = $files['tmp_name'][$key];
$_FILES['image']['error'] = $files['error'][$key];
$_FILES['image']['size'] = $files['size'][$key];
$config['upload_path'] = './user_images/original/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['overwrite'] = TRUE;
$config['file_name'] = 'place_image_'.$last_inserted_id;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('image')){
$file_details = $this->upload->data();
$update_data = array('image'=>$file_details['file_name']);
$this->db->update('user_images',$update_data,array('id'=>$last_inserted_id));
}
}
}
}
I can't access my controller array in my view in codeigniter. When I pass array in my view it give me unidentified variable error, please help me! This code is for change profile pic.
I want to display changed pic to my view! I can get photo path from my model to my view but can't access it to my view from my controller
My code is here
Model:
class Upload_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->database();
}
public function update_photo($source) {
$username = $_SESSION['username'];
$pass = array('avatar'=> $source);
$this->db->update('users',$pass,array('username'=>$username));
$query = $this->db->get_where('users',array('avatar'=>$source,'username'=>$username),1 );
foreach ($query ->result() as $row)
{
return $row->avatar;
}
}
}
Controller:
class Upload_photo extends CI_Controller {
public $upload_model = "";
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->model('upload_model');
}
public function index() {
session_start();
if(isset($_FILES['file1']))
{
$u = $_SESSION['username'];
$config['upload_path'] = 'user/'.$u;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '30000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload',$config);
$filename = $_FILES['file1']['name'];
if(!$this->upload->do_upload('file1'))
{
echo "Error". $this->upload->display_errors();
return;
}
$config = array();
$config['image_library'] = 'gd2';
$config['source_image'] = 'user/'.$u.'/'.$filename ;
$config['new_image']= FCPATH . 'user/'.$u.'/Thumb/';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 110;
$config['height'] = 110;
$this->load->library('image_lib',$config);
if(!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
$source = base_url().'user/'.$u.'/Thumb/'.$filename;
$data["avatar"] = $this->upload_model->update_photo($source);
}
else
{
$this->load->view('profile_view',$data);
}
}
}
View - Could not access $avatar:
<div id="showimage" name='showimage'>
<?php
foreach ($avatar as $row)
{
echo $row;
}
?>
</div>
for in above controller you are defining $data["avatar"] in if clause true part, but you are load the view in else part. how it will take....
thats way it given as undifened variable..
once check your controller.
in controller
class Upload_photo extends CI_Controller{
public $upload_model = "";
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->model('upload_model');
}
public function index()
{
session_start();
if(isset($_FILES['file1']))
{
$u = $_SESSION['username'];
$config['upload_path'] = 'user/'.$u;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '30000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload',$config);
$filename = $_FILES['file1']['name'];
if(!$this->upload->do_upload('file1'))
{
echo "Error". $this->upload->display_errors();
return;
}
$config = array();
$config['image_library'] = 'gd2';
$config['source_image'] = 'user/'.$u.'/'.$filename ;
$config['new_image']= FCPATH . 'user/'.$u.'/Thumb/';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 110;
$config['height'] = 110;
$this->load->library('image_lib',$config);
if(!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
else
{
$source = base_url().'user/'.$u.'/Thumb/'.$filename;
$data['avatar'] = $this->upload_model->update_photo($source);
$this->load->view('profile_view',$data);
}
}
}
}
try this in view
<div id="showimage" name='showimage'>
<?php
isset($avatar){
foreach ($avatar as $row)
{
echo $row;
}
}
?>
I am building a user profile in codeigniter. I have set up a system that allows the user to create a folder with their user-id, and then upload photos to it. I have been abel to successfully upload files to a folder but I am just not sure how to display the photo in my view pertinent to the user.
I would assume I attach it to my $user[''] just not sure exactly how to do that. Thanks in advance.
public function upload()
{
$this->load->library('session');
$this->load->helper('url');
$session_id = $this->session->userdata('id');
$this->load->model('account_model');
$user = $this->account_model->user();
$data['user'] = $user;
echo $user['id'];
$user_folder = './uploads/' . $this->session->userdata('id');
if(!is_dir($user_folder)){
mkdir($user_folder, 0777);
}
$config['image_library'] = 'gd2';
$config['source_image'] = $user_folder;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 50;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$config['upload_path'] = $user_folder;
$config['allowed_types'] = 'gif|jpg|png';
//$config['max_size'] = '165';
$config['max_width'] = '165';
$config['max_height'] = '165';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = '/account/upload';
$this->load->view('includes/templates/main_page_template', $data);
}
else
{
$data = array('upload_data' => $this->upload->data());
$data['main_content'] = '/account/success';
$this->load->view('includes/templates/main_page_template', $data);
}
}
public function img(array $user=array()) {
if(!isset($user['relation'])) {
return '';
}
$path = '';
if($user['relation'] != 'Choose One') {
$path = '/styles/images/Sharpie' . $user['relation'] . '.gif';
}
return $path;
}
}
Controller:
public function view_image($user_id)
{
$this->load->model('account_model');
$data['image'] = $this->account_model('model_that_will_fetch_relevant_image');
$this->load->view('where_image_is',$data);
}
model:
public function get_image($user_id)
{
$this->db->select('stuff'): \\ Select image filename
$this ->db->where('table_that_has_user_id', $user_id );
$query = $this->db->get($this->_table_name . ' as aliase'); \\ where filename is
return $query->result();
}
view:
<img src = "<?=site_url('path/on/server/' . $image['image_filename']); ?>">
This assumes you are storing the image filename in a mysql database.
class Users_model extends CI_Model {
var $table = 'tbl_admin';
function __construct(){
parent::__construct();
$this->load->database();
}
public function processLogin($username,$password){
$this->db->select('username,password');
$where = $array = array('username' =>$username,'password'=>$password);
$this->db->where($where);
$this->db->from($this->table);
$query = $this->db->get();
return $query;
}
}
Please use this code, I hope it will help you.
Using profile_image variable you can display image.
<?php
public function upload()
{
$this->load->library('session');
$this->load->helper('url');
$session_id = $this->session->userdata('id');
$this->load->model('account_model');
$user = $this->account_model->user();
$data['user'] = $user;
echo $user['id'];
$user_folder = './uploads/' . $this->session->userdata('id');
if(!is_dir($user_folder)){
mkdir($user_folder, 0777);
}
$config['image_library'] = 'gd2';
$config['source_image'] = $user_folder;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 50;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$config['upload_path'] = $user_folder;
$config['allowed_types'] = 'gif|jpg|png';
//$config['max_size'] = '165';
$config['max_width'] = '165';
$config['max_height'] = '165';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = '/account/upload';
$this->load->view('includes/templates/main_page_template', $data);
}
else
{
$image = array('upload_data' => $this->upload->data());
$data['profile_image'] = $user_folder.$image['upload_data']['file_name'];
$data['main_content'] = '/account/success';
$this->load->view('includes/templates/main_page_template', $data);
}
}
public function img(array $user=array()) {
if(!isset($user['relation'])) {
return '';
}
$path = '';
if($user['relation'] != 'Choose One') {
$path = '/styles/images/Sharpie' . $user['relation'] . '.gif';
}
return $path;
}
}
I am having trouble resizing photos that I am successfully uploading in my my upload module.
I am able to upload photos to the appropriate folder, however my resizing is not working and I also want to simultaneously create a thumbnail duplicate in the same folder.
public function upload()
{
$this->load->library('session');
$this->load->helper('url');
$session_id = $this->session->userdata('id');
$this->load->model('account_model');
$user = $this->account_model->user();
$data['user'] = $user;
echo $user['id'];
$user_folder = './uploads/' . $this->session->userdata('id');
if(!is_dir($user_folder)){
mkdir($user_folder, 0777);
}
$config['image_library'] = 'gd2';
$config['source_image'] = $user_folder;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 50;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$config['upload_path'] = $user_folder;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '165';
$config['max_width'] = '165';
$config['max_height'] = '165';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = '/account/upload';
$this->load->view('includes/templates/main_page_template', $data);
}
else
{
$data = array('upload_data' => $this->upload->data());
$data['main_content'] = '/account/success';
$this->load->view('includes/templates/main_page_template', $data);
}
}
Try this:
public function upload()
{
$this->load->library('session');
$this->load->helper('url');
$session_id = $this->session->userdata('id');
$this->load->model('account_model');
$user = $this->account_model->user();
$data['user'] = $user;
echo $user['id'];
$user_folder = './uploads/' . $this->session->userdata('id');
if(!is_dir($user_folder)){
mkdir($user_folder, 0777);
}
$this->load->library('image_lib'); #load the image manipulation library without initiatlising it here.
$configThumb['image_library'] = 'gd2';
//$configThumb['source_image'] = $user_folder;
$configThumb['create_thumb'] = TRUE;
$configThumb['maintain_ratio'] = TRUE;
$configThumb['width'] = 50;
$configThumb['height'] = 50;
//$this->image_lib->resize();
$config['upload_path'] = $user_folder;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '165';
$config['max_width'] = '165';
$config['max_height'] = '165';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = '/account/upload';
$this->load->view('includes/templates/main_page_template', $data);
}
else
{
$configThumb['source_image'] = $uploadedDetails['full_path'];
$this->image_lib->initialize($configThumb); #initialize the library here
$this->image_lib->resize(); #resize is done here
$data = array('upload_data' => $this->upload->data());
$data['main_content'] = '/account/success';
$this->load->view('includes/templates/main_page_template', $data);
}
}
Full path refers to the source image that is uploaded to your server. See the else condition you will get the idea. Now, the change is that we are loading the library and initializing it after uploading the image and hence we are getting the full path from $this->upload->data().
EDIT : MY WORKING FUNCTION TO CROP AN IMAGE IN TWO DIFFERENT SIZES
function udpate_profile($userId = 0){
$data = array();
//echo "<pre>";print_r($_POST);echo "</pre>";
/* Upload Image */
if($_FILES['image']['name'] != ""){
//echo "<pre>";print_r($_FILES);echo "</pre>";
//echo "enter";die;
/* Check if previous file exists */
$chkRs = $this->db->select('image')->where('id', $this->session->userdata['logged_user']['id'])->get('admins');
//echo $this->db->last_query();die;
if($chkRs->num_rows() > 0){
$chkD = $chkRs->row_array();
if($chkD['image'] != ""){
### delete the previous image ###
$pathActual = './profile_images/';
$pathMedium = './profile_images/medium/';
$pathThumb = './profile_images/thumbs/';
if(file_exists($pathActual.$chkD['image'])){ #delete the actual image
unlink($pathActual.$chkD['image']);
}
if(file_exists($pathMedium.$chkD['image'])){ #delete the medium image
unlink($pathMedium.$chkD['image']);
}
if(file_exists($pathThumb.$chkD['image'])){ #delete the thumb image
unlink($pathThumb.$chkD['image']);
}
### delete the previous image ###
}
}
/* Check if previous file exists */
//print_r($_FILES['image']);die;
$this->load->library('image_lib');
$configUpload['upload_path'] = './profile_images/';
$configUpload['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$configUpload['max_size'] = '0';
$configUpload['max_width'] = '0';
$configUpload['max_height'] = '0';
$configUpload['encrypt_name'] = true;
$this->load->library('upload', $configUpload);
/* size 64*72 for comments */
$configThumb = array();
$configThumb['image_library'] = 'gd2';
$configThumb['create_thumb'] = TRUE;
$configThumb['new_image'] = './profile_images/thumbs/';
$configThumb['maintain_ratio'] = TRUE;
$configThumb['width'] = 64;
$configThumb['height'] = 72;
$configThumb['thumb_marker'] = "";
//$this->load->library('image_lib');
/* size 64*72 for comments */
/* size 167*167 for profile page */
$configThumbMedium = array();
$configThumbMedium['image_library'] = 'gd2';
$configThumbMedium['create_thumb'] = TRUE;
$configThumbMedium['new_image'] = './profile_images/medium/';
$configThumbMedium['maintain_ratio'] = TRUE;
$configThumbMedium['width'] = 167;
$configThumbMedium['height'] = 167;
$configThumbMedium['thumb_marker'] = "";
/* size 167*167 for profile page */
if(!$this->upload->do_upload('image')){
return 0;
}
$uploadedDetails = $this->upload->data();
if($uploadedDetails['is_image'] == 1){
$configThumb['source_image'] = $uploadedDetails['full_path'];
$configThumbMedium['source_image'] = $uploadedDetails['full_path'];
$raw_name = $uploadedDetails['raw_name'];
$file_ext = $uploadedDetails['file_ext'];
$imgname = $raw_name.$file_ext;
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
$this->image_lib->initialize($configThumbMedium);
$this->image_lib->resize();
}
}
//die();
/* Upload Image */
$data = $this->input->post(null);
//echo "<pre>";print_r($data);echo "</pre>";die;
if(isset($imgname) && $imgname != ""){
$data['image'] = $imgname;
}
$this->db->where('id',$this->session->userdata['logged_user']['id'])->update('admins', $data);
return 1;
}
it's likely you need to pass source_image the full path of the image on the server. so if you're trying to use /uploads, then it's likely you'll need to set it as /var/www/html/uploads or whatever the full path is to that folder since the image processing is relative to the server, not the site url