我试图在图像(jpg/gif/png/jpeg)上添加水印,一个具有透明背景的 png...我有 2 个文件...第一个是 watermark.php:
<?php
require 'func/images.func.php';
if (isset($_FILES['image'])){
$file_name = $_FILES['image']['name'];
$file_tmp = $_FILES['image']['tmp_name'];
$name = explode('.', $file_name);
if (allowed_image($file_name) === true){
$file_name = $img_name .'.png';
watermark_image($file_tmp, 'images-watermark/uploads/' . $file_name);
} else{
echo '<p>no image, or image type not accepted.</p>';
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento senza titolo</title>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
Choose an image:
<input type="file" name="image" />
<input type="submit" value="watermark"/>
</form>
</body>
</html>
...第二个是 images.func.php:
<?php
function allowed_image($file_name){
$allow_set = array('jpg', 'jpeg', 'gif', 'png');
$file_ext = (explode('.', $file_name));
$end = end($file_ext);
return (in_array($end , $allow_set) === true) ? true : false;
}
function watermark_image($file, $destination){
$watermark = imagecreatefrompng('images-watermark/watermarkf.png');
$source = getimagesize($file);
$source_mime = $source['mime'];
if ($source_mime === 'image/png'){
$image = imagecreatefrompng($file);
}else if($source_mime === 'image/jpeg'){
$image = imagecreatefromjpeg($file);
}else if($source_mime === 'image/gif'){
$image = imagecreatefromgif($file);
}
imagecopy($image, $watermark, 70, 160, 0, 0, imagesx($watermark),imagesy($watermark));
imagepng($image, $destination);
imagedestroy($image);
imagedestroy($watermark);
}
?>
...我的代码适用于 jpg/png/jpeg 格式(图像具有透明背景的水印)...在 gif 图像中,水印没有透明背景...任何提示?提前致谢