gpt4 book ai didi

php - 使用codeigniter上传时如何在图像的不同位置添加水印?

转载 作者:可可西里 更新时间:2023-10-31 23:27:35 25 4
gpt4 key购买 nike

我添加了如下代码以在图像中心添加水印:

if (!is_dir('assets/uploads_image/')) {
mkdir('assets/uploads_image/', 0777, true);
}
if (!is_dir('assets/uploads_watermark_image/')) {
mkdir('assets/uploads_watermark_image/', 0777, true);
}
$config1['upload_path'] = 'assets/uploads_image/';
$config1['allowed_types'] = 'jpeg|png|jpg|svg';
$this->load->library('upload', $config1);
$this->upload->initialize($config1);
if ( ! $this->upload->do_upload('image')) {
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('error', $error['error']);
redirect('photographer/uploadimage');
}
$config['upload_path'] = 'assets/uploads_watermark_image/';
$config['allowed_types'] = 'jpeg|png|jpg|svg';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$config['image_library'] = 'GD2';
$_FILES['file']['name'] = $_FILES['image']['name'];
$_FILES['file']['type'] = $_FILES['image']['type'];
$_FILES['file']['tmp_name'] = $_FILES['image']['tmp_name'];
$_FILES['file']['error'] = $_FILES['image']['error'];
$_FILES['file']['size'] = $_FILES['image']['size'];
$config['source_image'] = $_FILES['image']['tmp_name'];
$config['wm_type'] = 'overlay';
$config['wm_overlay_path'] = 'assets/img/overlay_watermark.png';
$config['wm_vrt_alignment'] = 'middle';
$config['wm_hor_alignment'] = 'center';
$config['wm_opacity'] = '100';
$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
$this->image_lib->watermark();

现在我想修改这段代码,需要在图像的所有角落和中心添加水印图像。请帮我解决这个问题。

提前感谢您的宝贵时间。

最佳答案

看起来您正在使用 CodeIgniter 2:https://codeigniter.com/userguide2/libraries/image_lib.html

所以看起来您需要一次添加一个水印。

您的代码示例稍作修改,首先定义您想要水印的所有位置,然后循环将水印添加到每个位置(基于您想要在每个位置使用相同水印的想法)。

// YOUR SETUP CODE

if (!is_dir('assets/uploads_image/')) {
mkdir('assets/uploads_image/', 0777, true);
}
if (!is_dir('assets/uploads_watermark_image/')) {
mkdir('assets/uploads_watermark_image/', 0777, true);
}
$config1['upload_path'] = 'assets/uploads_image/';
$config1['allowed_types'] = 'jpeg|png|jpg|svg';
$this->load->library('upload', $config1);
$this->upload->initialize($config1);
if ( ! $this->upload->do_upload('image')) {
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('error', $error['error']);
redirect('photographer/uploadimage');
}
$config['upload_path'] = 'assets/uploads_watermark_image/';
$config['allowed_types'] = 'jpeg|png|jpg|svg';
$this->load->library('upload', $config);
$this->upload->initialize($config);

// YOUR WATERMARK SETUP CODE

$config['image_library'] = 'GD2';
$_FILES['file']['name'] = $_FILES['image']['name'];
$_FILES['file']['type'] = $_FILES['image']['type'];
$_FILES['file']['tmp_name'] = $_FILES['image']['tmp_name'];
$_FILES['file']['error'] = $_FILES['image']['error'];
$_FILES['file']['size'] = $_FILES['image']['size'];
$config['source_image'] = $_FILES['image']['tmp_name'];
$config['wm_type'] = 'overlay';
$config['wm_overlay_path'] = 'assets/img/overlay_watermark.png';
$config['wm_opacity'] = '100';
$this->load->library('image_lib', $config);


// DEFINE YOUR WATERMARK LOCATIONS

$watermark_array = array(
array('horizontal' => 'center', 'vertical' => 'middle'),
array('horizontal' => 'left', 'vertical' => 'top'),
array('horizontal' => 'right', 'vertical' => 'top'),
array('horizontal' => 'left', 'vertical' => 'bottom'),
array('horizontal' => 'right', 'vertical' => 'bottom')
);


// ADD YOUR WATERMARKS

foreach ($watermark_array as $row) {
$config['wm_vrt_alignment'] = $row['vertical'];
$config['wm_hor_alignment'] = $row['horizontal'];

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

关于php - 使用codeigniter上传时如何在图像的不同位置添加水印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45986981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com