2016-02-14 275 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'); –