Documente Academic
Documente Profesional
Documente Cultură
Why?
article.
That way, I have several images with different sizes so that I can put
In the picture above, it can be seen in the blog image, I displayed a
(710x344px).
If you skip this step, that means you missed the whole of this tutorial.
tutorial.
Don't skip this step, even though it feels complex.
1. Codeigniter
site: www.codeigniter.com
2. Jquery
site: jquery.com
3. Bootstrap
Bootstrap is a framework that contains CSS and JavaScript files that
site: https://getbootstrap.com/
the www directory (if you use a WampServer) or htdocs (if you use
a XAMPP).
directory c:/wamp/www/
Open the "upload" folder, then create a new folder
After that copy the bootstrap.css file into the css folder and copy
After that create 3 new folders "large", "medium", and "small" into
Large, medium and small folders are folders where the thumbnail
1. Autoload.php
application/config/autoload.php
1 $autoload['libraries'] = array();
2 $autoload['helper'] = array();
1 $autoload['libraries'] = array('session');
2 $autoload['helper'] = array('url');
2. Config.php
application/config/config.php
Open config.php using the text editor, then find the following code:
1 $config['base_url'] = '';
1 $config['base_url'] = 'http://localhost/upload/';
3.Routes.php
application/config/routes.php
Open routes.php using the text editor, then find the following code:
1 $route['default_controller'] = 'welcome';
1 $route['default_controller'] = 'upload';
with Codeigniter.
Multiple thumbnails are the result of uploading images of different
small(100 X 67 pixels).
original image.
1 <?php
3
function __construct(){
4
parent::__construct();
5
$this->load->library('upload');
6
}
7
function index(){
8 $this->load->view('upload_view');
9 }
10
11 function do_upload(){
$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
13
$config['encrypt_name'] = TRUE;
14
15
$this->upload->initialize($config);
16
17
if(!empty($_FILES['filefoto']['name'])){
18
if ($this->upload->do_upload('filefoto')){
19 $gbr = $this->upload->data();
20 //Compress Image
21 $this->_create_thumbs($gbr['file_name']);
27
28 }else{
}
30
}
31
32
function _create_thumbs($file_name){
33
// Image resizing config
34
$config = array(
35 // Large Image
36 array(
37 'image_library' => 'GD2',
39 'maintain_ratio'=> FALSE,
43 ),
44 // Medium Image
array(
45
'image_library' => 'GD2',
46
'source_image' => './assets/images/'.$file_name,
47
'maintain_ratio'=> FALSE,
48
'width' => 600,
49
'height' => 400,
50
'new_image' => './assets/images/medium/'.$file_name
51
),
52
// Small Image
53 array(
54 'image_library' => 'GD2',
55 'source_image' => './assets/images/'.$file_name,
56 'maintain_ratio'=> FALSE,
60 ));
61
62 $this->load->library('image_lib', $config[0]);
70
71 }
72
73
74
75
76
_create_thumbs().
thumbnails.
URL.
Next, create a view named “upload_view.php”
on “application/views”.
2 <!DOCTYPE html>
<html lang="en">
3
<head>
4
<meta charset="utf-8">
5
<title>Multiple Thumbnails Codeigniter.</title>
6
<link href="<?php echo base_url().'assets/css/bootstrap.css'?>" rel="stylesheet
7
</head>
8 <body>
9 <div class="container">
11 <div class="row">
16 </div>
</form>
18
</div>
19
</div>
20
<script src="<?php echo base_url().'assets/js/jquery-3.3.1.min.js'?>"></script>
21
<script src="<?php echo base_url().'assets/js/bootstrap.js'?>"></script>
22 </body>
23 </html>
24
Then click “Browse…” to select the image that you want to upload.
upload is successful.
While the image outside the folder is an image with the original
resolution.
In this step, I will share with you how to upload multiple sizes of
thumbnails and insert the path of the images into the database.
1
CREATE TABLE images(
2 image_id INT PRIMARY KEY AUTO_INCREMENT,
3 image_title VARCHAR(100),
4 image_large VARCHAR(50),
5 image_medium VARCHAR(50),
image_small VARCHAR(50)
6
)ENGINE=INNODB;
7
The query above will create a table with the name "images" with
“application/config” folder.
1 $autoload['libraries'] = array('session');
1 $autoload['libraries'] = array('session','database');
1 $active_group = 'default';
2 $query_builder = TRUE;
4 $db['default'] = array(
23
24
1 $active_group = 'default';
$query_builder = TRUE;
2
3
$db['default'] = array(
4
'dsn' => '',
5
'hostname' => 'localhost',
6
'username' => 'root',
7
'password' => '',
8 'database' => 'thumbs_db',
9 'dbdriver' => 'mysqli',
10 'dbprefix' => '',
23
24
#3. Models
1 <?php
3
class Upload_model extends CI_Model{
4
//show all images
5
function show_images(){
6
$query = $this->db->get('images');
7
return $query;
8
}
9
10 //insert to database
11 function insert_images($title,$image_large,$image_medium,$image_small){
12 $data = array(
17 );
18 $this->db->insert('images', $data);
}
19
20
}
21
22
#4. Controller
1 <?php
3
4 class Upload extends CI_Controller{
6 function __construct(){
parent::__construct();
7
$this->load->library('upload');
8
//load model Upload_model.php
9
$this->load->model('Upload_model','upload_model');
10
}
11
12 function index(){
13 $this->load->view('upload_view');
14 }
15
16 function do_upload(){
$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
18
$config['encrypt_name'] = TRUE;
19
20
$this->upload->initialize($config);
21
22
if(!empty($_FILES['filefoto']['name'])){
23
if ($this->upload->do_upload('filefoto')){
24 $img = $this->upload->data();
25 //Compress Image
26 $this->_create_thumbs($img['file_name']);
27
28 $title = $this->input->post('title',TRUE);
29 $image_large = $img['file_name'];
$image_medium = $img['file_name'];
30
$image_small = $img['file_name'];
31
32
$this->upload_model->insert_images($title,$image_large,$image_mediu
33
$this->session->set_flashdata('msg','<div class="alert alert-info">
34 Successful.</div>');
35 redirect('upload/show_images');
36 }else{
echo $this->upload->display_errors();
37
}
38
39
}else{
40
echo "image is empty or type of image not allowed";
41
}
42 }
43
44 function _create_thumbs($file_name){
45 // Image resizing config
46 $config = array(
47 // Large Image
array(
48
'image_library' => 'GD2',
49
'source_image' => './assets/images/'.$file_name,
50
'maintain_ratio'=> FALSE,
51
'width' => 700,
52
'height' => 467,
53
'new_image' => './assets/images/large/'.$file_name
54
),
55
// Medium Image
56 array(
57 'image_library' => 'GD2',
59 'maintain_ratio'=> FALSE,
63 ),
64 // Small Image
array(
65
66 'image_library' => 'GD2',
68 'maintain_ratio'=> FALSE,
));
72
73
$this->load->library('image_lib', $config[0]);
74
foreach ($config as $item){
75
$this->image_lib->initialize($item);
76
if(!$this->image_lib->resize()){
77 return false;
78 }
79 $this->image_lib->clear();
80 }
81 }
82
//function to show images to view
83
function show_images(){
84
$data['images']=$this->upload_model->show_images();
85
$this->load->view('images_view', $data);
86
}
87
88 }
89
90
91
92
93
94
#5. Views
1
<!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="utf-8">
10
<div class="row">
11
<form class="form-horizontal" action="<?php echo site_url('upload/do_uploa
12 data">
13
<div class="form-group">
14
<label for="FormControlTitle">Title:</label>
15
<input type="text" name="title" class="form-control" id="FormControlTit
16
</div>
17
18
<div class="form-group">
19 <label for="FormControlFile">File Image:</label>
20 <input type="file" name="filefoto" class="form-control-file" id="FormCo
21 </div>
22
24 </form>
</div>
25
26
27 </div>
32
1
<!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="utf-8">
</head>
7
<body>
8
<div class="container-fluid">
9
<?php echo $this->session->flashdata('msg');?>
10
11
<div class="row">
12 <table class="table">
13 <thead>
14 <tr>
15 <th>Title</th>
16 <th>Image Large</th>
<th>Image Medium</th>
17
<th>Image Small</th>
18
19 </tr>
20 </thead>
<tbody>
21
<?php foreach($images->result() as $row):?>
22
<tr>
23
<td><?php echo $row->image_title;?></td>
24
<td><img src="<?php echo base_url().'assets/images/large/'.$row->
25 <td><img src="<?php echo base_url().'assets/images/medium/'.$row-
26 <td><img src="<?php echo base_url().'assets/images/small/'.$row->
27 </tr>
28 <?php endforeach;?>
29 </tbody>
</table>
30
</div>
31
32
</div>
33
<script src="<?php echo base_url().'assets/js/jquery-3.3.1.min.js'?>"></script>
34
<script src="<?php echo base_url().'assets/js/bootstrap.js'?>"></script>
35 </body>
36 </html>
37
38
39
#6. Testing
Input Title and Browse for the image you want to upload.
In the picture above, you can see the difference of the resolution
resolution.
Conclusion:
This discussion is about how to create upload multiple sizes of
and resolutions.
So, you can display the image on your website according to the
resolution.