Image watermarking is generally used for branding, for copyright text and so on. Images can be easily watermarked in CodeIgniter by using CI’s image manipulation class. The Watermarking feature requires the GD/GD2 library. This article will show you how to go about it.
Firstly, you have to load the image_lib library in your controller.
Syntax:
1 | $this->load->library(‘image_lib’); |
Once loaded, the image library object is available using:
1 | $this->image_lib |
This library helps you to resize, crop, rotate, create thumbnail and watermark your images, but we will only check out watermarking here.
1 | $this->image_lib->watermark(); |
In CodeIgniter, there are two types of watermarking that you can use, with small configurations changes;
1. Text watermark.(wm_text)
Codeigniter text watermark message will be generated using text, either with a True Type font that you specify, or using the native text output that the GD library supports.
1 2 3 4 5 6 7 8 9 10 11 | $config['source_image'] = $source_image; $config['wm_text'] = 'Copyright 2019 Sapphire'; $config['wm_type'] = 'text'; $config['wm_font_path'] = ./system/fonts/texb.ttf'; $config['wm_font_size'] = '16'; $config['wm_font_color'] = 'ffffff'; $config['wm_vrt_alignment'] = 'middle'; $config['wm_hor_alignment'] = 'center'; $config['wm_padding'] = '20'; $this->image_lib->initialize($config); if (!$this->image_lib->watermark()) {echo $this->image_lib->display_errors();} |
2. Image overlay watermark.(wm_overlay_path)
Codeigniter image watermark message will be generated by overlaying an image (usually a transparent PNG or GIF) containing your watermark over the uploaded source image.
1 2 3 4 5 6 7 8 9 | $config['image_library'] = 'gd2'; $config['source_image'] = $source_image; $config['wm_type'] = 'overlay'; $config['wm_overlay_path'] = 'uploads/sapp.PNG'; //the overlay image $config['wm_opacity'] = 50; $config['wm_vrt_alignment'] = 'middle'; $config['wm_hor_alignment'] = 'center'; $this->image_lib->initialize($config); if (!$this->image_lib->watermark()) {echo $this->image_lib->display_errors();} |
You can check out the watermarking preferences and their descriptions here:
https://www.codeigniter.com/userguide3/libraries/image_lib.html
Here is a full Example of Image Watermark:
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 | <?php defined('BASEPATH') or exit('No direct script access allowed'); class Welcome extends CI_Controller{ public function __construct(){ parent::__construct(); $this->load->library('image_lib'); } public function index(){ $this->upload(); } public function upload(){ $config['upload_path'] = 'uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '100'; $config['max_width'] = '1024'; $config['max_height'] = '768'; $this->load->library('upload', $config); $this->upload->do_upload();$data = $this->upload->data();//for text watermark $this->textWatermark($data['full_path']);//for overlay watermark $this->overlayWatermark($data['full_path']); } public function textWatermark($source_image){ $config['source_image'] = $source_image; //The path of the image to be watermarked $config['wm_text'] = 'Copyright 2019 Sapphire'; $config['wm_type'] = 'text'; $config['wm_font_path'] = 'C:/xampp/htdocs/watermark/system/fonts/texb.ttf'; $config['wm_font_size'] = '16'; $config['wm_font_color'] = 'ffffff'; $config['wm_vrt_alignment'] = 'middle'; $config['wm_hor_alignment'] = 'center'; $config['wm_padding'] = '20'; $this->image_lib->initialize($config); if (!$this->image_lib->watermark()) {echo $this->image_lib->display_errors(); } echo “<img src='uploads/text.jpg' style='width:600px';/>”; } public function overlayWatermark($source_image){ $config['image_library'] = 'gd2'; $config['source_image'] = $source_image; $config['wm_type'] = 'overlay'; $config['wm_overlay_path'] = 'uploads/sapp.PNG'; //the overlay image $config['wm_opacity'] = 50; $config['wm_vrt_alignment'] = 'middle'; $config['wm_hor_alignment'] = 'center'; $this->image_lib->initialize($config); if (!$this->image_lib->watermark()) {echo $this->image_lib->display_errors(); } echo “<img src='uploads/banner2.jpg' style='width:600px';/>”; } } |