Codeigniter 4 Watermark Image Example Tutorial
In this article, we will explain to you how to add text watermark on the image in CodeIgniter 4(Codeigniter 4 Watermark Image Example Tutorial). sometimes we need to text overlay watermark on the image.
In this example, we use Image Manipulation Class and which also allows actions such as Image Resizing, Thumbnail Creation, Image Cropping, Image Rotating, and Image Watermarking. so you can see our text overlay watermark example.
Overview
Step 1: Download Codeigniter
Step 2: Basic Configurations
Step 3: Create a Database in table
Step 4: Connect to Database
Step 5: Create Controller and Model
Step 6: Create Views Files
Step 7: Run The Application
Step 1: Download Codeigniter
If you want to download or install the latest version of CodeIgniter 4 then you can go to Codeigniter’s official site and download the latest version of Codeigniter 4. after the downloaded you can configure in “xampp/htdocs/” directory.
Step 2: Basic Configurations
If you want to Basic Configurations in your project then you can below Url.
Codeigniter 4 Removing Index.Php From Url
Step 3: Create a Database in table
In this step, We will create the database and table.
1 2 3 4 5 | CREATE TABLE IF NOT EXISTS `students` ( `id` int(11) NOT NULL AUTO_INCREMENT, `image` varchar(164) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1; |
Step 4: Connect to Database
Go to the “app/Config/Database.php” folder and open the database.php file some changes in this file like hostname, database username, database password, and database name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public $default = [ 'DSN' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'codeigniter4_jqurey_validation', 'DBDriver' => 'MySQLi', 'DBPrefix' => '', 'pConnect' => false, 'DBDebug' => (ENVIRONMENT !== 'production'), 'cacheOn' => false, 'cacheDir' => '', 'charset' => 'utf8', 'DBCollat' => 'utf8_general_ci', 'swapPre' => '', 'encrypt' => false, 'compress' => false, 'strictOn' => false, 'failover' => [], 'port' => 3306, ]; |
Step 5: Create Controller and Model
In this step, we will create the “Student.php” controller and the “StudentModel.php” model.
app/Controllers/Student.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | <?php namespace App\Controllers; use CodeIgniter\Controller; use App\Models\StudentModel; class Student extends Controller { public function __construct() { helper(['form', 'url']); } public function index() { return view('add'); } public function store() { $rules = [ 'image' => [ 'uploaded[image]', 'mime_in[image,image/jpg,image/jpeg,image/gif,image/png]', 'max_size[image,4096]', ], ]; if (!$rules) { return view('add', ['validation' => $this->validator]); } else { $imageFile = $this->request->getFile('image'); // Image manipulation $image = \Config\Services::image() ->withFile($imageFile) ->text('Copyright XpertPhp.com @2021', [ 'color' => '#fff', 'opacity' => 0.7, 'withShadow' => true, 'hAlign' => 'center', 'vAlign' => 'bottom', 'fontSize' => 20 ]) ->save(FCPATH .'/images/'. $imageFile->getRandomName()); $imageFile->move(WRITEPATH . 'uploads'); $data = [ 'image' => $imageFile->getName(), ]; $model = new StudentModel(); $save = $model->insert($data); return redirect()->to( base_url('student') ); } } } ?> |
app/Models/StudentModel.php
1 2 3 4 5 6 7 8 9 10 11 12 | <?php namespace App\Models; use CodeIgniter\Database\ConnectionInterface; use CodeIgniter\Model; class StudentModel extends Model { protected $table = 'Students'; protected $allowedFields = ['image']; } ?> |
Step 6: Create Views Files
Finally, we will create the add.php in the app/views directory.
app/views/add.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Codeigniter 4 Watermark Image Example Tutorial - XpertPhp</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"> </head> <body> <div class="container mt-5" style="max-width: 500px"> <h2 class="mb-4 text-center">Codeigniter 4 Image Upload with Text Watermark Example</h2> <form method='post' action='<?php echo site_url('student/store'); ?>' enctype='multipart/form-data'> <div class="form-group"> <label for="formFileLg" class="form-label">Select image :</label> <input class="form-control form-control-lg" type="file" name="image" /> </div> <div class="d-grid mt-3"> <input type="submit" value="Upload" class="btn btn-outline-primary" /> </div> </form> </div> </body> </html> |
Step 7: Run The Application
We can start the server and run the codeigniter 4 application using the below command.
1 | php spark serve |
Now we will run our example using the below Url in the browser.
1 | http://localhost:8080/student |
If you liked this article, you can also download it through our Github Repository.