codeigniter image resize not working - codeigniter-2

can't understand why this image resizing not working pls help
//updating article
function updateArticle(){
$data = array(
'a_title' =>$_POST['a_title'],
'a_description' =>$_POST['a_description'],
'a_flash_news' => $_POST['a_flash_news'],
'a_content' =>$_POST['a_content'],
//'a_views' => $_POST['a_views'],
'a_image_caption' =>$_POST['a_image_caption'],
'a_audio_caption' =>$_POST['a_audio_caption'],
'a_video' =>$_POST['a_video'],
'a_video_caption' =>$_POST['a_video_caption'],
'a_channel' =>$_POST['a_channel'],
'a_grouping' =>$_POST['a_grouping'],
'a_status' =>$_POST['a_status'],
'a_breaking' =>$_POST['a_breaking'],
'a_hot' =>$_POST['a_hot'],
'a_category_id' =>$_POST['a_category_id'],
'a_featured' =>$_POST['a_featured'],
'a_tags' =>$_POST['a_tags'],
'a_author' =>$_POST['a_author'],
'a_date' =>$_POST['a_date']
);
//UPLOAD IMAGE
//some $config vars for image
$config['upload_path'] = './images/articles';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '0';
$config['remove_spaces'] = true;
$config['overwrite'] = false;
$config['max_width'] = '0';
$config['max_height'] = '0';
//for image resize
$config['image_library'] = 'gd2';
$config['maintain_ratio'] = TRUE;
$config['width'] = 320;
$config['height'] = 320;
$this->load->library('upload', $config);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
//upload main image
if(!$this->upload->do_upload('a_image')){
//$e = $this->upload->display_errors();
//print_r($e);
}
$image = $this->upload->data();
if($image['file_name']){
$data['a_image'] = "images/articles/". $image['file_name'];
}
//UPLOAD THUMBNAIL
unset($config);
//now upload thumb
//some $config vars for thumb
$config['upload_path'] = './images/articles/thumb';
$config['allowed_types'] = 'gif|jpg|jpeg|png|wav';
$config['max_size'] = '0';
$config['remove_spaces'] = true;
$config['overwrite'] = false;
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->upload->initialize($config);
if(!$this->upload->do_upload('a_thumbnail')){
//$e = $this->upload->dispaly_errors();
//print_r($e);exit();
}
$thumb = $this->upload->data();
if($thumb['file_name']){
$data['a_thumbnail'] = "images/articles/thumb/". $thumb['file_name'];
}
//UPLOAD AUDIO
unset($config);
//now upload thumb
//some $config vars for thumb
$config['upload_path'] = './audio';
$config['allowed_types'] = 'mp3|gif|jpg|jpeg|png|wav';
$config['max_size'] = '0';
$config['remove_spaces'] = true;
$config['overwrite'] = false;
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->upload->initialize($config);
if(!$this->upload->do_upload('a_audio')){
//$e = $this->upload->dispaly_errors();
//print_r($e);exit();
}
$thumb = $this->upload->data();
if($thumb['file_name']){
$data['a_audio'] = "audio/". $thumb['file_name'];
}
//goes at last
$this->db->where('id',$_POST['id']);
$this->db->update('articles', $data);
}

what error are you getting?
have the upload paths 777 permissions?
I think if you need to rezise the images you need to set 777 permissions on the upload path.

Related

How to validate upload file only pdf

I would like to validate file upload, the condition is file input can upload file if file input is empty, but if file input does not match the type of file setting then the error showing. How to make these error. if I using this script : !$this->upload->do_upload('file') and i let file input empty, certainly the error are showing.
this is my Config upload :
$config['upload_path'] = '../../upload/file';
$config['allowed_types'] = 'pdf';
$config['file_name'] = 'file_'.time();
$config['overwrite'] = true;
$this->upload->initialize($config);
if(!$this->upload->do_upload('file')){
$respond['error'] = true;
$respond['message'] = "Error Upload".$this->upload->display_errors();
}
You could add a condition, so it will only validate if there are uploaded file :
<?php
if ( $_FILES && $_FILES['file']['name'] )
{
$config['upload_path'] = '../../upload/file';
$config['allowed_types'] = 'pdf';
$config['file_name'] = 'file_'.time();
$config['overwrite'] = true;
$this->upload->initialize($config);
if(!$this->upload->do_upload('file')){
$respond['error'] = true;
$respond['message'] = "Error Upload".$this->upload->display_errors();
}
}
This solution will work perfectly for you
$config['upload_path'] = './uploads/;
$config['allowed_types'] = 'pdf';
$config['max_size'] = 0;
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['file_name'] = round(100,99); //make your file name random
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('file_name')){
print $this->upload->data();
}else{
print $this->upload->display_errors();
}

I want to resize uploaded image in codeigniter but it is not working

I want to resize uploaded image in codeigniter but it is not working with this code. Image uploading successfully but I want to resize it to 110x110 and display it !
my code is here
class Upload_photo extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index(){
session_start();
$u = $_SESSION['username'];
$config['upload_path'] = 'user/'.$u.'/'.$filename;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '30000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload',$config);
if(!$this->upload->do_upload('file1'))
{
echo "Error". $this->upload->display_errors();
}
else {
$config['image_library'] = 'GD2';
$config['source_image'] = $config['upload_path'];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 110;
$config['height'] = 110;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
echo 'Photo Uploaded Successfully';
}
}
}
First you upload the original image so set the configs and upload the original image, then you resize the image. With that said:
$fullPath = 'user/' . $u . '/';
$config['upload_path'] = $fullPath;
$config['file_name'] = 'theNameofFile';
$config['allowed_types'] = 'jpg|png|bmp';
$config['max_size'] = '30000';
$config['overwrite'] = FALSE;
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if($this->upload->do_upload('file1') == FALSE)
{
echo "Error:" . $this->upload->display_errors();
return;
}
At this point the the file was uploaded. Now you need to grab it and resize.
// Reset
$config = array();
$config['image_library'] = 'gd2';
$config['source_image'] = $fullPath . $this->upload->data()['file_name'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 100;
$config['height'] = 100;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
Finally, if you wish to, you can delete the original image.
If you want both images 1>uploded image 2>Thumbnail Image and want to store Thumbnail image to different folder you should use $config['new_image'] and path should not your url but should be absolute server path. If you don't know how to get absolute server path ? you can see my code below.
I got the solution, my code is here
class Upload_photo extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index(){
session_start();
$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'];
//echo $filename;
//echo $source;
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();
}
}
}

CodeIgniter upload multiple files to multiple directories

$configUpload = array();
$configUpload['upload_path'] = './directory_1/';
$configUpload['max_size'] = '6000';
$configUpload['max_width'] = '9500';
$configUpload['max_height'] = '9500';
$configUpload['allowed_types'] = 'png|jpg|jpeg|gif|bmp';
$this->load->library('upload',$configUpload);
$upload_1 = $this->upload->do_upload('product_image');
if($upload_1 === FALSE)
$product_data = $this->upload->data();
continue;
$config = array();
$config['upload_path'] = './directory_2/';
$config['max_size'] = '5000';
$config['max_width'] = '10000';
$config['max_height'] = '10000';
$config['allowed_types'] = 'png|jpg|jpeg|gif|bmp';
$this->load->library('upload', $config);
$upload = $this->upload->do_upload('product_image_2');
When I submit to upload, image files are in the same directory (directory_1).
Need To initialize your class
$configUpload = array();
$configUpload['upload_path'] = 'uploads/canvas_uploads/upload_save/';
$configUpload['allowed_types'] = 'png|jpg|jpeg|gif|bmp';
$configUpload['file_name'] = date("Ymd") . time();
$configUpload['overwrite'] = true;
$this->load->library('upload', $configUpload);
$this->upload->initialize($configUpload);
$upload_1 = $this->upload->do_upload('file_input');
$config['upload_path'] = 'uploads/canvas_uploads';
$config['allowed_types'] = 'jpg|png|jpeg|gif|bmp|tif';
$config['file_name'] = date("Ymd") . time();
$config['overwrite'] = true;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->do_upload("file_input");

how to define 2 config upload file in codeigniter

I want to upload 2 file with 2 config section but first file don't work and second file work good for me. how can i find problem in this code.
Please help me.
This is my code :
$this->load->library('upload');
$config['upload_path'] = './uploads/images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '200';
$config['max_width'] = '200';
$config['max_height'] = '200';
$config['file_name'] = time();
$this->upload->initialize($config); // Important
if ($this->upload->do_upload('logo')) {
$logo = $this->upload->data();
} elseif ($this->input->post('logo')) {
$logo['file_name'] = $this->input->post('logo');
} else {
$logo['file_name'] = '';
}
//===================================================
$config['upload_path'] = './uploads/images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '200';
$config['max_width'] = '950';
$config['max_height'] = '250';
$config['file_name'] = time();
$this->upload->initialize($config); // Important
if ($this->upload->do_upload('header')) {
$header = $this->upload->data();
} elseif($this->input->post('header')) {
$header['file_name'] = $this->input->post('header');
} else {
$header['file_name'] = '';
}

upload image problem at codeigniter

Hello frinds I want to upload two images at two different location but they still get ulpoaded at same place. Please help
//upload images
//some $config vars for image
$config['upload_path'] = './images/articles';
$config['allowed_types'] = 'gif|jpg|jpeg|png|mp3|wav';
$config['max_size'] = '0';
$config['remove_spaces'] = true;
$config['overwrite'] = false;
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
//upload main image
if(!$this->upload->do_upload('a_image')){
$e = $this->upload->display_errors();
//print_r($e);
}
$image = $this->upload->data();
if($image['file_name']){
$data['a_image'] = "images/articles/". $image['file_name'];
}
unset($config);
//now upload thumb
//some $config vars for thumb
$config['upload_path'] = './images/articles/thumb';
$config['allowed_types'] = 'gif|jpg|jpeg|png|mp3|wav';
$config['max_size'] = '0';
$config['remove_spaces'] = true;
$config['overwrite'] = false;
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
//upload thumbnail
if(!$this->upload->do_upload('a_thumbnail')){
$this->upload->dispaly_errors();
}
The second time around, call
$this->upload->initialize($config)
instead of
$this->load->library('upload', $config);
Also , you can reuse the $config array, changing just the values that need changing...

Resources