I have a problem with CI class image, image does not resize...
for example :
controller
private function _do_upload(){
$config['upload_path'] = 'upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1000; //set max size allowed in Kilobyte
$config['max_width'] = 1000; // set max width image allowed
$config['max_height'] = 1000; // set max height allowed
$config['file_name'] = round(microtime(true) * 1000);
$this->load->library('upload', $config);
if($this->upload->do_upload('photo')) //upload and validate{
$config2['image_library'] = 'gd2';
$config2['source_image'] = $this->upload->upload_path;
$config2['create_thumb'] = TRUE;
$config2['maintain_ratio'] = TRUE;
$config2['width'] = 450;
$config2['height'] = 500;
$this->load->library('image_lib', $config2);
if(!$this->image_lib->resize()){
$data['inputerror'][] = 'photo';
$data['error_string'][] = 'Upload error: '.$this->upload->display_errors('','');
$data['status'] = FALSE;
}
return $this->upload->data('file_name');
}else{
$data['inputerror'][] = 'photo';
$data['error_string'][] = 'Upload error: '.$this->upload->display_errors('',''); //show ajax error
$data['status'] = FALSE;
echo json_encode($data);
exit();
}
}
But i got no error, I dont know where is the mistake?
Well you have a few issues...
1. You have commented out your opening { which would break your code.
if($this->upload->do_upload('photo')) //upload and validate {
Should be
if($this->upload->do_upload('photo')) { //upload and validate
2. The next part: If you do have a resize error you are not doing anything with your messages... So...
if(!$this->image_lib->resize()){
$data['inputerror'][] = 'photo';
$data['error_string'][] = 'Upload error: '.$this->upload->display_errors('','');
$data['status'] = FALSE;
}
Needs the added json_encode like..
if(!$this->image_lib->resize()){
$data['inputerror'][] = 'photo';
$data['error_string'][] = 'Upload error: '.$this->upload->display_errors('','');
$data['status'] = FALSE;
echo json_encode($data);
exit();
}
In reference to your resizing, you do need to supply the images full filename and not just the path where "files" live...
Your source_image is a Path to where the files are uploaded to. Not the Path/Filename of the image you want to resize...
$config2['source_image'] = $this->upload->upload_path;
So if you add this line that gives you the full path and filename and use it...
$file_data = $this->upload->data();
$config2['source_image'] = $file_data['full_path'];
Does that work better?
Related
I get a module like a blog, and I must upload an image to my website. but when I upload my image not be resized/cropped automatically.
CONTROLLER
function simpan_campaign(){
$config['upload_path'] = './assets/images/upload'; //path folder
$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp'; //type yang dapat diakses bisa anda sesuaikan
$config['encrypt_name'] = TRUE; //Enkripsi nama yang terupload
$this->upload->initialize($config);
if(!empty($_FILES['filefoto']['name'])){
if ($this->upload->do_upload('filefoto')){
$gbr = $this->upload->data();
//Compress Image
$config['image_library']='gd2';
$config['source_image']='./assets/images/upload'.$gbr['file_name'];
$config['create_thumb']= FALSE;
$config['maintain_ratio']= FALSE;
$config['quality']= '50%';
$config['width']= 380;
$config['height']= 264;
$config['new_image']= './assets/images/upload'.$gbr['file_name'];
$this->load->library('image_lib', $config);
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();
$image=$gbr['file_name'];
$title=$this->input->post('title');
$cashtarget=$this->input->post('cashtarget');
$campcode=$this->input->post('campcode');
$datefrom=$this->input->post('datefrom');
$dateend=$this->input->post('dateend');
$category=$this->input->post('category');
$desc=$this->input->post('description');
$this->main_model->save_campaign($title,$desc,$image,$cashtarget,$campcode,$datefrom,$dateend,$category);
echo "Image berhasil diupload";
redirect('account/add');
}
}else{
echo "Image yang diupload kosong";
}
}
and my model like :
MODEL
function save_campaign
($title,$desc,$image,$cashtarget,$campcode,$datefrom,$dateend,$category){
$hsl=$this->db->query("INSERT INTO tcampaign (title,description,pathimage,cashtarget,campcode,datefrom,dateend,category) VALUES ('$title','$desc','$image','$cashtarget','$campcode','$datefrom','$dateend','$category')");
return $hsl;
}
I can upload but i cant resize or crop on my view
I suspect that the biggest problem was this declaration:
'./assets/images/upload'.$gbr['file_name']; if you notice there is no ending slash before the filename...
Also you used the same variable $config for both upload and resizing. This can also cause some unexpected results. Just use a different variable for both. Here we have $upconfig and $config.
I've also cleaned up the function a bit and added in the error methods so you can see what is going wrong if something does. You should handle the errors more elegantly than just echoing them.
function simpan_campaign() {
$this->load->library('upload'); // not sure if you already autoloaded this
// this way $config won't overwrite or add on to the upload config
$upconfig['upload_path'] = './assets/images/upload/'; // missing end slash
$upconfig['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
$upconfig['encrypt_name'] = TRUE;
$this->upload->initialize($upconfig);
if (!empty($_FILES['filefoto']['name'])) {
if ($this->upload->do_upload('filefoto')) {
$filename = $this->upload->data('file_name');
//Compress Image
$config['image_library'] = 'gd2';
$config['source_image'] = $this->upload->data('full_path'); // missing slash before name
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = FALSE;
$config['quality'] = '50%';
$config['width'] = 380;
$config['height'] = 264;
// not required as you have declared the same filename
// the original image will be targeted for resize
//$config['new_image'] = './assets/images/upload/' . $filename;
$this->load->library('image_lib');
$this->image_lib->clear();
$this->image_lib->initialize($config);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
exit;
}
$title = $this->input->post('title');
$cashtarget = $this->input->post('cashtarget');
$campcode = $this->input->post('campcode');
$datefrom = $this->input->post('datefrom');
$dateend = $this->input->post('dateend');
$category = $this->input->post('category');
$desc = $this->input->post('description');
$this->main_model->save_campaign($title, $desc, $filename, $cashtarget, $campcode, $datefrom, $dateend, $category);
echo "Image berhasil diupload";
redirect('account/add');
} else {
echo $this->upload->display_errors();
exit;
}
} else {
echo "Image yang diupload kosong";
}
}
I am trying to upload the multiple images through my form. I have taken care of many expect like form_open_multipart, calling a name as Array in upload input.
I have no issue with the images upload. I can upload multiple images successfully, but the problem comes when I select any text or non-image file with the images.
I am checking scenarios that if user mixes it up (accidently select any other file with images) images with text file then some files are getting uploaded and then I get the error of the wrong type of file.
In Ideal case, it may not allow uploading the file if any one file does not have the correct file type.
so can any one help me over that how can I check before file upload that all files are allowed type or not?
I tried many examples but get the same problem In all.
There is no problem if i upload the multiple images. It's work fine.
<input type="file" name="pic_url[]" multiple accept="image/*" />
And this below is my controller
$config['upload_path'] = './assets/prop_pic/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 20000;
$config['encrypt_name'] = TRUE;
$config['detect_mime'] = TRUE;
//initialize upload class
$this->load->library('upload', $config);
$upload_error = array();
for ($i = 0; $i < count($_FILES['pic_url']['name']); $i++) {
$_FILES['pic_url']['name'] = $_FILES['pic_url']['name'][$i];
$_FILES['pic_url']['type'] = $_FILES['pic_url']['type'][$i];
$_FILES['pic_url']['tmp_name'] = $_FILES['pic_url']['tmp_name'][$i];
$_FILES['pic_url']['error'] = $_FILES['pic_url']['error'][$i];
$_FILES['pic_url']['size'] = $_FILES['pic_url']['size'][$i];
if (!$this->upload->do_upload('pic_url')) {
// fail
$upload_error = array('error' => $this->upload->display_errors());
//$this->load->view('multiple_upload_view', $upload_error);
$this->session->set_flashdata('upload_error', $upload_error);
redirect('property_master/view_prop_master_form');
break;
}
}
First problem is primarily down to how you handle your errors.
if (!$this->upload->do_upload('pic_url')) {
// fail
$upload_error = array('error' => $this->upload->display_errors());
//$this->load->view('multiple_upload_view', $upload_error);
$this->session->set_flashdata('upload_error', $upload_error);
redirect('property_master/view_prop_master_form');
break;
}
The above code will simply halt the rest of the for loop and redirect on the first error.
A simple solution would be to change that to log failures and continue processing:
$config['upload_path'] = './assets/prop_pic/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 20000;
$config['encrypt_name'] = TRUE;
$config['detect_mime'] = TRUE;
//initialize upload class
$this->load->library('upload', $config);
$upload_error = array();
for ($i = 0; $i < count($_FILES['pic_url']['name']); $i++) {
$_FILES['pic_url']['name'] = $_FILES['pic_url']['name'][$i];
$_FILES['pic_url']['type'] = $_FILES['pic_url']['type'][$i];
$_FILES['pic_url']['tmp_name'] = $_FILES['pic_url']['tmp_name'][$i];
$_FILES['pic_url']['error'] = $_FILES['pic_url']['error'][$i];
$_FILES['pic_url']['size'] = $_FILES['pic_url']['size'][$i];
if (!$this->upload->do_upload('pic_url')) {
$upload_error[$i] = array('error' => $this->upload->display_errors());
}
}
if (!empty($upload_error)) {
$this->session->set_flashdata('upload_error', $upload_error);
redirect('property_master/view_prop_master_form');
}
Alternatively, you would need to look at manually validating files based on their file extension.
$config['upload_path'] = './assets/prop_pic/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 20000;
$config['encrypt_name'] = TRUE;
$config['detect_mime'] = TRUE;
//initialize upload class
$this->load->library('upload', $config);
$upload_error = array();
foreach ($_FILES['pic_url'] as $key => $file) {
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
if (strpos($config['allowed_types'], $ext) === false) {
$upload_error[$key] => array('error' => sprintf('<p>Invalid file extension (%s)</p>', $ext));
continue;
}
$_FILES['userfile'] = $file;
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile')) {
$upload_error[$key] = array('error' => $this->upload->display_errors());
} else {
// do something with $this->upload->data();
}
}
if (!empty($upload_error)) {
$this->session->set_flashdata('upload_error', $upload_error);
redirect('property_master/view_prop_master_form');
}
Hope that helps
Here my controller file this working fine except in thumbs folder name get changed to converted_thumb
example:
my original image name is converted.jpg but in thumbs folder it save as converted_thumb so i want to remove _thumb from image name please solve this issue
public function do_upload() {
$config['upload_path'] = './uploads'; // uploaded file store here
$config['allowed_types'] = 'jpg|png|jpeg|gif';
$config['max_size'] = ' 2097152';
$this->load->library('upload', $config);
if ($this->upload->do_upload()) {
$data = $this->upload->data();
//create copy of image
$configs['image_library'] = 'gd2';
$configs['source_image'] = $data['full_path'];
$configs['new_image'] = 'uploads/thumbs/'; //resize image will save here
$configs['create_thumb'] = 'false';
$configs['width'] = '250';
$configs['height'] = '250';
$this->load->library('image_lib', $configs);
$this->image_lib->resize();
$image_name = $data['file_name'];
//$full_path = $data['full_path'];
$post = array(
'product_name' => $image_name,
'product_path' => $configs['new_image'].$image_name
);
$this->db->insert('project', $post);
} else {
echo $this->upload->display_errors();
}
}
}
Why wouldn't you like the _thumbs from that? It is what it is for. Anyway do this.
rename("<?php echo base_url()?>/uploads/thumbs/YOUR_FILE_NAME_THUMBS", "<?php echo base_url()?>/uploads/thumbs/YOUR_FILE_NAME");
Try thumb_marker in your $configs array.
$configs['image_library'] = 'gd2';
$configs['source_image'] = $data['full_path'];
$configs['new_image'] = 'uploads/thumbs/'; //resize image will save here
$configs['create_thumb'] = 'false';
$configs['thumb_marker'] = ''; //Add this in your config array empty string
Then '_thumb' will not add in your newly created files. Source for more detail.
Just add thumb_marker to your config array and set it to FALSE
$configs['image_library'] = 'gd2';
$configs['source_image'] = $data['full_path'];
$configs['new_image'] = 'uploads/thumbs/'; //resize image will save here
$configs['create_thumb'] = 'false';
$configs['thumb_marker'] = FALSE; //this will remove the "_thumb" to your thumb image name
$configs['width'] = '250';
$configs['height'] = '250';
on this way i can upload image and pdf..
but i want to download the pdf or image from my view...please someone give me the code of downloading pdf or image from view..give me the full code
public function save_about_1() {
$about_1_image_info = $this->w_model->select_about_1_image();
$image_path = explode(base_url(), $about_1_info->about_1_link, 2);
unlink($image_path[1]);
$this->sa_model->delete_about_1($about_1_info->about_1_id);
$data = array();
/* Uplod start */
$config['upload_path'] = 'images/about_1/';
$config['allowed_types'] = 'gif|jpg|png|pdf|doc|xml';
$config[ 'overwrite'] = TRUE;
$config['max_size'] = '10000kb';
$config['max_width'] = '100240';
$config['max_height'] = '76800';
$error = array();
$fdata = array();
$this->load->library('upload', $config);
if (!$this->upload->do_upload('about_1_link')) {
$error = $this->upload->display_errors();
$edata = array();
$edata['error_message'] = $error;
$this->session->set_userdata($edata);
redirect('super_admin/about_1');
} else {
$fdata = $this->upload->data();
$data['about_1_link'] = base_url() . $config['upload_path'] . $fdata['file_name'];
$this->sa_model->save_about_1_info($data);
$sdata = array();
$sdata['message'] = "Saved Image Successfully";
$this->session->set_userdata($sdata);
redirect('super_admin/about_1');
}
}
You can use the CI download helper for this.
$data = file_get_contents("/path/to/photo.jpg"); // Read the file's contents
$name = 'myphoto.jpg';
force_download($name, $data);
From the CI User Guide.
https://ellislab.com/codeigniter/user-guide/helpers/download_helper.html
I would like to upload one image to different directory in CodeIgniter, so one images are stored in 2 folders.
Path one etc/www/image1/ and path two etc/www/image2/
Code
$config[‘upload_path’] =‘etc/www/image1/’;
$config[‘allowed_types’] = ‘jpg|jpeg|gif|png’;
$config[‘file_name’]=“imageone.jpg”;
$config[‘max_size’] = ‘10000’;
$this->upload->initialize($config);
if(!$this->upload->do_upload(‘userfile’)){
echo $this->upload->display_errors();
}else {
$this->upload->data(‘userfile’);
}
Just use PHP's Copy() function after you have uploaded the file successfully in your CI controller method...
EX:
$file = '/www/image1/example.txt';
$newfile = '/www/image1/example.txt';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
$config['upload_path'] = FCPATH.'upload/';
$config['allowed_types'] = FCPATH.'gif|jpg|jpeg|png';
$config['encrypt_name'] = TRUE;
$this->load->library('upload',$config);
$upload = $this->upload->do_upload("resim");
if($upload){
$resim = $this->upload->data();
$resimadi = $resim['file_name'];
$resimkayit = 'upload/'.$resimyolu.'';
$resimhotnews = 'upload/hotnews'.$resimadi.'';
$resimlastnews = 'upload/lastnews'.$resimadi.'';
$config['image_library'] = 'gd2';
$config['source_image'] = 'upload/'.$resimadi.'';
$config['new_image'] = 'upload/hotnews'.$resimadi.'';
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = FALSE;
$config['quality'] = '%80';
$config['width'] = 500;
$config['height'] = 350;
$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
//tmb folder resim uploading end
//mini folder resim uploading start
$config1['image_library'] = 'gd2';
$config1['source_image'] = 'upload/'.$resimadi.'';
$config1['new_image'] = 'upload/lastnews/'.$resimadi.'';
$config1['create_thumb'] = FALSE;
$config1['maintain_ratio'] = FALSE;
$config1['quality'] = '%80';
$config1['width'] = 200;
$config1['height'] = 150;
$this->load->library('image_lib', $config1);
$this->image_lib->initialize($config1);
$this->image_lib->resize();
$this->image_lib->clear();
//on mac computers you have to give a writable permission for the folders