Php 将水印图像添加到上载的图像
我使用这段代码将png图像作为水印添加到上传的图像中,但结果不是图像,不想使用header(),我希望代码继续执行其他php查询,而无需导航到另一个页面来显示图像。上传的图像没有水印,header()不会发布任何图像,只是一个小的灰色正方形Php 将水印图像添加到上载的图像,php,image,upload,watermark,Php,Image,Upload,Watermark,我使用这段代码将png图像作为水印添加到上传的图像中,但结果不是图像,不想使用header(),我希望代码继续执行其他php查询,而无需导航到另一个页面来显示图像。上传的图像没有水印,header()不会发布任何图像,只是一个小的灰色正方形 $path = "../large/"; $num = substr(md5(mt_rand(1,9999999999)),0,9); $new_name = $path.$num.".jpg"; $image = $num.".jpg"; move
$path = "../large/";
$num = substr(md5(mt_rand(1,9999999999)),0,9);
$new_name = $path.$num.".jpg";
$image = $num.".jpg";
move_uploaded_file($img_tmpname,$new_name);
$image = imagecreatefromjpeg($new_name);
$logoImage = imagecreatefrompng("images/watermark.png");
imagealphablending($logoImage, true);
$imageWidth=imagesx($image);
$imageHeight=imagesy($image);
$logoWidth=imagesx($logoImage);
$logoHeight=imagesy($logoImage);
imagecopy(
// destination
$image,
// source
$logoImage,
// destination x and y
$imageWidth-$logoWidth, $imageHeight-$logoHeight,
// source x and y
0, 0,
// width and height of the area of the source to copy
$logoWidth, $logoHeight);
// Set type of image and send the output
header("Content-type: image/png");
imagePng($image);
// Release memory
imageDestroy($image);
imageDestroy($imageLogo);
我使用下面的代码对此进行了测试,结果正常。显然,我使用了与我的测试系统相关的路径,但希望它能有所帮助 在上传和处理上传的文件时,最重要且经常被遗忘的事情之一是表单的
enctype
——因此我以我的测试表单为例
如果要保存图像并使用水印显示图像,请使用imagepng函数两次,一次使用文件名,另一次不使用
<form method='post' action='/test/so/wtrmarkimg.php' enctype='multipart/form-data'>
<h1>Image uploader - Watermark</h1>
<input type='file' name='image' />
<input type='submit' value='Submit' />
</form>
<?php
#$path = "../large/";
$path='c:/temp/';/* output path for images generated */
$watermarksrc=realpath( 'c:/wwwroot/images/watermark.png' );
if( isset( $_FILES['image'] ) ){
$img_tmpname=$_FILES['image']['tmp_name'];
$num = substr( md5( mt_rand( 1,9999999999 ) ),0,9);
$new_name = $path.$num.".jpg";
$image = $num.".jpg";
if( move_uploaded_file( $img_tmpname, $new_name ) ){
$image = imagecreatefromjpeg( $new_name );
$logoImage = imagecreatefrompng( $watermarksrc );
imagealphablending( $logoImage, true );
$imageWidth=imagesx($image);
$imageHeight=imagesy($image);
$logoWidth=imagesx($logoImage);
$logoHeight=imagesy($logoImage);
imagecopy(
$image,
$logoImage,
$imageWidth-$logoWidth, $imageHeight-$logoHeight,
0, 0,
$logoWidth, $logoHeight );
// Set type of image and send the output
header("Content-type: image/png");
imagepng( $image );/*display image with watermark */
@imagepng( $image, $new_name );/* save image with watermark */
// Release memory
imagedestroy( $image );
imagedestroy( $imageLogo );
}
} else {
echo "ERROR";
print_r($_FILES);
}
?>
图像上传器-水印
imagePng
应该是imagePng
并且imageDestroy
应该是imageDestroy
路径images/watermark.png
~首先文件是否存在于该位置,其次您是否尝试使用该图像的完整路径?是的,但同样的问题将此用于最酷的示例。没有错误,也没有发布图像。原始图像刚刚移动到路径。能否显示您更改代码的内容和方式?稍作调整,现在工作正常,非常感谢$path='../large/';/*生成的图像的输出路径*/$watermarksrc=realpath('images/watermark.png');