Manish Tiwari Manish Tiwari - 7 months ago 449
PHP Question

How to customize Codeigniter Watermark text and Image using image library?

I have added watermark text and image during uploading image.

Watermark is added perfectly but issue with watermark size.

when I upload medium size image watermark is added perfectly but when I upload large size image watermark text and image appear very small. And if I upload small image then watermark will appear very large.

I want to add watermark on all images with similar size.

So any other configuration for watermark so that this issue will resolved.

Code :

$this->load->library('upload');
$this->load->library('image_lib');
$config['image_library'] = 'GD2';

for ($i = 0; $i < $number_of_files; $i++) {
$_FILES['file']['name'] = $files['file']['name'][$i];
$_FILES['file']['type'] = $files['file']['type'][$i];
$_FILES['file']['tmp_name'] = $files['file']['tmp_name'][$i];
$_FILES['file']['error'] = $files['file']['error'][$i];
$_FILES['file']['size'] = $files['file']['size'][$i];
$config['source_image'] = $files['file']['tmp_name'][$i];
$config['wm_text'] = 'Copyright example.com';
$config['wm_type'] = 'text';
$config['wm_font_size'] = '16';
$config['wm_vrt_alignment'] = 'middle';
$config['wm_hor_alignment'] = 'center';
$this->image_lib->initialize($config);
$this->image_lib->watermark();

$config['source_image'] = $files['file']['tmp_name'][$i];
$config['wm_overlay_path'] = './application/assets/images/example.png';
$config['wm_type'] = 'overlay';
$config['width'] = '50';
$config['height'] = '50';
$config['padding'] = '50';
$config['wm_opacity'] = '100';
$config['wm_vrt_alignment'] = 'bottom';
$config['wm_hor_alignment'] = 'right';
$config['wm_vrt_offset'] = '100';
$this->image_lib->initialize($config);
$this->image_lib->watermark();

if (!$this->upload->do_upload("file")) {
$errors++;
}
}

Answer
        // Define it in configuration file. And call it
        // About configurations: https://codeigniter.com/user_guide/libraries/config.html
        $wm_font_size = 16; // Watermark height (font size) contains 16% of image 

        $this->load->library('upload');
        $this->load->library('image_lib');
        $config['image_library'] = 'GD2';

        for ($i = 0; $i < $number_of_files; $i++) {
                $_FILES['file']['name'] = $files['file']['name'][$i];
                $_FILES['file']['type'] = $files['file']['type'][$i];
                $_FILES['file']['tmp_name'] = $files['file']['tmp_name'][$i];
                $_FILES['file']['error'] = $files['file']['error'][$i];
                $_FILES['file']['size'] = $files['file']['size'][$i];
                $config['source_image'] = $files['file']['tmp_name'][$i];
                $config['wm_text'] = 'Copyright example.com';
                $config['wm_type'] = 'text';
                $config['wm_font_size'] = ceil($files['file'][image_height][$i]/100*wm_font_size);
                $config['wm_vrt_alignment'] = 'middle';
                $config['wm_hor_alignment'] = 'center';
                $this->image_lib->initialize($config);
                $this->image_lib->watermark();

                $config['source_image'] = $files['file']['tmp_name'][$i];
                $config['wm_overlay_path'] = './application/assets/images/example.png';
                $config['wm_type'] = 'overlay';
                $config['width'] = '50';
                $config['height'] = '50';
                $config['padding'] = '50';
                $config['wm_opacity'] = '100';
                $config['wm_vrt_alignment'] = 'bottom';
                $config['wm_hor_alignment'] = 'right';
                $config['wm_vrt_offset'] = '100';
                $this->image_lib->initialize($config);
                $this->image_lib->watermark();

                if (!$this->upload->do_upload("file")) {
                    $errors++;
                } 
Comments