Images can be uploaded into a specific folder in the server by checking whether a folder with given name is exists or not. If exists, that folder can be deleted and uploaded the desired images. And also in some cases we may need to create "Thumbnails" (Small images) of the original image. Steps for uploading images with thumbnails are given below.
1. Path to the folder to upload images should be specified. If the folder is in the root folder name of that folder can be used as the path.
$path = 'images';
Or else, a sub folder within the images folder can be given as follows,
$folder_name = $ad_id;
$path = 'images/'.$folder_name;
2. Checking whether a folder exists with the given name and if exists deleting it with its sub folders.
if(file_exists($path)){
$path = rtrim($path, '/') . '/';
$items = glob($path . '*');
foreach($items as $item)
{
is_dir($item) ? removeDir($item) : unlink($item);
}
rmdir($path);
}
3. Then create the folder with the given name
mkdir($path);
4. Next set configurations for the image
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$config['file_name'] = $next_ad_id;
$this->load->library('upload', $config);
$this->load->library('image_lib');
5. Setting Configurations for Thumbnail
$configThumb = array();
$configThumb['image_library'] = 'gd2';
$configThumb['source_image'] = '';
$configThumb['create_thumb'] = TRUE;
$configThumb['maintain_ratio'] = TRUE;
$configThumb['width'] = 140;
$configThumb['height'] = 210
6. Uploading images. HTML file input elements should have the name as 'photo1, photo2...'. Here I have 6 file input elements. So, images are uploaded in a loop.
for($i = 1; $i < 7; $i++) {
$upload = $this->upload->do_upload('photo'.$i);
if($upload == FALSE) continue;
$data = $this->upload->data();
$data = $this->upload->data();
$img_name = $data['raw_name'];
$img_thumb_name = $img_name.'_thumb';
$full_thumb_name = $img_thumb_name.$data['file_ext'];
array_push($image_details, $full_thumb_name);
$uploadedFiles[$i] = $data;
if($data['is_image'] == 1) {
$configThumb['source_image'] = $data['full_path'];
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
}
}
If the file is an image, thumbnail is created.
1. Path to the folder to upload images should be specified. If the folder is in the root folder name of that folder can be used as the path.
$path = 'images';
Or else, a sub folder within the images folder can be given as follows,
$folder_name = $ad_id;
$path = 'images/'.$folder_name;
2. Checking whether a folder exists with the given name and if exists deleting it with its sub folders.
if(file_exists($path)){
$path = rtrim($path, '/') . '/';
$items = glob($path . '*');
foreach($items as $item)
{
is_dir($item) ? removeDir($item) : unlink($item);
}
rmdir($path);
}
3. Then create the folder with the given name
mkdir($path);
4. Next set configurations for the image
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$config['file_name'] = $next_ad_id;
$this->load->library('upload', $config);
$this->load->library('image_lib');
5. Setting Configurations for Thumbnail
$configThumb = array();
$configThumb['image_library'] = 'gd2';
$configThumb['source_image'] = '';
$configThumb['create_thumb'] = TRUE;
$configThumb['maintain_ratio'] = TRUE;
$configThumb['width'] = 140;
$configThumb['height'] = 210
6. Uploading images. HTML file input elements should have the name as 'photo1, photo2...'. Here I have 6 file input elements. So, images are uploaded in a loop.
for($i = 1; $i < 7; $i++) {
$upload = $this->upload->do_upload('photo'.$i);
if($upload == FALSE) continue;
$data = $this->upload->data();
$data = $this->upload->data();
$img_name = $data['raw_name'];
$img_thumb_name = $img_name.'_thumb';
$full_thumb_name = $img_thumb_name.$data['file_ext'];
array_push($image_details, $full_thumb_name);
$uploadedFiles[$i] = $data;
if($data['is_image'] == 1) {
$configThumb['source_image'] = $data['full_path'];
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
}
}
If the file is an image, thumbnail is created.
Comments
Post a Comment