Codeigniter news image resize - codeigniter

How to resize the news picture? I never could.
My problem [source_image]
I use Codeigniter 3.1.6
Controller
public function insert_news() {
$config['upload_path'] = 'uploads/news/';
$config['allowed_types'] = 'gif|jpg|png';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if ($this->upload->do_upload('news_image'))
{
$image = $this->upload->data();
$image_url = $image['file_name'];
$db_insert ='upload/news/'.$image_url.'';
$data=array(
'news_title' => $this->input->post('news_title'),
'news_content' => $this->input->post('news_content'),
'news_image' => $db_insert,
'news_sef'=>sef($this->input->post('news_title'))
);
$this->load->model('vt');
$result = $this->vt->insert_news($data);
if ($result) {
echo "yes";
} else {
echo "no";
}
}
}

You can try this solution for your problem :
Controller :
<?php
public function insert_news() {
$config['upload_path'] = 'uploads/news/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '20240000245';
$config['overwrite'] = TRUE;
$config['encrypt_name'] = TRUE;
// You can change width & height
$imgage_width= 60;
$imgage_height= 60;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload($field)) {
$error = $this->upload->display_errors();
// uploading failed. $error will holds the errors.
$this->form_validation->set_message('error',$error);
return FALSE;
} else {
$fdata = $this->upload->data();
$configer = array(
'image_library' => 'gd2',
'source_image' => $fdata['full_path'],
'maintain_ratio' => TRUE,
'width' => $imgage_width,
'height' => $imgage_height,
);
// Load Image Lib Library
$this->load->library('image_lib');
$this->image_lib->clear();
$this->image_lib->initialize($configer);
$this->image_lib->resize();
$img_data ['path'] = $config['upload_path'] . $fdata['file_name'];
// uploading successfull, now do your further actions
$data=array(
'news_title' => $this->input->post('news_title'),
'news_content' => $this->input->post('news_content'),
'news_image' => $img_data ['path'],
'news_sef'=>sef($this->input->post('news_title'))
);
$this->load->model('vt');
$result = $this->vt->insert_news($data);
if ($result) {
$this->form_validation->set_message('success',"News has been added successfully.");
redirect('contorller/action_name');
} else {
$this->form_validation->set_message('error',"Error in aading news");
redirect('contorller/action_name');
}
}
}
?>
I Hope it will Help you.

Related

how to do image upload in codeigniter?

I am trying to upload an image in root folder and its file name in database. here is what I did for the upload function:
public function add_blog($id=0){
if(!empty($_FILES['picture']['name'])){
$config['upload_path'] = 'uploads/blog_image';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['file_name'] = $_FILES['picture']['name'];
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
// print_r($value['name'][$s]);exit;
if($this->upload->do_upload('picture')){
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
print_r($picture); exit;
}
}
print_r($config['file_name']); exit;
$data['blog_data']=array('blog_post'=>$this->input->post('blog_post'),
'posted_by'=>$this->input->post('posted_by'),
'blog_image'=>$picture);
if ($id==0){
$this->db->insert('blog',$data['blog_data']);
// $last_id = $this->db->insert_id();
}
else {
$this->db->where('id',$id);
// $last_id = $this->db->insert_id();
$this->db->update('blog',$data['blog_data']);
}
}
problem here is i am being able to insert other data except image. I get the image name with that print_r($config[file_name]) if i do print_r() and exit, if not it will just insert other data except image. But the image is neither uploaded in root folder nor its name in database. If I give the non existing upload path, then also its not throwing any error. I think code inside If is not executed. How can i solve this ? Thanks in advance.
private function _upload_image( ) {
$this->load->library( 'upload' );
if ($_FILES && $_FILES['picture']['name'] !== ""){
$config['upload_path'] = 'uploads/blog_image';
$config['allowed_types'] = 'jpg|jpeg|png|bmp';
$config['max_size'] = 10000;
/*the picture name must be unique, use function now()*/
$config['file_name'] = $_FILES['picture']['name'] . now();
$config['file_ext_tolower'] = TRUE;
$this->upload->initialize( $config );
if ( $this->upload->do_upload( 'picture' ) ){
$file_name = $this->upload->data()['file_name'];
$full_path = $this->upload->data()['full_path'];
/*If you want create a thumb, use this part*/
$this->load->library('image_lib');
$config = array(
'source_image' => $path,
'new_image' => $this->_image_path,
'maintain_ratio' => true,
'width' => 128,
'height' => 128,
'create_thumb' => TRUE,
'thumb_marker' => '_thumb',
);
$this->image_lib->initialize( $config );
$this->image_lib->resize();
/*Save in database*/
$this->db->insert('blog', [
'file_name' => $file_name,
'full_path' => $full_path
]);
} else {
//if picture is empty, do something
}
}
}
You do not need to use $_FILES && $_FILES ['picture']['name']! == "" only if your form has the picture field as an optional field, $this->upload->do_upload('picture') and get data from $this->upload->data(), read the manual
public function add_blog()
{
$config['upload_path'] = '.uploads/blog_image';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = 10000;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('picture'))
{//Do something with errors
$errors = $this->upload->display_errors();
}
else
{
$data = $this->upload->data();
$this->db->insert('blog', [
'file_name' => $data['file_name'],
'full_path' => $data['full_path']
]);
}
}
I just didn't mention the file size to be uploaded. I did this in my above code and worked.
EDIT
public function add_blog($id=0){
if(!empty($_FILES['picture']['name'])){
$config['upload_path'] = 'uploads/blog_image';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = 0;
$config['file_name'] = $_FILES['picture']['name'];
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
// print_r($value['name'][$s]);exit;
if($this->upload->do_upload('picture')){
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
// print_r($picture); exit;
}
}
// print_r($config['file_name']); exit;
$data['blog_data']=array('blog_post'=>$this->input->post('blog_post'),
'posted_by'=>$this->input->post('posted_by'),
'blog_image'=>$picture);
if ($id==0){
$this->db->insert('blog',$data['blog_data']);
// $last_id = $this->db->insert_id();
}
else {
$this->db->where('id',$id);
// $last_id = $this->db->insert_id();
$this->db->update('blog',$data['blog_data']);
}
}
And this code works for both insert and update.

When 2 different images are uploaded to 2 different folders,then the images are uploaded.but the thumbs are not created

Here is my controller function, please help me to create the thumbs of both images. Only the images are uploaded to the folder. i created a function named resize to create the thumbs. that's also given in the controller.
public function add() {
$this->load->helper(array('form', 'url'));
$this->load->helper('file');
$this->load->library('form_validation');
$this->form_validation->set_rules('txtPrdname', 'Product Name', 'trim|required|htmlspecialchars');
$this->form_validation->set_rules('sbPrdcategory', 'Product Category', 'trim|required|htmlspecialchars');
$this->form_validation->set_rules('sbPrduser', 'Managing User', 'trim|required|htmlspecialchars');
$this->form_validation->set_rules('txtPrdprofile', 'Product Profile', 'trim|required|htmlspecialchars');
if ($this->form_validation->run() == FALSE) {
$data_view["error"] = "";
$this->load->view('moderator/templates/header');
$this->load->view('moderator/templates/sidebar');
$this->load->view('moderator/b2bproduct_add', $data_view);
$this->load->view('moderator/templates/footer');
} else {
// Image uploading codes
$config['upload_path'] = 'assets/images/b2bproduct';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1000';
$config['max_width'] = '2024';
$config['max_height'] = '1768';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
if (isset($_FILES['filePrdimage']['name'])) {
$config['file_name'] = substr(md5(time()), 0, 28) . $_FILES['filePrdimage']['name'];
}
$this->load->library('upload');
$this->upload->initialize($config);
if (!$this->upload->do_upload('filePrdimage')) {
//no file uploaded or failed upload
$error = array('error' => $this->upload->display_errors());
} else {
$dat = array('upload_data' => $this->upload->data());
$this->load->library('upload');
$this->upload->initialize($config);
$this->resize($dat['upload_data']['full_path'], 'assets/images/b2bproduct/thump/' . $dat['upload_data']['file_name'], 180, 400);
}
if (empty($dat['upload_data']['file_name'])) {
$prdimage = '';
} else {
$prdimage = $dat['upload_data']['file_name'];
}
// End Image uploading Codes
// Logo uploading codes
$config['upload_path'] = 'assets/images/b2blogo';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1000';
$config['max_width'] = '2024';
$config['max_height'] = '1768';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
if (isset($_FILES['filePrdlogo']['name'])) {
$config['file_name'] = substr(md5(time()), 0, 28) . $_FILES['filePrdlogo']['name'];
}
$this->load->library('upload');
$this->upload->initialize($config);
if (!$this->upload->do_upload('filePrdlogo')) {
//no file uploaded or failed upload
$error = array('error' => $this->upload->display_errors());
} else {
$dat1 = array('upload_data' => $this->upload->data());
$this->load->library("upload",$config);
$this->resize($dat1['upload_data']['full_path'], 'assets/images/b2blogo/thump/' . $dat1['upload_data']['file_name'], 180, 400);
}
if (empty($dat1['upload_data']['file_name'])) {
$prdlogo = '';
} else {
$prdlogo = $dat1['upload_data']['file_name'];
}
// End Logo uploading Codes
$data = array(
'prd_name' => $this->input->post('txtPrdname'),
'prd_category' => $this->input->post('sbPrdcategory'),
'prd_user' => $this->input->post('sbPrduser'),
'prd_profile' => $this->input->post('txtPrdprofile'),
'prd_oem' => $this->input->post('rbtnPrdoem'),
'prd_protype' => $this->input->post('rbtnPrdprotype'),
'prd_image' => $prdimage,
'prd_ranktype' => $this->input->post('sbPrdranktype'),
'prd_points' => $this->input->post('txtPrdpoints'),
'prd_extrakey' => $this->input->post('txtPrdextrakey'),
'prd_dated' => time(),
'prd_ipadd' => $_SERVER['REMOTE_ADDR']
);
$result_id = $this->b2bproduct_model->add($data);
if ($result_id) {
redirect(base_url() . 'moderator/b2bproduct/view/' . $result_id, 'refresh');
} else {
$data_view["error"] = "Data can't insert due to database error";
$this->load->view('moderator/templates/header');
$this->load->view('moderator/templates/sidebar');
$this->load->view('moderator/b2bproduct_add', $data_view);
$this->load->view('moderator/templates/footer');
}
}
}
Resize function
public function resize($source, $destination, $width, $height) {
$config['image_library'] = 'gd2';
$config['source_image'] = $source;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$config['new_image'] = $destination;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
First of all you are loading library two times in your function add please load it one time probably at the top of function.
in resize use $this->image_lib->initialize($config) as below
public function resize($source, $destination, $width, $height) {
$config['image_library'] = 'gd2';
$config['source_image'] = $source;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$config['new_image'] = $destination;
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->resize();
}
Is it possible that the folders for your thumbs (assets/images/b2bproduct/thump/ and assets/images/b2blogo/thump/) do not exist?
It is very likely the reason to be a simple spelling mistake like thump instead of thumb.
EDIT:
You really don't have to load the upload and image_lib libraries so many times. Do it once at the beginning. After that you can use $this->upload->initialize($config); or $this->image_lib->initialize($config); to all these places where now you are trying to re-load the libraries.
To make your code works you should at least add $this->image_lib->initialize($config); before $this->image_lib->resize(); in your resize function.

codeigniter success insert to database but failed to insert uploaded file

I'm new here, so nice to meet you all.
I have trouble with codeigniter.
Maybe hard to tell so I will show all of my code :
my controller :
function upload_barang(){
$this->admin_model->login();
$this->admin_model->valid_product();
$config['upload_path'] = './produk/';
$config['allowed_types'] = 'jpg';
$config['max_size'] = '100';
$config['max_width'] = '400';
$config['max_height'] = '400';
$this->load->library('upload',$config);
$data['query'] = $this->db->get('kategori');
if($this->form_validation->run() == FALSE && ! $this->upload->do_upload()) {
$data['error'] = $this->upload->display_errors();
$data['success'] = '';
$this->load->view('backoffice/tambahbarang',$data);
} else {
$upload_data = $this->upload->data();
$data['error'] = '';
$data = array(
'idkategori' => $this->input->post('idkategori'),
'namabarang' => $this->input->post('nbarang'),
'image' => $upload_data['file_name'],
'status' => 1
);
$data_insert = $this->db->insert('barang',$data);
if($data_insert == TRUE) {
$data['query'] = $this->db->get('kategori');
$query = $this->db->get('barang');
$rows = $query->row();
$data['idbarang'] = $rows->idbarang;
$harga = array(
'idbarang' => $data['idbarang'],
'satuan' => $this->input->post('stcbarang'),
'harga' => $this->input->post('hbarang')
);
$this->db->insert('hargabarang',$harga);
$data['success'] = '<b>Barang Telah Ditambahkan</b>';
$this->load->view('backoffice/tambahbarang',$data);
} else {
$data['success'] = '<b>Barang Gagal Ditambahkan</b>';
$this->load->view('backoffice/tambahbarang',$data);
}
}
}
This code success insert all of data to database, except on column 'image' that I fill with '$upload_data['file_name']' this empty on my database.
what's wrong with my code?
please help thanks
You can use this format:
/*
* ------- Start Image Upload---------
*/
$config['upload_path'] = 'image/product_images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '400';
$config['max_height'] = '400';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$error='';
$fdata=array();
if ( ! $this->upload->do_upload('product_image'))
{
$error = $this->upload->display_errors();
echo $error;
exit();
}
else
{
$fdata = $this->upload->data();
$data['product_image']=$config['upload_path'] .$fdata['file_name'];
}
/*
* --------End Image Upload---------
*/
$this->super_admin_model->save_product_info($data);

csv file is uploaded locally but not online in codeigniter

my csv file is locally uploading perfectly but when i upload it on my website then its not uploading following is my upload function
function upload_title()
{
if(isset($_POST['upload_titles'])) // check if submit button is clicked
{
$config['upload_path'] = 'uploads/title/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '5000';
$config['overwrite'] = TRUE;
$config['encrypt_name'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->do_upload();
$temp = $this->upload->data('userfile');
$article_titlelist = $temp['file_name'];
if($this->upload->do_upload())
{
$this->load->library('csvreader');
$result = $this->csvreader->parse_file('./uploads/title/'.$article_titlelist);
foreach($result as $val)
{
$articletitle = $val['title'];
$title_category = $val['category'];
$mDate = date('Y-m-d H:i:s');
$data = array(
'article_title' => $articletitle,
'title_category' => $title_category,
'article_cdate' => $mDate,
'article_mdate' => $mDate
);
$this->article_model->addtitle($data);
}
redirect('admin/article/title_listing');
}
}
$error = array('error' => $this->upload->display_errors());
$data['page_title'] = 'Article Title';
$data['content'] = $this->load->view('admin/add_title', $error, true);
$this->load->view('admin/template', $data);
}
i have check it online its not going in do_upload condition

How to update image data with form_upload in codeigniter

How to edit this data with image inside.Data have been updated when i press the button "submit", but the old image can not change with the new image
if($this->input->post('go_upload'))
{
validation.......
$IdPerumahan = $this->uri->segment(3);
if ($this->form_validation->run() == TRUE)
{
$lokasi_file = $_FILES['userfile']['tmp_name'];
$nama_file = $_FILES['userfile']['name'];
$ukuran_file = $_FILES['userfile']['size'];
$direktori_file = "./images/$nama_file";
if (empty($lokasi_file))
{
$this->load->model('PerumahanMDL');
$this->PerumahanMDL->SimpanUbahPerumahan($IdPerumahan);
redirect('PerumahanCON');
} else
{
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg|png';
$config['max_size'] = '2000';
$config['overwrite'] = 'true';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$field_name = "userfile";
if(!$this->upload->do_upload("userfile"))
{
$data['data']= 'TambahPerumahanVW';
$this->load->view('Desain2',$data);
}else
{
$asdf = $this->upload->data();
$config = array(
'source_image' => $asdf['full_path'],
'new_image' => './images/thumb',
'maintain_ration' => true,
'width' => 180,
'height' => 120
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$userfile = $_FILES['userfile']['name'];
$this->load->model('PerumahanMDL');
$this->PerumahanMDL->UbahDataPerumahan($userfile,$IdPerumahan);
redirect('PerumahanCON');
}
}
}
}
}
Add overwrite = true.
$this->load->library('upload', $config);
$this->upload->overwrite = true;

Resources