2014-11-08 93 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