Hello friends, Today we will show CodeIgniter 4 Watermark Image Example. In this example you will learn how to add text watermarking on the image in CodeIgniter 4 application. We will explain step by step CodeIgniter 4 image watermarking tutorial.
Here we will explain how to add text watermarking on the image in CodeIgniter 4 application. Sometime we need to upload image similarly to create image watermarking functionality to add digital signature on the image using image manipulation class.
CodeIgniter 4 Image Text Watermark Example
Step 1: Install CodeIgniter Project
In this we will install CodeIgniter 4 project for Image Text Watermark. so let’s following line run in your terminal.
composer create-project codeigniter4/appstarter
Step 2 : Basic Configurations
In this step, you will set some basic configuration on the app/config/app.php file, so let’s go to application/config/config.php and open this file on text editor.
Set Base URL like this
public $baseURL = 'http://localhost:8080'; To public $baseURL = 'http://localhost/demo/';
Step 3 : Create Database With Table
Now, you required to create database name demo, so open PHPMyAdmin and create the database with the name demo. After successfully create a database, you can use the following SQL query for creating a crop_images table in your database.
CREATE TABLE files( id int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary Key', name varchar(100) NOT NULL COMMENT 'Name', type varchar(255) NOT NULL COMMENT 'File Type', created_at varchar(20) NOT NULL COMMENT 'Created date', PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='demo table' AUTO_INCREMENT=1;
Step 4 : Setup Database Credentials
Now you required to connect our project to the database. So, visit app/Config/Database.php and open database.php file in text editor. After opening the file in a text editor, You required to set up database credentials in this file like following.
public $default = [ 'DSN' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'demo', '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
Now, You go to app/Controllers and create a controller name ImageUploadController.php. Then add bellow code into it:
<?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) ->text('Copyright Tutsmake.com @2021', [ 'color' => '#fff', 'opacity' => 0.7, 'withShadow' => true, 'hAlign' => 'center', 'vAlign' => 'bottom', 'fontSize' => 20 ]) ->save(FCPATH .'/images/'. $imgPath->getRandomName()); $imgPath->move(WRITEPATH . 'uploads'); $fileData = [ 'name' => $imgPath->getName(), 'type' => $imgPath->getClientMimeType() ]; $store = $db->insert($fileData); print_r('File uploaded successfully.'); } } } ?>
Step 6 : Create Views
Now, you required to create imageUploadForm.php, Go to application/views/ folder and create imageUploadForm.php file. So let’s update the bellow HTML into your files:
<!DOCTYPE html> <html> <head> <title>Codeigniter 4 Watermark Image Example - web-tuts.com</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/css/bootstrap.min.css"> </head> <body> <div class="container mt-5" style="max-width: 500px"> <h2 class="mb-4 text-center">Codeigniter 4 Watermark Image Example</h2> <form method='post' action='<?php echo base_url(); ?>/ImageUploadController/upload' 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="file"> </div> <div class="d-grid mt-3"> <input type="submit" value="Upload" class="btn btn-outline-primary" /> </div> </form> </div> </body> </html>
Step 7 : Add Routes
Now, visit app/Config/ folder and open Routes.php file. Then define in the routes in this file, as shown following:
$routes->setDefaultController('ImageUploadController'); $routes->get('/', 'ImageUploadController::index');
Step 8 : Start Development server
For start development server, open your terminal and execute the bellow command it:
php spark serve
Then, Go to the browser and hit following the URL:
http://localhost:8080
I hope you understand of CodeIgniter 4 Watermark Image and it can help you…