📜  在 codeigniter 中上传带水印的图像 - PHP (1)

📅  最后修改于: 2023-12-03 15:07:38.170000             🧑  作者: Mango

在 CodeIgniter 中上传带水印的图像

在 CodeIgniter 中上传图像是一个比较常见的任务,而给上传的图像加上水印则是给图像增加保护和提高品质的一种方式。本文将介绍如何在 CodeIgniter 中添加水印并上传图像。

步骤
  1. 创建上传表单

首先需要创建一个上传表单,用于让用户选择要上传的图像。可以使用 CodeIgniter 自带的表单辅助函数来创建表单:

echo form_open_multipart('upload/do_upload'); // 表单提交至 upload 控制器下的 do_upload 方法
echo form_upload('userfile'); // 上传文件的 input[type=file]
echo form_submit('submit', 'Upload'); // 提交按钮
echo form_close();

这里的上传表单中仅包含一个上传文件的 input[type=file] 和一个提交按钮。

  1. 配置上传设置

在 CodeIgniter 中,上传文件需要在配置文件中配置上传设置,包括允许上传文件的类型、文件大小限制等。可以在 application/config/config.php 文件中进行配置:

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width']  = '2000';
$config['max_height']  = '2000';

这里将上传文件保存在 ./uploads/ 目录下(若该目录不存在则需要手动创建),允许上传的文件类型为 gif、jpg 和 png,文件大小限制为 2048 KB,宽度和高度限制都为 2000 像素。

  1. 处理上传文件

代码片段:

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width']  = '2000';
$config['max_height']  = '2000';

$this->load->library('upload', $config);

if (! $this->upload->do_upload('userfile')) { // 如果上传失败
    $error = array('error' => $this->upload->display_errors());
    $this->load->view('upload_form', $error);
} else { // 如果上传成功
    $data = array('upload_data' => $this->upload->data());
    $this->load->library('image_lib');
    $config['image_library'] = 'gd2';
    $config['source_image'] = $data['upload_data']['full_path'];
    $config['create_thumb'] = FALSE;
    $config['maintain_ratio'] = TRUE;
    $config['width']         = 250;
    $config['height']       = 250;
    $this->image_lib->initialize($config);
    $this->image_lib->resize();
    $this->image_lib->clear();
    $config['source_image'] = $data['upload_data']['full_path'];
    $config['wm_text'] = 'Your Watermark';
    $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'] = 'top';
    $config['wm_hor_alignment'] = 'right';
    $config['wm_padding'] = '20';
    $this->image_lib->initialize($config);
    $this->image_lib->watermark();
    $this->image_lib->clear();
    $this->load->view('upload_success', $data);
}

首先通过 $this->load->library('upload', $config) 语句加载上传库并配置上传参数,然后使用 $this->upload->do_upload('userfile') 方法上传文件。如果上传失败,可以使用 $this->upload->display_errors() 方法获取错误信息并将其传递给表单视图;如果上传成功,则会返回上传的图像数据,包括文件名、路径等信息。随后可以使用 $this->load->library('image_lib') 语句加载图像处理库来对上传的图像进行处理。

  1. 添加水印

使用 $this->image_lib->initialize($config) 语句初始化库,并使用 $this->image_lib->resize() 方法对图像进行大小调整。随后可以使用 $this->image_lib->clear() 方法清除先前调用的所有设置。接着再次使用 $this->image_lib->initialize($config) 语句初始化库,传入水印设置参数,包括文本、字体、大小、颜色、对齐等参数。最后使用 $this->image_lib->watermark() 方法添加水印,并使用 $this->image_lib->clear() 方法清除先前调用的设置。

  1. 完成

最后可以将处理后的图像返回给用户或保存到数据库中,实现上传带水印的图像功能。

参考资料