利用php制作水印图片,通过表单输入的位置信息来控制这些函数的参数完整代码示例
时间: 2023-05-29 11:07:24 浏览: 60
以下是一个示例代码,其中包含了一个表单和处理水印图片的PHP代码:
HTML表单代码:
```
<form method="post" action="watermark.php">
<label for="image">选择图片:</label>
<input type="file" name="image" id="image"><br><br>
<label for="text">水印文字:</label>
<input type="text" name="text" id="text"><br><br>
<label for="x">X轴位置:</label>
<input type="number" name="x" id="x"><br><br>
<label for="y">Y轴位置:</label>
<input type="number" name="y" id="y"><br><br>
<label for="color">水印颜色:</label>
<input type="color" name="color" id="color"><br><br>
<input type="submit" value="添加水印">
</form>
```
PHP代码:
```
<?php
// 检查文件是否上传成功
if ($_FILES["image"]["error"] > 0) {
echo "上传错误:" . $_FILES["image"]["error"];
exit();
}
// 定义一些常量
define('WATERMARK_FONT', 'arial.ttf'); // 水印字体
define('WATERMARK_SIZE', 24); // 字体大小
define('WATERMARK_ANGLE', 0); // 文字旋转角度
// 获取表单数据
$text = $_POST['text'];
$x = $_POST['x'];
$y = $_POST['y'];
$color = $_POST['color'];
// 获取图片信息
$filename = $_FILES["image"]["tmp_name"];
list($width, $height, $type) = getimagesize($filename);
// 根据图片类型创建图像对象
switch ($type) {
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($filename);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($filename);
break;
case IMAGETYPE_GIF:
$image = imagecreatefromgif($filename);
break;
default:
echo "不支持的图片类型";
exit();
}
// 创建水印
$font_color = hex2rgb($color);
$watermark = imagettftext($image, WATERMARK_SIZE, WATERMARK_ANGLE, $x, $y, $font_color, WATERMARK_FONT, $text);
// 输出图像
header('Content-Type: image/jpeg');
imagejpeg($image);
// 释放内存
imagedestroy($image);
// 将16进制颜色转换为RGB格式
function hex2rgb($hex) {
$hex = str_replace('#', '', $hex);
if (strlen($hex) == 3) {
$r = hexdec(substr($hex, 0, 1) . substr($hex, 0, 1));
$g = hexdec(substr($hex, 1, 1) . substr($hex, 1, 1));
$b = hexdec(substr($hex, 2, 1) . substr($hex, 2, 1));
} else {
$r = hexdec(substr($hex, 0, 2));
$g = hexdec(substr($hex, 2, 2));
$b = hexdec(substr($hex, 4, 2));
}
return array($r, $g, $b);
}
?>
```
在上面的代码中,我们使用了`imagettftext()`函数来创建水印。该函数的参数包括:
- `$image`:要添加水印的图像资源
- `WATERMARK_SIZE`:字体大小
- `WATERMARK_ANGLE`:文字旋转角度
- `$x`:水印文字的X轴位置
- `$y`:水印文字的Y轴位置
- `$font_color`:水印文字的颜色,使用了`hex2rgb()`函数将表单中的16进制颜色转换为RGB格式
- `WATERMARK_FONT`:水印字体文件路径
- `$text`:水印文字内容
最后,我们使用`header()`函数将输出类型设置为`image/jpeg`,并使用`imagejpeg()`函数将图像输出到浏览器。然后,我们使用`imagedestroy()`函数释放内存。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)