Image upload properly but not resizing the image:
Here is the html:
<form action="<?php echo base_url();?>index.php/welcome/image_upload" method="post" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" name="send" value="send">
</form>
here is the controller:
public function image_upload()
{
$config['upload_path'] = './upload';
$config['allowed_types'] = 'jpg|png|jpeg';
$config['max_size'] = 2000;
$config['min_size'] = 100;
$config['max_width'] = 1024;
$config['min_width'] = 400;
$config['max_height'] = 1000;
$config['min_height'] = 400;
$config['encrypt_name'] = TRUE;
$this->load->library('upload');
$this->upload->initialize($config);
if ( ! $this->upload->do_upload("image"))
{
echo $this->upload->display_errors();
}else{
$data[] = $this->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = $data[0]["full_path"];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 600;
$config['height'] = 800;
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->resize();
echo $this->image_lib->display_errors();
}
output:
Your server does not support the GD function required to process this type of image.
JPG images are not supported.(when i upload jpg file says, when upload png file then also says not supported).
The problem is solved by installing GD library in my linux system.
Related
Following is my controller In this I am using two if statements one for multiple images and another is for the featured image.. my images are uploaded in a folder very well but multiple names are not inserted in the database...Only one file name is inserted in the database...
public function uploadApi()
{
if (isset($_FILES['userfile'])) {
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 200000;
$config['max_width'] = 2024;
$config['max_height'] = 1768;
$this->upload->initialize($config);
$this->load->library('upload', $config);
$this->upload->do_upload('userfile');
$data = array( $this->upload->data());
$this->m->update_post($data[0]['file_name']);
}
if(isset($_FILES['userfile1'])) {
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 200000;
$config['max_width'] = 2024;
$config['max_height'] = 1768;
$this->upload->initialize($config);
$this->load->library('upload', $config);
$this->upload->do_upload('userfile1');
$data = array( $this->upload->data());
$this->m->update_feature($data[0]['file_name']);
}
}
This is a model ..
#-Update images Post-#
public function update_post($picture) {
$post = array(
'post_images'=>$picture,
);
$this->db
->where('post_status','draft')
->update('post',$post);
return true;
}
public function update_feature($picture) {
$post = array(
'post_featured_image'=>$picture,
);
$this->db
// ->set('post_created', 'NOW()', FALSE)
->where('post_status','draft')
->update('post',$post);
return true;
}
filepond plugin script
FilePond.registerPlugin(
FilePondPluginFileValidateSize,
FilePondPluginImageExifOrientation,
FilePondPluginImageCrop,
FilePondPluginImageResize,
FilePondPluginImagePreview,
FilePondPluginImageTransform
);
// Set default FilePond options
FilePond.setOptions({
// maximum allowed file size
maxFileSize: '50MB',
imagePreviewHeight: 100,
imagePreviewWidth: 200,
instantUpload: true,
// crop the image to a 1:1 ratio
imageCropAspectRatio: '1:1',
// upload to this server end point
server: {
url: '<?php echo base_url() ?>Admin/uploadApi',
}
});
var pond = FilePond.create(document.querySelector('input[name="userfile"]'));
var pond = FilePond.create(document.querySelector('input[name="userfile1"]'));
**This is a view ..**
<form method="post" enctype="multipart/form-data" class="toggle-disabled" action="<?php echo base_url() ?>Admin/update_post1" id='ritesh'>
<div class="col-md-6">
<div class="form-group">
<label>Upload images</label>
<input type="file"
class="filepond"
name="userfile"
multiple
data-max-file-size="5MB"
data-max-files="50" data-validation="required extension" />
</div>
<div class="form-group">
<label>Feature image</label>
<input type="file"
class="filepond"
name="userfile1"
data-max-file-size="5MB"
data-validation="required extension"
/>
</div>
</form>
For multiple image upload you should post images array like; imagename[]. Your current approach is not good.
You must try already posted answers:
Multiple image upload with CodeIgniter
Multiple image upload with Codeigniter saving only one file path to MySQL Database
https://www.codexworld.com/codeigniter-upload-multiple-files-images/
Please try to this in controller
function uploadApi() {
$image = $_FILES;
foreach ($image as $key => $img) {
if (!is_dir('./Uploads/')) {
mkdir('./Uploads/', 0777, TRUE);
}
if (!empty($img['name'])) {
$config['upload_path'] = './Uploads/Products/';
$config['allowed_types'] = '*';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = TRUE;
$config['file_name'] = date('U') . '_' . $img['name'];
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload($key)) {
$error = array('error' => $this->upload->display_errors());
print_r($error);
die;
} else {
if ($this->upload->do_upload($key)) {
$image_data = $this->upload->data();
$update["userfile"] = $config['file_name'];
$res = $this->m->update_post($update);
}
}
}
}
$this->load->view('imgtest');
}
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['maintain_ratio'] = TRUE;
$config['width'] = 100;
$config['height'] = 150;
$config['file_name'] = $_FILES['image']['name'];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->initialize($config);
$this->load->library('upload',$config);
$this->upload->initialize($config);
I am unable to resize image ... please give me my mistake
As per the #jigar Shah comment you have to first initalize upload library before resize
$this->load->library('upload');
$config['upload_path'] = './uploads';
$config['allowed_types'] ='csv|xls|xlsx';
$config['max_size'] = 1024; //in KB
$config['overwrite'] = TRUE;
$config['encrypt_name'] = TRUE;
$config['max_filename'] = 25;
$this->upload->initialize($config);
if (!$this->upload->do_upload('image_name')) {
//if file upload failed then catch the errors
$this->handle_error($this->upload->display_errors());
$is_file_error = TRUE;
} else {
//store the file info
$image_data = $this->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = $image_data['full_path']; //get original image
$config['maintain_ratio'] = TRUE;
$config['width'] = 150;
$config['height'] = 100;
$this->load->library('image_lib', $config);
if (!$this->image_lib->resize()) {
$this->handle_error($this->image_lib->display_errors());
}
}
For more reference please visit
I want to upload large images on my website. I want to reduce the size of those using codeigniter. So I am doing this code
function upload_image($data) {
$config['upload_path'] = './temp/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 10000;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('image')) {
$error = array('error' => $this->upload->display_errors());
pre($error);
} else {
$config = array();
$data = array('upload_data' => $this->upload->data());
$config['image_library'] = 'gd';
$config['source_image'] = './temp/' . $data['upload_data']['file_name'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['quality'] = 50;
$config['new_image'] = './temp/' . $data['upload_data']['file_name'];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
pre($data);
}
}
But images are not being compressed. Original and uploaded size are the same. Where am I wrong?
Maybe exists an error. Check for errors:
if (!$this->image_lib->resize()){
echo $this->image_lib->display_errors();
}
Note: Use gd2 as library (default value is gd2):
$config['image_library'] = 'gd2';
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!
I have a problem , I took me 2 hours but I couldn't solve it.
When I want to upload multiple images (3 images ) it will upload but then I can't create a thumb for all , just for the first picture , I don't know where is the problem
this is my form
<form action="" method="post" enctype="multipart/form-data">
<input type="file" id="hotel_img1" value="upload" name="hotel_img1">
<input type="file" id="hotel_img2" value="upload" name="hotel_img1">
<input type="file" id="hotel_img3" value="upload" name="hotel_img1">
<input type="submit">
</form>
this is my code
controller
<?php
if(!empty($_FILES))
$upload_image = array('1','2','3','4','5');
foreach($upload_image as $i) {
if(!empty($_FILES["hotel_img$i"]['name']))
{
$hotel_ID= 12;
$config['file_name'] = $this->session->userdata('user_id').'-'.$hotel_ID.'-'.time().$i;
$config['upload_path'] =realpath(APPPATH . '../img');
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '512';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = TRUE;
$this->upload->initialize($config);
$form_name = 'hotel_img' . $i;
if(!$this->upload->do_upload($form_name))
{
$data['msg'] .= "ERROR";
}
else
{
$file_data = $this->upload->data();
$image_data['img_url'] = $file_data['file_name'];
$image_data['img_size'] = $file_data['file_size'] * 1024;
$image_data['img_forID'] = $hotel_ID;
$this->model_hotels->insert_image($image_data);
$data['msg'] .= "Uploaded Picture No $i";
$config_thumb['image_library'] = 'gd2';
$config_thumb['source_image'] = $file_data['full_path'];
$config_thumb['maintain_ratio'] = FALSE;
$config_thumb['width'] = 250;
$config_thumb['height'] = 150;
$config_thumb['new_image'] = realpath(APPPATH . '../img').'/thumb/thumb_' . $file_data['file_name'];
$this->load->library('image_lib', $config_thumb);
$this->image_lib->resize();
$this->image_lib->clear();
}
}
}
$this->model_hotels->set_hotel_thumb($hotel_ID);
} ?>
try adding this to your config:
$config['create_thumb']
Seems to me that $file_data is always the same and probably contains an array with the other pictures as well. Now your code is saying for every picture resize $file_data and it only picks the first picture uploaded. Try print_r($file_data) because i guess that's what is causing the problem.