Skip to content

CodeIgniter: Add multiple watermarks on the image

In this tutorial, You will learn how to use the CodeIgniter image library to watermark images with text or overlaying a watermark image on top of a base image.

CI’s image manipulation library allows us to do, image watermark in two ways with tiny configuration changes.

1. Text watermark. (wm_text)
2. Image over relay watermark. (wm_overlay_path)

Step 1 – Download Codeigniter Latest

Download the source code and unzip it. for more detailed installation instructions click here. And create an uploads directory and place base images and overly images (watermark image).

Step 2 – Start the Development server and Test the installation

Start your development server with the following command, feel free to change the port number if needed.

php -S localhost:8080

Step 3 – Create a Controller

Below is my Watermarkdemo controller, you will find both text and overlay examples in the controller.

<?php
if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}
class Watermarkdemo extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('image_lib');
    }

    public function text()
    {

        $watermarkLocations = $this->watermarkLocations();

        foreach ($watermarkLocations as $row) {
            $config['source_image'] = './uploads/test.jpg';
            //The image path,which you would like to watermarking
            $config['wm_text'] = 'arjunphp.com';
            $config['wm_type'] = 'text';
            $config['wm_font_path'] = './system/fonts/texb.ttf';
            $config['wm_font_size'] = 40;
            $config['wm_font_color'] = 'ffffff';
            $config['wm_hor_alignment'] = $row['horizontal']; // wm_hor_alignment =	left, center, right
            $config['wm_vrt_alignment'] = $row['vertical']; // wm_vrt_alignment	= top, middle, bottom
            $this->image_lib->initialize($config);
            $this->image_lib->watermark();
        }
        echo 'Successfully updated image with watermark';

    }
    
    public function overlay()
    {

        $watermarkLocations = $this->watermarkLocations();

        foreach ($watermarkLocations as $row) {
            $config['image_library'] = 'gd2';
            $config['source_image'] = './uploads/test.jpg';
            $config['wm_type'] = 'overlay';
            $config['wm_overlay_path'] = './uploads/logo.png';
            //the overlay image
            $config['wm_opacity'] = 50;
            $config['wm_hor_alignment'] = $row['horizontal']; // wm_hor_alignment =	left, center, right
            $config['wm_vrt_alignment'] = $row['vertical']; // wm_vrt_alignment	= top, middle, bottom
            $this->image_lib->initialize($config);
            $this->image_lib->watermark();
        }
 
        echo 'Successfully updated image with watermark';
        
    }

    
    protected function watermarkLocations() {
        return [
            ['horizontal' => 'left', 'vertical' => 'bottom'],
            ['horizontal' => 'left', 'vertical' => 'top'],
            ['horizontal' => 'right', 'vertical' => 'bottom'],
            ['horizontal' => 'right', 'vertical' => 'top'],
            ['horizontal' => 'center', 'vertical' => 'middle'],
        ];
    }

}

Step 4 – Test the watermark script

php -S localhost:8080

Once the server is up and running you should able to access the watermark controller and its methods at –

http://localhost:8080/index.php/watermarkdemo/text

http://localhost:8080/index.php/watermarkdemo/overlay

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments