2016-02-14 287 views
1

我使用此代码来将png图像添加为上传图像的水印,但结果不是图像,也不想使用header()我希望代码继续执行其他的PHP查询,而无需导航到另一个页面来显示图像。图像获得上传,但没有水印和标题()没有发布任何图像只是一个灰色小方块在上传的图像中添加水印图像

$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); 
+0

'imagePng'应该是'imagepng'和'imageDestroy'应该是'imagedestroy' – RamRaider

+0

相同的结果 –

+0

路径'图像/ watermark.png'〜首先请问文件存在于该位置,其次有你尝试使用该图像的完整路径? – RamRaider

回答

2

我测试了这个使用下面的代码,这工作确定。很明显,我已经使用了与我的测试系统相关的路径,但希望它能帮助。

上传和处理上传文件时最重要也常常被遗忘的事情之一是表格的enctype--所以我将测试表格作为示例。

如果要保存图像并将其与水印一起显示,请使用imagepng函数两次,一次使用文件名,另一次使用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); 
    } 
?> 
+0

没有错误,没有张贴的图像。原始图像刚刚移动到路径中。 –

+0

你能告诉你什么以及你如何改变你的代码吗? – RamRaider

+0

调整了一下,现在很好地工作,非常感谢 $ path ='../large /';/*输出图像生成路径*/ \t $ watermarksrc = realpath('images/watermark.png'); –