Go to app/Controllers and create a controller file
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
class ImageUploadController extends Controller {
public function index() {
return view('imageUploadForm');
}
public function upload() {
helper(['form', 'url']);
/* access database */
$database = \Config\Database::connect();
$db = $database->table('files');
/* file validation */
$isValidFile = $this->validate([
'file' => [
'uploaded[file]',
'mime_in[file,image/jpg,image/jpeg,image/png,image/gif]',
'max_size[file,4096]',
]
]);
/* check validation */
if (!$isValidFile) {
print_r('Upload valid file upto 4mb size');
} else {
$imgPath = $this->request->getFile('file');
/* Image manipulation */
$image = \Config\Services::image()
->withFile($imgPath)
->resize(200, 100, true, 'height')
->save(FCPATH .'/images/'. $imgPath->getRandomName());
$imgPath->move(WRITEPATH . 'uploads');
$fileData = [
'name' => $imgPath->getName(),
'type' => $imgPath->getClientMimeType()
];
$store = $db->insert($fileData);
print_r('Image has been successfully resized');
}
}
} ?>
<!DOCTYPE html>
<html>
<head>
<title>Codeigniter Compress Image Size Example - Tutsmake.com</title>
</head>
<body>
<?php echo $error;?>
<form method='post' action='/ImageUploadController/upload' enctype='multipart/form-data'>
<input type="file" name="file" size="20" />
<input type="submit" value="upload" />
</form>
</body>
</html>