setting width and height of image in codeigniter php not working - php

i have a codeigniter website where user can upload multiple images, i want all the images to be of the same size, i have done the following code in controller:
if (isset($_POST['addblog'])) {
$this->load->library('upload');
$image = array();
$ImageCount = count($_FILES['image_name']['name']);
for ($i = 0; $i < $ImageCount; $i++) {
$_FILES['file']['name'] = $_FILES['image_name']['name'][$i];
$_FILES['file']['type'] = $_FILES['image_name']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['image_name']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['image_name']['error'][$i];
$_FILES['file']['size'] = $_FILES['image_name']['size'][$i];
$uploadPath = './uploads/blog/';
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['width'] = 200;
$config['height'] = 250;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload('file')) {
$imageData = $this->upload->data();
$uploadImgData[] = $imageData['file_name'];
}
}
$blogimage = $uploadImgData;
}
as u can see i have set the height and width in config, the images are still uploaded in ther original size, can anyone please tell me what is wrong in here, thanks in advance

You has just made a little mistake in your config :
$config['max_width'] = 200;
$config['max_height'] = 250;
Sadly "min_width" and "min_height" seems doesn't exist yet

Related

Upload Resize Image CI

I'm setting up the upload file before add resize code. And its normally process, but after adding resize code my controller isn't working. Can you help me with this? I'm trying but it didn't work.
And my another problem, I can't upload image file size more than 2MB.
This for my work on online shop.
public function submit_image()
{
$input_name = $_POST['input_name'];
$input_email = $_POST['input_email'];
$input_code_transaction = $_POST['input_code_transaction'];
$config['file_name'] = $input_code_transaction;
$config['overwrite'] = TRUE;
$config['upload_path'] = './img/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 50000000;
$config['max_width'] = 6000;
$config['max_height'] = 4000;
$this->upload->initialize($config);
if(!empty($_FILES['filefoto']['name'])) {
if ($this->upload->do_upload('doc')) {
$gbr = $this->upload->data();
//Compress Image
$config['image_library']='gd2';
$config['source_image']='./img/'.$gbr['file_name'];
$config['create_thumb']= FALSE;
$config['maintain_ratio']= FALSE;
$config['quality']= '50%';
$config['width']= 1280;
$config['height']= 720;
$config['new_image']= './img/'.$gbr['file_name'];
$this->load->library('upload', $config);
$this->upload->resize();
$data = $upload_data = $this->upload->data();
$input_picture = $this->upload->do_upload('doc');
}
}
}
The upload library has no inbuilt features for image resizing.
This whole section:
$config['image_library']='gd2';
$config['source_image']='./img/'.$gbr['file_name'];
$config['create_thumb']= FALSE;
$config['maintain_ratio']= FALSE;
$config['quality']= '50%';
$config['width']= 1280;
$config['height']= 720;
$config['new_image']= './img/'.$gbr['file_name'];
$this->load->library('upload', $config);
$this->upload->resize();
$data = $upload_data = $this->upload->data();
$input_gambar = $this->upload->do_upload('doc');
needs to reference image_lib the image resizer codeigniter uses: https://www.codeigniter.com/user_guide/libraries/image_lib.html
So:
if ($this->upload->do_upload('doc')) {
$gbr = $this->upload->data();
//Compress Image
$imlib['image_library'] = 'gd2';
$imlib['source_image'] = './img/' . $gbr['file_name'];
$imlib['create_thumb'] = FALSE;
$imlib['maintain_ratio'] = FALSE;
$imlib['quality'] = '50%';
$imlib['width'] = 1280;
$imlib['height'] = 720;
$imlib['new_image'] = './img/resized_' . $gbr['file_name'];
$this->load->library('image_lib', $imlib);
if (!$this->image_lib->resize()) {
show_error($this->image_lib->display_errors());
}
$yourouputimage = './img/resized_' . $gbr['file_name'];
}

how to upload multi file type with one form in codeigniter?

I have one multi-part form. I want to upload 5 images and one video with this form. I do not want using AJAX upload.
$this->form_validation->set_rules('file' , 'lang:pic' , 'callback_multiple_upload');
$this->form_validation->set_rules('video' , 'lang:video' , 'callback_video_upload');
For multiple images
$config['upload_path'] = PATH; //add path according to your requirements
$config['allowed_types'] = 'jpg|jpeg|png';
$config['overwrite'] = false; //OR true
$config['max_size'] = '100000'; //You can change it
$this->load->library('upload');
$files = $_FILES;
$number_of_files = count($_FILES['pic']['name']); //"pic" is name of FILE input
//images name will be details0, details1, details2 and soo on.
$errors = 0;
for ($i = 0; $i < $number_of_files; $i++) {
$_FILES['pic']['name'] = "details" . $i . ".jpg"; //If you want to change the name of images change "details" with your require name
$_FILES['pic']['type'] = $files['pic']['type'][$i];
$_FILES['pic']['tmp_name'] = $files['pic']['tmp_name'][$i];
$_FILES['pic']['error'] = $files['pic']['error'][$i];
$_FILES['pic']['size'] = $files['pic']['size'][$i];
$this->upload->initialize($config);
if (!$this->upload->do_upload("pic"))
{
$errors++;
}
}
if ($errors > 0)
{
echo $errors . "File(s) could not be uploaded";
}
You can initialize the upload class for each file and set the required file type i.e.
$config['upload_path']= './uploads/files/';
$config['allowed_types'] = 'docx|doc|pdf|txt|odt';
$config['max_size'] = 10000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->upload->initialize($config);
$this->upload->do_upload();
//check errors in first upload.
then for video
$config['upload_path']= './uploads/videos/';
$config['allowed_types'] = 'mp4|flv';
$config['max_size'] = 10000;
$this->upload->initialize($config);
$this->upload->do_upload();
For multiple file uploading
$filesCount = count($_FILES['userFiles']['name']);
for($i = 0; $i < $filesCount; $i++){
$_FILES['userFile']['name'] = $_FILES['userFiles']['name'][$i];
$_FILES['userFile']['type'] = $_FILES['userFiles']['type'][$i];
$_FILES['userFile']['tmp_name'] = $_FILES['userFiles']['tmp_name'][$i];
$_FILES['userFile']['error'] = $_FILES['userFiles']['error'][$i];
$_FILES['userFile']['size'] = $_FILES['userFiles']['size'][$i];
$upload_path ="/test/assets/upload";
$output_dir = $_SERVER['DOCUMENT_ROOT']. $upload_path;
$config['upload_path'] = $output_dir;
// $config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'jpg|png|mp4; //add other_extensions';
$config['file_name'] = 'PIC_'.$user_id.'_'.$i;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('userFile')){
$fileData = $this->upload->data();
$img_insert_array = array('user_id'=>$user_id,'profile_pic'=> $fileData['file_name']);
$res = $this->Test_model->update_img($img_insert_array );
//here insert in db
echo "uploaded successful";
}
else {
print_r($this->upload->display_errors());
}
}

Multiple ImageUpload in Code Igniter

I have problem in uploading multiple Image in codeigniter
myview
<tr>
<td> Picture</td>
<td>:</td>
<td><input type="file" name="userfile[]" multiple /></input></td>
</tr>
and my model
function multi_upload_files($imagepath=''){
$number_of_files_uploaded = count($_FILES['userfile']['name']);
for ($i = 0; $i < $number_of_files_uploaded; $i++) {
$_FILES['userfile']['name'] = $_FILES['userfile']['name'][$i];
$_FILES['userfile']['type'] = $_FILES['userfile']['type'][$i];
$_FILES['userfile']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
$_FILES['userfile']['error'] = $_FILES['userfile']['error'][$i];
$_FILES['userfile']['size'] = $_FILES['userfile']['size'][$i];
$config['upload_path'] = $imagepath;
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 900;
$config['max_width'] = 2024;
$config['max_height'] = 1068;
$this->upload->initialize($config);
$fileData = $this->upload->data();
$uploadData[$i]['file_name'] = $fileData['file_name'];
$images[]= $uploadData[$i]['file_name'];
return $images;
} }
I have testing it and the problem this code just read my upload Image just 1 image, not equal to number of my file that just upload (for ex: 2 image)
If you look at the following lines, you just overwrote everything with the first item in the array. Thus, only the first item is processed and the rest are overwritten. Plus, you have a return at the end of the loop. The function returns before the loop can finish.
$_FILES['userfile']['name'] = $_FILES['userfile']['name'][$i];
$_FILES['userfile']['type'] = $_FILES['userfile']['type'][$i];
$_FILES['userfile']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
$_FILES['userfile']['error'] = $_FILES['userfile']['error'][$i];
$_FILES['userfile']['size'] = $_FILES['userfile']['size'][$i];
...
return $images;
You miss two things.
First,variable $imagepath is not defined.
Second, You miss to load upload libaray.
So,Solution is here try it.Hope it will works...
function multi_upload_files($imagepath=''){
$data = array();
$number_of_files_uploaded = count($_FILES['userfile']['name']);
for ($i = 0; $i < $number_of_files_uploaded; $i++) {
$_FILES['userfile']['name'] = $_FILES['userfile']['name'][$i];
$_FILES['userfile']['type'] = $_FILES['userfile']['type'][$i];
$_FILES['userfile']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
$_FILES['userfile']['error'] = $_FILES['userfile']['error'][$i];
$_FILES['userfile']['size'] = $_FILES['userfile']['size'][$i];
$imagepath = 'Define upload path here';
$config['upload_path'] = $imagepath;
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 900;
$config['max_width'] = 2024;
$config['max_height'] = 1068;
//Load library first then use
$this->load->library('upload');
$this->upload->initialize($config);
if($this->upload->do_upload('userfile'))
{
$fileData = $this->upload->data();
$uploadData[$i]['file_name'] = $fileData['file_name'];
$images[]= $uploadData[$i]['file_name'];
}
return $images;
}

codeigniter image resize only working with px on $config_img

I'm trying to upload an image and everything is working fine, except the width and heigh of the resize. the strange part is, if I add px on the $config_img['width'] and $config_img['height'] I got an error saing:
"A non well formed numeric value encountered
Filename: libraries/Image_lib.php"
What is obvious because the px on the value, but this way the image do resize.
Here is my upload image function:
function upload_foto_acompanhe_sua_obra($field) {
$dir = realpath('assets/uploads/acompanhe_sua_obra');
$config['upload_path'] = $dir;
$config['allowed_types'] = 'gif|jpg|png';
$config['encrypt_name'] = TRUE;
$config['max_size'] = '500000';
$config['max_width'] = '10024';
$config['max_height'] = '7068';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$field_name = $field;
if ($this->upload->do_upload($field_name)) {
$dados = $this->upload->data();
$this->load->library('image_lib');
$this->image_lib->clear();
$size = getimagesize($dados['full_path']);
$config_img['image_library'] = 'GD2';
$config_img['source_image'] = $dados['full_path'];
$config_img['create_thumb'] = FALSE;
$config_img['maintain_ratio'] = TRUE;
$config_img['encrypt_name'] = TRUE;
$config_img['width'] = '1092px';
$config_img['height'] = '295px';
$this->image_lib->initialize($config_img);
$this->image_lib->resize();
// Returns the photo name
return $dados['file_name'];
} else {
$error = array('error' => $this->upload->display_errors());
return $error;
}
}
That's it, any help will be appreciated! Thanks!

Upload array of files with thumbnail

Im trying to upload multiple files from a single form but for some reason i get "The upload path does not appear to be valid." I have checked permissions and they are correct, so i dont know if this is a problem with the images it self.. these are the functions i use to create the images
<input type="file" name="images[]" multiple id="img"></input>
function upload_image(){
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 2*1024;
$config['max_width'] = '1024';
$config['max_height'] = '1024';
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('images'))
{
$error = array('error' => $this->upload->display_errors());
return $error;
}
else
{
for($i = 0, $t = count($_FILES['images']); $i < $t; $i++) {
$id = $this->files->create_thumb($i);
}
}
}
function create_thumb($i){
$file = $this->upload->data();
$files = $file['file_name'];
$fields = array('files' => $files[$i]);
print_r($fields);
exit;
for ($i = 0; $i < count($files); $i++) {
$config['image_library'] = 'gd2';
$config['source_image'] = 'files/images/'.$files[$i];
$config['new_image'] = 'files/images/thumbs/'.$files[$i];
$config['thumb_marker'] = '';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 120;
$config['height'] = 120;
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
}
}
Move your config to config/upload.php. As an alternative solution, you can use the following line of code to initialize the config after loading the library:
$this->upload->initialize($config);
From the documentation:
If you prefer not to set preferences using the above method, you can instead put them into a config file. Simply create a new file called the upload.php, add the $config array in that file. Then save the file in: config/upload.php and it will be used automatically. You will NOT need to use the $this->upload->initialize function if you save your preferences in a config file.
To check if your directory is accessible by PHP, you can use is_dir():
var_dump(is_dir('/path/to/upload/folder/'));
Hope this helps!

Categories