Multiple Image Upload and Resize Image In CodeIgniter
First you must load “image_lib” library of CI.
This is a controller in CI
public function imageUpload()
{
$data = array();
if ($this->input->post('submit'))
{
if ($_FILES["sc_name"]["name"])
{
$path = './upload_image';
if (!is_dir($path))
mkdir($path, 0755);
$pathMain = './upload_image/source';
if (!is_dir($pathMain))
mkdir($pathMain, 0755);
$pathThumb = './upload_image/100X100';
if (!is_dir($pathThumb))
mkdir($pathThumb, 0755);
$path2Thumb = './upload_image/200X200';
if (!is_dir($path2Thumb))
mkdir($path2Thumb, 0755);
$path3Thumb = './upload_image/300X300';
if (!is_dir($path3Thumb))
mkdir($path3Thumb, 0755);
$path4Thumb = './upload_image/400X400';
if (!is_dir($path4Thumb))
mkdir($path4Thumb, 0755);
$result = $this->do_upload("sc_name", $pathMain);
if (!$result['status'])
$data['error_msg'] ="Can not upload Image for " . $result['error'] . " ";
else
{
$pathThumb = './upload_image/100X100';
if (!is_dir($pathThumb))
mkdir($pathThumb, 0755);
//in other folder
if($this->resize_image($pathMain . '/' . $result['upload_data']['file_name'], $pathThumb . '/'.time().'100X100.gif','100','100'))
echo 'Image 100X100 resize done';
else
echo 'Image 100X100 resize failed';
if($this->resize_image($pathMain . '/' . $result['upload_data']['file_name'], $path2Thumb . '/'.time().'200X200.gif','200','200'))
echo 'Image 200X200 resize done';
else
echo 'Image 200X200 resize failed';
if($this->resize_image($pathMain . '/' . $result['upload_data']['file_name'], $path3Thumb . '/'.time().'300X300.gif','300','300'))
echo 'Image 300X300 resize done';
else
echo 'Image 300X300 resize failed';
//in same folder
if($this->resize_image($pathMain . '/' . $result['upload_data']['file_name'], time().'100X100.gif','100','100'))
echo 'Image 100X100 Same Folder resize done';
else
echo 'Image 100X100 Same Folder resize failed';
if($this->resize_image($pathMain . '/' . $result['upload_data']['file_name'], time().'200X200.gif','200','200'))
echo 'Image 200X200 Same Folder resize done';
else
echo 'Image 200X200 Same Folder resize failed';
if($this->resize_image($pathMain . '/' . $result['upload_data']['file_name'], time().'300X300.gif','300','300'))
echo 'Image 300X300 Same Folder resize done';
else
echo 'Image 300X300 Same Folder resize failed';
}
}
}
$this->load->view('sc/upload_image', $data);
}
Upload Function inside the CI Controller
function
do_upload(
$htmlFieldName
,
$path
)
{
$config
[
'file_name'
] = time();
$config
[
'upload_path'
] =
$path
;
$config
[
'allowed_types'
] =
'gif|jpg|png'
;
$config
[
'max_size'
] =
'2000'
;
$config
[
'max_width'
] =
'2000'
;
$config
[
'max_height'
] =
'2000'
;
$this
->load->library(
'upload'
,
$config
);
$this
->upload->initialize(
$config
);
unset(
$config
);
if
(!
$this
->upload->do_upload(
$htmlFieldName
))
{
return
array
(
'error'
=>
$this
->upload->display_errors(),
'status'
=> 0);
}
else
{
return
array
(
'status'
=> 1,
'upload_data'
=>
$this
->upload->data());
}
}
Resize Function inside the CI Controller
function resize_image($sourcePath, $desPath, $width = '500', $height = '500')
{
$this->image_lib->clear();
$config['image_library'] = 'gd2';
$config['source_image'] = $sourcePath;
$config['new_image'] = $desPath;
$config['quality'] = '100%';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = true;
$config['thumb_marker'] = '';
$config['width'] = $width;
$config['height'] = $height;
$this->image_lib->initialize($config);
if ($this->image_lib->resize())
return true;
return false;
}
And Finally The form in View
<?
php
echo form_open_multipart() ?>
<
label
for
=
"sc_name"
>Name</
label
>
<
input
type
=
"file"
name
=
"sc_name"
/><
br
/>
<
input
type
=
"submit"
name
=
"submit"
value
=
"Create"
/>
Multiple Image Upload and Resize Image In CodeIgniter
Reviewed by Anonymous
on
April 11, 2018
Rating:
No comments: