2014-11-08 51 views
0

因此,在這個網站上,管理員發佈圖像,然後通過sql發送到數據庫我想添加水印。這是我這部分的代碼。水印到張貼的圖像

if(!$_POST['name'] | !$_POST['cat'] | !$_POST['frame'] | !$_POST['molding'] | !$_POST['price'] | $_FILES["photo"]["tmp_name"]) { 

    die('You did not fill in a required field.'); 

    } 

$image = file_get_contents($_FILES['photo']['tmp_name']); 
$_POST['name'] = addslashes($_POST['name']); 
$_POST['price'] = addslashes($_POST['price']); 
$_POST['molding'] = addslashes($_POST['molding']); 
$_POST['frame'] = addslashes($_POST['frame']); 
$_POST['cat'] = addslashes($_POST['cat']); 



// Load the stamp and the photo to apply the watermark to 

$stamp = imagecreatefrompng('watermark2.PNG'); 

$save_watermark_photo_address = 'watermark_photo2.jpg'; 

// Set the margins for the stamp and get the height/width of the stamp image 

$marge_right = 0; 
$marge_bottom = 0; 
$sx = imagesx($stamp); 
$sy = imagesy($stamp); 


$imgx = imagesx($image); 
$imgy = imagesy($image); 
$centerX=round($imgx/2) - ($sx/2); 
$centerY=round($imgy/2) - ($sy/2); 
// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 

imagecopy($image, $stamp, $centerX, $centerY, 0, 0, imagesx($stamp), imagesy($stamp)); 


// Output and free memory 
//header('Content-type: image/png'); 

imagejpeg($image, $save_watermark_photo_address, 80); 

最後的保存部分是檢查它。使用$圖像

了任何事情,與錯誤 預計參數1是資源在給定的字符串。

我認爲,這意味着圖像格式是錯誤的,但我不知道如何解決它。

對於任何人的信息,我添加水印代碼之前,一切正常。

+0

我認爲問題在這裏'$ image = addslashes(file_get_contents($ _FILES ['photo'] ['tmp_name']));'爲什麼要在圖片文件內容中添加斜槓?這使得它的字符串值而不是圖像...我認爲 – Yazan 2014-11-08 10:34:47

+0

我沒有真正的理由添加它。我知道什麼是斜槓,SQL注入它只是我不知道什麼極端的人可以用SQL注入攻擊我。它沒有解決任何問題,仍然是同樣的問題。 – 2014-11-08 10:37:57

+0

好的,我認爲你需要使用'imagecreatefromstring()',檢查這個http://stackoverflow.com/questions/5770795/php-temporary-file-upload-not-valid-image-resource – Yazan 2014-11-08 11:08:08

回答

0

更多的澄清, 您可能希望使用此

老colde:

$image = file_get_contents($_FILES['photo']['tmp_name']); 

新代碼:

$imgData= file_get_contents($_FILES['photo']['tmp_name']); 
$image = imagecreatefromstring($imgData); 

這是一個澄清我的上述評論的。

+0

哦,很抱歉聽到如果這個帖子修復了原始問題(創建水印),請標記爲答案 – Yazan 2014-11-08 11:28:07

+0

那麼我現在有一個新問題。我已經添加了水印,但現在我需要原始格式,當我做了file_get_contents()將它作爲BLOB放入我的數據庫。任何想法如何做到這一點?我嘗試了幾種方法。 – 2014-11-08 11:28:19

+0

使用$ imgData插入db ?! – Yazan 2014-11-08 11:29:46