如何使用图像库自定义Codeigniter水印文本和图像?

时间:2016-05-03 13:51:28

标签: php image codeigniter

我在上传图片时添加了水印文字和图片。

水印添加完美但水印尺寸问题。

当我上传中等大小的图像时,水印完美添加,但是当我上传大尺寸图像时,水印文字和图像显得非常小。如果我上传小图片,那么水印会显得非常大。

我想在所有类似尺寸的图像上添加水印。

所以水印的任何其他配置都可以解决这个问题。

代码:

    $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++;
        } 
}

2 个答案:

答案 0 :(得分:2)

        // 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++;
                } 

答案 1 :(得分:-1)

function doWatreMark($image){ $this->load->library('image_lib'); $config['source_image'] = BASEPATH.'../assets/portfolio/'.$image;
$config['wm_overlay_path'] = BASEPATH.'../assets/images/watermark-icon.png'; $config['wm_type'] = 'overlay'; $config['wm_opacity'] = '48'; $config['wm_x_transp'] = '9'; $config['wm_y_transp'] = '9';
$this->image_lib->initialize($config); $this->image_lib->watermark(); }