因此,在這個網站上,管理員發佈圖像,然後通過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是資源在給定的字符串。
我認爲,這意味着圖像格式是錯誤的,但我不知道如何解決它。
對於任何人的信息,我添加水印代碼之前,一切正常。
我認爲問題在這裏'$ image = addslashes(file_get_contents($ _FILES ['photo'] ['tmp_name']));'爲什麼要在圖片文件內容中添加斜槓?這使得它的字符串值而不是圖像...我認爲 – Yazan 2014-11-08 10:34:47
我沒有真正的理由添加它。我知道什麼是斜槓,SQL注入它只是我不知道什麼極端的人可以用SQL注入攻擊我。它沒有解決任何問題,仍然是同樣的問題。 – 2014-11-08 10:37:57
好的,我認爲你需要使用'imagecreatefromstring()',檢查這個http://stackoverflow.com/questions/5770795/php-temporary-file-upload-not-valid-image-resource – Yazan 2014-11-08 11:08:08