upload image with watermark in codeigniter

// Step 1: Load the necessary libraries
$this->load->library('image_lib');
$this->load->library('upload');

// Step 2: Set up configuration for image upload
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048; // Maximum file size in kilobytes
$config['encrypt_name'] = TRUE; // Encrypt the file name for security

$this->upload->initialize($config);

// Step 3: Upload the image
if ($this->upload->do_upload('userfile')) {
    $uploaded_data = $this->upload->data();

    // Step 4: Add watermark to the uploaded image
    $config['source_image'] = $uploaded_data['full_path'];
    $config['wm_text'] = 'Your Watermark Text';
    $config['wm_type'] = 'text';
    $config['wm_font_path'] = './system/fonts/texb.ttf'; // Path to the font file
    $config['wm_font_size'] = 16;
    $config['wm_font_color'] = 'ffffff'; // Hex color code
    $config['wm_vrt_alignment'] = 'middle'; // Vertical alignment
    $config['wm_hor_alignment'] = 'center'; // Horizontal alignment

    $this->image_lib->initialize($config);
    $this->image_lib->watermark();

    // Step 5: Clear memory and reset configurations
    $this->image_lib->clear();
    $this->upload->clear();

    // Step 6: Handle the rest of your logic (e.g., save data to the database)
    // Your code here...
} else {
    $error = $this->upload->display_errors();
    // Handle the upload error
    // Your code here...
}